Module: Elastictastic::Properties::ClassMethods

Defined in:
lib/elastictastic/properties.rb

Instance Method Summary collapse

Instance Method Details

#all_fields ⇒ Object



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

#define_embed(embed_name, options) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/elastictastic/properties.rb', line 94

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/elastictastic/properties.rb', line 69

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_field ⇒ Object



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



86
87
88
89
90
91
92
# File 'lib/elastictastic/properties.rb', line 86

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

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

#embeds ⇒ Object



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_properties ⇒ Object



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

def field_properties
  @field_properties ||= {}
end

#properties ⇒ Object



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

#select_fields ⇒ Object



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