Class: Hashme::Property
- Inherits:
-
Object
- Object
- Hashme::Property
- Defined in:
- lib/hashme/property.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#build(owner, value) ⇒ Object
Build a new object of the type defined by the property.
-
#cast(owner, value) ⇒ Object
Use cast method when we do not know if we may need to handle a casted array of objects.
-
#initialize(name, type, opts = {}) ⇒ Property
constructor
A new instance of Property.
- #to_s ⇒ Object
- #to_sym ⇒ Object
- #use_casted_array? ⇒ Boolean
Constructor Details
#initialize(name, type, opts = {}) ⇒ Property
Returns a new instance of Property.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/hashme/property.rb', line 6 def initialize(name, type, opts = {}) self.name = name.to_sym # Always set type to base type if type.is_a?(Array) && !type.first.nil? @_use_casted_array = true klass = type.first else @_use_casted_array = false klass = type end self.type = klass # Handle options self.default = opts[:default] end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
4 5 6 |
# File 'lib/hashme/property.rb', line 4 def default @default end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/hashme/property.rb', line 4 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
4 5 6 |
# File 'lib/hashme/property.rb', line 4 def type @type end |
Instance Method Details
#build(owner, value) ⇒ Object
Build a new object of the type defined by the property. Will not deal create CastedArrays!
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/hashme/property.rb', line 44 def build(owner, value) obj = nil if value.is_a?(type) obj = value elsif type == Date obj = type.parse(value) else obj = type.new(value) end obj.casted_by = owner if obj.respond_to?(:casted_by=) obj.casted_by_property = self if obj.respond_to?(:casted_by_property=) obj end |
#cast(owner, value) ⇒ Object
Use cast method when we do not know if we may need to handle a casted array of objects.
34 35 36 37 38 39 40 |
# File 'lib/hashme/property.rb', line 34 def cast(owner, value) if use_casted_array? CastedArray.new(owner, self, value) else build(owner, value) end end |
#to_s ⇒ Object
24 25 26 |
# File 'lib/hashme/property.rb', line 24 def to_s name.to_s end |
#to_sym ⇒ Object
28 29 30 |
# File 'lib/hashme/property.rb', line 28 def to_sym name end |
#use_casted_array? ⇒ Boolean
58 59 60 |
# File 'lib/hashme/property.rb', line 58 def use_casted_array? @_use_casted_array end |