Module: Elastictastic::ParentChild::ClassMethods

Defined in:
lib/elastictastic/parent_child.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parent_associationObject (readonly)

Returns the value of attribute parent_association.



6
7
8
# File 'lib/elastictastic/parent_child.rb', line 6

def parent_association
  @parent_association
end

Instance Method Details

#belongs_to(parent_name, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/elastictastic/parent_child.rb', line 8

def belongs_to(parent_name, options = {})
  @parent_association = Association.new(parent_name, options)

  module_eval(<<-RUBY, __FILE__, __LINE__+1)
    def #{parent_name}
      _parent
    end
  RUBY
end

#child_association(name) ⇒ Object



29
30
31
# File 'lib/elastictastic/parent_child.rb', line 29

def child_association(name)
  child_associations[name.to_s]
end

#child_associationsObject



33
34
35
# File 'lib/elastictastic/parent_child.rb', line 33

def child_associations
  @_child_associations ||= {}
end

#has_many(children_name, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/elastictastic/parent_child.rb', line 18

def has_many(children_name, options = {})
  children_name = children_name.to_s
  child_associations[children_name] = Association.new(children_name, options)

  module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
    def #{children_name}
      read_child(#{children_name.inspect})
    end
  RUBY
end

#mappingObject



37
38
39
40
41
# File 'lib/elastictastic/parent_child.rb', line 37

def mapping
  super.tap do |mapping|
    mapping[type]['_parent'] = { 'type' => @parent_association.clazz.type } if @parent_association
  end
end