class NinjaModel::Attribute
Constants
- FALSE_VALUES
- TRUE_VALUES
- VALID_TYPES
Attributes
default[R]
name[R]
type[R]
Public Class Methods
convert_to_string(value)
click to toggle source
# File lib/ninja_model/attribute.rb, line 62 def convert_to_string(value) case value when String, NilClass value when Fixnum, Float, Date, DateTime, TrueClass, FalseClass value.to_s else raise InvalidConversion.new("Unable to convert #{value.inspect} to string") end end
new(name, type, options = {})
click to toggle source
# File lib/ninja_model/attribute.rb, line 12 def initialize(name, type, options = {}) @name, @type = name.to_s, type @default = options[:default] raise UnsupportedType.new("Invalid type: #{@type}") unless VALID_TYPES.include?(@type) end
string_to_date(string)
click to toggle source
# File lib/ninja_model/attribute.rb, line 73 def string_to_date(string) return string unless string.is_a?(String) return nil if string.empty? fast_string_to_date(string) || fallback_string_to_date(string) end
string_to_time(string)
click to toggle source
# File lib/ninja_model/attribute.rb, line 80 def string_to_time(string) return string unless string.is_a?(String) return nil if string.empty? fast_string_to_time(string) || fallback_string_to_time(string) end
value_to_boolean(value)
click to toggle source
convert something to a boolean
# File lib/ninja_model/attribute.rb, line 88 def value_to_boolean(value) if value.is_a?(String) && value.blank? nil else TRUE_VALUES.include?(value) end end
Protected Class Methods
fallback_string_to_date(string)
click to toggle source
# File lib/ninja_model/attribute.rb, line 132 def fallback_string_to_date(string) begin ::Date.strptime(string, I18n.translate('date.formats.default')) rescue ArgumentError nil end end
fallback_string_to_time(string)
click to toggle source
# File lib/ninja_model/attribute.rb, line 140 def fallback_string_to_time(string) time_hash = Date._parse(string) time_hash[:sec_fraction] = microseconds(time_hash) new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)) end
fast_string_to_date(string)
click to toggle source
# File lib/ninja_model/attribute.rb, line 117 def fast_string_to_date(string) if string =~ Format::ISO_DATE new_date $1.to_i, $2.to_i, $3.to_i end end
fast_string_to_time(string)
click to toggle source
Doesn’t handle time zones.
# File lib/ninja_model/attribute.rb, line 124 def fast_string_to_time(string) if string =~ Format::ISO_DATETIME microsec = ($7.to_f * 1_000_000).to_i res = new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec res end end
microseconds(time)
click to toggle source
‘0.123456’ -> 123456 ‘1.123456’ -> 123456
# File lib/ninja_model/attribute.rb, line 100 def microseconds(time) ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i end
new_date(year, mon, mday)
click to toggle source
# File lib/ninja_model/attribute.rb, line 104 def new_date(year, mon, mday) if year && year != 0 Date.new(year, mon, mday) rescue nil end end
new_time(year, mon, mday, hour, min, sec, microsec)
click to toggle source
# File lib/ninja_model/attribute.rb, line 110 def new_time(year, mon, mday, hour, min, sec, microsec) # Treat 0000-00-00 00:00:00 as nil. return nil if year.nil? || year == 0 DateTime.new(year, mon, mday, hour, min, sec, microsec) rescue nil end
Public Instance Methods
convert(value)
click to toggle source
# File lib/ninja_model/attribute.rb, line 41 def convert(value) case type when :string then self.class.convert_to_string(value) when :integer then value.to_i rescue value ? 1 : 0 when :float then value.to_f rescue value ? 1.0 : 0.0 when :date then self.class.string_to_date(value) when :datetime then self.class.string_to_time(value) when :boolean then self.class.value_to_boolean(value) end end
klass()
click to toggle source
Most of the following code was taken from ActiveRecord
. Credit to the Rails team is due.
# File lib/ninja_model/attribute.rb, line 26 def klass case type when :integer then Fixnum when :float then Float when :decimal then BigDecimal when :datetime then Time when :date then Date when :timestamp then Time when :time then Time when :text, :string then String when :binary then String when :boolean then Object end end
number?()
click to toggle source
# File lib/ninja_model/attribute.rb, line 18 def number? [:integer, :float].include?(@type) end