Class: MontageRails::Base::Column
- Inherits:
-
Object
- Object
- MontageRails::Base::Column
- Defined in:
- lib/montage_rails/base/column.rb
Constant Summary collapse
- TYPE_MAP =
{ "integer" => Integer, "float" => Float, "text" => String, "date" => Date, "time" => Time, "datetime" => DateTime, "numeric" => Numeric }
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#required ⇒ Object
(also: #required?)
Returns the value of attribute required.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #coerce(value) ⇒ Object
-
#initialize(name, type, required = false) ⇒ Column
constructor
A new instance of Column.
-
#is_f?(value) ⇒ Boolean
Determines if the string value passed in is a float Returns true or false.
-
#is_i?(value) ⇒ Boolean
Determines if the string value passed in is an integer Returns true or false.
- #value_valid?(value) ⇒ Boolean
Constructor Details
#initialize(name, type, required = false) ⇒ Column
Returns a new instance of Column.
18 19 20 21 22 |
# File 'lib/montage_rails/base/column.rb', line 18 def initialize(name, type, required = false) @name = name @type = type @required = required end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
14 15 16 |
# File 'lib/montage_rails/base/column.rb', line 14 def name @name end |
#required ⇒ Object Also known as: required?
Returns the value of attribute required.
14 15 16 |
# File 'lib/montage_rails/base/column.rb', line 14 def required @required end |
#type ⇒ Object
Returns the value of attribute type.
14 15 16 |
# File 'lib/montage_rails/base/column.rb', line 14 def type @type end |
Instance Method Details
#coerce(value) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/montage_rails/base/column.rb', line 42 def coerce(value) return value if value.is_a?(TYPE_MAP[type]) if is_i?(value) coerce_to = Integer elsif is_f?(value) coerce_to = Float else coerce_to = TYPE_MAP[type] end Virtus::Attribute.build(coerce_to).coerce(value) end |
#is_f?(value) ⇒ Boolean
Determines if the string value passed in is a float Returns true or false
38 39 40 |
# File 'lib/montage_rails/base/column.rb', line 38 def is_f?(value) /\A\d+\.\d+\z/ =~ value end |
#is_i?(value) ⇒ Boolean
Determines if the string value passed in is an integer Returns true or false
31 32 33 |
# File 'lib/montage_rails/base/column.rb', line 31 def is_i?(value) /\A\d+\z/ =~ value end |
#value_valid?(value) ⇒ Boolean
24 25 26 |
# File 'lib/montage_rails/base/column.rb', line 24 def value_valid?(value) !(required? && value.nil?) end |