Module: AbstractModel

Included in:
EmbeddedAssociation, MongoModel
Defined in:
lib/yodel/models/core/model/abstract_model.rb

Instance Method Summary collapse

Instance Method Details

#embed_many(name, options = {}, &block) ⇒ Object Also known as: add_embed_many



24
25
26
27
# File 'lib/yodel/models/core/model/abstract_model.rb', line 24

def embed_many(name, options={}, &block)
  embedded_field = field(name, 'many_embedded', options)
  embedded_field.instance_exec(embedded_field, &block) if block_given?
end

#embed_one(name, options = {}, &block) ⇒ Object Also known as: add_embed_one



30
31
32
33
# File 'lib/yodel/models/core/model/abstract_model.rb', line 30

def embed_one(name, options={}, &block)
  embedded_field = field(name, 'one_embedded', options)
  embedded_field.instance_exec(embedded_field, &block) if block_given?
end

#field(name, type, options = {}) ⇒ Object Also known as: add_field



6
7
8
9
10
# File 'lib/yodel/models/core/model/abstract_model.rb', line 6

def field(name, type, options={})
  type = type.to_s
  options = deep_stringify_keys({'type' => type}.merge(options))
  fields[name.to_s] = Field.field_from_type(type).new(name.to_s, options)
end

#fieldsObject



2
3
4
# File 'lib/yodel/models/core/model/abstract_model.rb', line 2

def fields
  @fields ||= {}
end

#many(name, options = {}) ⇒ Object Also known as: add_many



36
37
38
39
# File 'lib/yodel/models/core/model/abstract_model.rb', line 36

def many(name, options={})
  type = query_association?(options) ? 'many_query' : 'many_store'
  field(name, type, options)
end

#modify_field(name, options = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/yodel/models/core/model/abstract_model.rb', line 13

def modify_field(name, options={})
  field = fields[name.to_s]
  field.options = field.options.dup.merge(deep_stringify_keys(options))
  field.instance_exec(field, &block) if block_given?
  changed!('record_fields') if respond_to?(:changed!)
end

#one(name, options = {}) ⇒ Object Also known as: add_one



42
43
44
45
# File 'lib/yodel/models/core/model/abstract_model.rb', line 42

def one(name, options={})
  type = query_association?(options) ? 'one_query' : 'one_store'
  field(name, type, options)
end

#remove_field(name) ⇒ Object



20
21
22
# File 'lib/yodel/models/core/model/abstract_model.rb', line 20

def remove_field(name)
  fields.delete(name.to_s)
end