Module: Uorm::Attributes

Defined in:
lib/uorm/attributes.rb,
lib/uorm/attributes/type.rb,
lib/uorm/mongo/object_id.rb,
lib/uorm/attributes/field.rb,
lib/uorm/attributes/field_collection.rb

Defined Under Namespace

Modules: Type Classes: Field, FieldCollection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



27
28
29
# File 'lib/uorm/attributes.rb', line 27

def attributes
  @attributes
end

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/uorm/attributes.rb', line 7

def self.included base
  base.instance_eval do
    def field name, options
      fields << ::Uorm::Attributes::Field.new(name, options[:type])

      define_method name do
        get_attribute name
      end

      define_method :"#{name}=" do |value|
        set_attribute name, fields.get(name).convert(value)
      end
    end

    def fields
      @fields ||= ::Uorm::Attributes::FieldCollection.new
    end
  end
end

Instance Method Details

#as_jsonObject



33
34
35
36
37
38
# File 'lib/uorm/attributes.rb', line 33

def as_json
  attributes.inject({}) do |hash, (key, value)|
    hash[key] = (field = fields.get(key)) ? field.as_json(value) : value
    hash
  end
end

#initialize(values = {}) ⇒ Object



29
30
31
# File 'lib/uorm/attributes.rb', line 29

def initialize values = {}
  initialize_attributes values
end