Module: Gorillib::Builder

Included in:
Wukong::Migration::Dsl
Defined in:
lib/gorillib/model/elasticsearch_ext.rb

Instance Method Summary collapse

Instance Method Details

#getset(field, *args, &block) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/gorillib/model/elasticsearch_ext.rb', line 4

def getset(field, *args, &block)
  ArgumentError.check_arity!(args, 0..1)
  if args.empty?
    read_attribute(field.name)
  else
    self.send("receive_#{field.name}", args.first)
  end
end

#getset_collection_item(field, item_key, attrs = {}, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gorillib/model/elasticsearch_ext.rb', line 13

def getset_collection_item(field, item_key, attrs={}, &block)
  plural_name = field.plural_name
  if attrs.is_a?(field.item_type)
    # actual object: assign it into collection                                                                                                                          
    val = attrs
    set_collection_item(plural_name, item_key, val)
  elsif has_collection_item?(plural_name, item_key)
    # existing item: retrieve it, updating as directed                                                                                                    
    val = get_collection_item(plural_name, item_key)
    val.receive!(attrs, &block)
  else
    # missing item: autovivify item and add to collection                                                                                                               
    params = { key_method => item_key, :owner => self }.merge(attrs)
    val = self.send("receive_#{field.singular_name}", params, &block)
    set_collection_item(plural_name, item_key, val)
  end
  val
end