Class: AppleData::DSL::CollectionDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/apple_data/dsl/collection_definition.rb

Overview

A definition of a collection of objects, with a particular schema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, mapper) ⇒ CollectionDefinition

Returns a new instance of CollectionDefinition.



10
11
12
13
# File 'lib/apple_data/dsl/collection_definition.rb', line 10

def initialize(name, mapper)
  @name = name
  @mapper = mapper
end

Instance Attribute Details

#mapperObject

Returns the value of attribute mapper.



8
9
10
# File 'lib/apple_data/dsl/collection_definition.rb', line 8

def mapper
  @mapper
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/apple_data/dsl/collection_definition.rb', line 7

def name
  @name
end

Instance Method Details

#build!(klass) ⇒ Object

Builds the collection into the given data file class



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/apple_data/dsl/collection_definition.rb', line 16

def build!(klass)
  name = @name
  klass.instance_eval do
    define_method(name) do
      @mapped_collections ||= {}
      if @mapped_collections[name].nil?
        collection_definition = self.class.const_get(:COLLECTIONS)[name]

        value = collection(name.to_s)
        result = case value.data
                 when Hash
                   construct_hash(collection_definition, value)
                 when Array
                   construct_array(collection_definition, value)
                 else
                   value.data
                 end

        @mapped_collections[name] = result
      end

      @mapped_collections[name]
    end
  end
end