Module: Elastictastic::Properties::ClassMethods

Defined in:
lib/elastictastic/properties.rb

Instance Method Summary collapse

Instance Method Details

#all_fieldsObject



34
35
36
37
38
# File 'lib/elastictastic/properties.rb', line 34

def all_fields
  @_all_fields ||= {}.tap do |fields|
    each_field { |field, properties| fields[field] = properties }
  end
end

#boost(field, options = {}) ⇒ Object



69
70
71
# File 'lib/elastictastic/properties.rb', line 69

def boost(field, options = {})
  @_boost = { 'name' => field.to_s, 'null_value' => 1.0 }.merge(options.stringify_keys)
end

#define_embed(embed_name, options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/elastictastic/properties.rb', line 115

def define_embed(embed_name, options)
  embed_name = embed_name.to_s
  embed = Association.new(embed_name, options)

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

    def #{embed_name}=(value)
      Util.call_or_each(value) do |check_value|
        unless check_value.nil? || check_value.is_a?(#{embed.class_name})
          raise TypeError, "Expected instance of class #{embed.class_name}; got \#{check_value.inspect}"
        end
      end
      write_embed(#{embed_name.inspect}, value)
    end
  RUBY

  embeds[embed_name] = embed
end

#define_field(field_name, options, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/elastictastic/properties.rb', line 90

def define_field(field_name, options, &block)
  field_name = field_name.to_s

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

    def #{field_name}=(value)
      write_attribute(#{field_name.inspect}, value)
    end
  RUBY

  field_properties[field_name.to_s] =
    Field.process(field_name, options, &block)
end

#each_fieldObject



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

def each_field
  properties.each_pair do |field, properties|
    if properties['properties']
      embeds[field].clazz.each_field do |embed_field, embed_properties|
        yield("#{field}.#{embed_field}", embed_properties)
      end
    elsif properties['fields']
      properties['fields'].each_pair do |variant_field, variant_properties|
        if variant_field == field
          yield(field, variant_properties)
        else
          yield("#{field}.#{variant_field}", variant_properties)
        end
      end
    else
      yield field, properties
    end
  end
end

#embed(*embed_names) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/elastictastic/properties.rb', line 107

def embed(*embed_names)
  options = embed_names.extract_options!

  embed_names.each do |embed_name|
    define_embed(embed_name, options)
  end
end

#embedsObject



58
59
60
# File 'lib/elastictastic/properties.rb', line 58

def embeds
  @_embeds ||= {}
end

#field(*field_names, &block) ⇒ Object



62
63
64
65
66
67
# File 'lib/elastictastic/properties.rb', line 62

def field(*field_names, &block)
  options = field_names.extract_options!
  field_names.each do |field_name|
    define_field(field_name, options, &block)
  end
end

#field_propertiesObject



40
41
42
# File 'lib/elastictastic/properties.rb', line 40

def field_properties
  @_field_properties ||= {}
end

#propertiesObject



44
45
46
47
48
49
50
51
52
# File 'lib/elastictastic/properties.rb', line 44

def properties
  return @_properties if defined? @_properties
  @_properties = {}
  @_properties.merge!(field_properties)
  embeds.each_pair do |name, embed|
    @_properties[name] = { 'properties' => embed.clazz.properties }
  end
  @_properties
end

#properties_for_field(field_name) ⇒ Object



54
55
56
# File 'lib/elastictastic/properties.rb', line 54

def properties_for_field(field_name)
  properties[field_name.to_s]
end

#route(instance) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/elastictastic/properties.rb', line 82

def route(instance)
  if @_routing_field
    @_routing_field.to_s.split('.').inject(instance) do |obj, attr|
      Util.call_or_map(obj) { |el| el.__send__(attr) } if obj
    end
  end
end

#route_with(field, options = {}) ⇒ Object



73
74
75
76
# File 'lib/elastictastic/properties.rb', line 73

def route_with(field, options = {})
  @_routing_field = field
  @_routing_required = !!options[:required]
end

#routing_required?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/elastictastic/properties.rb', line 78

def routing_required?
  !!@_routing_required
end

#select_fieldsObject



26
27
28
29
30
31
32
# File 'lib/elastictastic/properties.rb', line 26

def select_fields
  [].tap do |fields|
    each_field do |field, properties|
      fields << [field, properties] if yield(field, properties)
    end
  end
end