Class: ActiveCouch::Attribute
- Inherits:
-
Object
- Object
- ActiveCouch::Attribute
- Defined in:
- lib/active_couch/attribute.rb
Constant Summary collapse
- TYPES =
{:decimal => Float, :text => String, :number => Integer}
- DEFAULTS =
{:decimal => 0.0, :text => "", :number => 0}
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(name, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #nil? ⇒ Boolean
- #to_hash ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/active_couch/attribute.rb', line 7 def initialize(name, = {}) klass, value = String, "" # Check for types supported if .has_key?(:which_is) type = [:which_is] unless type == :decimal || type == :number || type == :text raise InvalidCouchTypeError, "Types must be either decimal, number or text" else klass = TYPES[type]; value = DEFAULTS[type] end end # Check if default value provided matches the type provided if .has_key?(:with_default_value) value = [:with_default_value] unless value.is_a?(klass) || value.is_a?(NilClass) raise InvalidCouchTypeError, "Default value provided does not match the type of #{klass.to_s}" end end # Set the value, defaults to empty String @value, @klass, @name = value, klass, name.to_s end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
3 4 5 |
# File 'lib/active_couch/attribute.rb', line 3 def klass @klass end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/active_couch/attribute.rb', line 3 def name @name end |
#value ⇒ Object
Returns the value of attribute value.
3 4 5 |
# File 'lib/active_couch/attribute.rb', line 3 def value @value end |
Instance Method Details
#nil? ⇒ Boolean
41 42 43 |
# File 'lib/active_couch/attribute.rb', line 41 def nil? @value.nil? end |
#to_hash ⇒ Object
37 38 39 |
# File 'lib/active_couch/attribute.rb', line 37 def to_hash { @name => @value } end |