Class: NinjaModel::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/ninja_model/attribute.rb

Defined Under Namespace

Modules: Format

Constant Summary collapse

VALID_TYPES =
[:string, :integer, :float, :date, :datetime, :boolean]
TRUE_VALUES =
[true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ Attribute

Returns a new instance of Attribute.

Raises:



12
13
14
15
16
# 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

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



8
9
10
# File 'lib/ninja_model/attribute.rb', line 8

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/ninja_model/attribute.rb', line 8

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/ninja_model/attribute.rb', line 8

def type
  @type
end

Class Method Details

.convert_to_string(value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# 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

.string_to_date(string) ⇒ Object



73
74
75
76
77
78
# 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) ⇒ Object



80
81
82
83
84
85
# 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) ⇒ Object

convert something to a boolean



88
89
90
91
92
93
94
# 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

Instance Method Details

#convert(value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# 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

#klassObject

Most of the following code was taken from ActiveRecord. Credit to the Rails team is due.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/ninja_model/attribute.rb', line 18

def number?
  [:integer, :float].include?(@type)
end