Class: ActiveCouch::Attribute

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  klass, value = String, ""
  # Check for types supported
  if options.has_key?(:which_is)
    type = options[: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 options.has_key?(:with_default_value)
    value = options[: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

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/active_couch/attribute.rb', line 3

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/active_couch/attribute.rb', line 3

def name
  @name
end

#valueObject

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/active_couch/attribute.rb', line 41

def nil?
  @value.nil?
end

#to_hashObject



37
38
39
# File 'lib/active_couch/attribute.rb', line 37

def to_hash
  { @name => @value }
end