Class: NinjaModel::Attribute
- Inherits:
-
Object
- Object
- NinjaModel::Attribute
- 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
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .convert_to_string(value) ⇒ Object
- .string_to_date(string) ⇒ Object
- .string_to_time(string) ⇒ Object
-
.value_to_boolean(value) ⇒ Object
convert something to a boolean.
Instance Method Summary collapse
- #convert(value) ⇒ Object
-
#initialize(name, type, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
-
#klass ⇒ Object
Most of the following code was taken from ActiveRecord.
- #number? ⇒ Boolean
Constructor Details
#initialize(name, type, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
12 13 14 15 16 |
# File 'lib/ninja_model/attribute.rb', line 12 def initialize(name, type, = {}) @name, @type = name.to_s, type @default = [:default] raise UnsupportedType.new("Invalid type: #{@type}") unless VALID_TYPES.include?(@type) end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
8 9 10 |
# File 'lib/ninja_model/attribute.rb', line 8 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/ninja_model/attribute.rb', line 8 def name @name end |
#type ⇒ Object (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 |
#klass ⇒ Object
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
18 19 20 |
# File 'lib/ninja_model/attribute.rb', line 18 def number? [:integer, :float].include?(@type) end |