Module: Elastictastic::Properties

Extended by:
ActiveSupport::Concern
Defined in:
lib/elastictastic/properties.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_routingObject



202
203
# File 'lib/elastictastic/properties.rb', line 202

def _routing
end

#attributesObject



145
146
147
# File 'lib/elastictastic/properties.rb', line 145

def attributes
  super.merge(@_attributes).with_indifferent_access
end

#attributes=(attributes) ⇒ Object



149
150
151
152
153
# File 'lib/elastictastic/properties.rb', line 149

def attributes=(attributes)
  attributes.each_pair do |field, value|
    __send__(:"#{field}=", value)
  end
end

#elasticsearch_docObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/elastictastic/properties.rb', line 165

def elasticsearch_doc
  {}.tap do |doc|
    @_attributes.each_pair do |field, value|
      next if value.nil?
      doc[field] = Util.call_or_map(value) do |item|
        serialize_value(field, item)
      end
    end
    @_embeds.each_pair do |field, embedded|
      next if embedded.nil?
      doc[field] = Util.call_or_map(embedded) do |item|
        item.elasticsearch_doc
      end
    end
  end
end

#elasticsearch_doc=(doc) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/elastictastic/properties.rb', line 182

def elasticsearch_doc=(doc)
  return if doc.nil?
  doc.each_pair do |field_name, value|
    if self.class.properties.has_key?(field_name)
      embed = self.class.embeds[field_name]
      if embed
        embedded = Util.call_or_map(value) do |item|
          embed.clazz.new.tap { |e| e.elasticsearch_doc = item }
        end
        write_embed(field_name, embedded)
      else
        deserialized = Util.call_or_map(value) do |item|
          deserialize_value(field_name, item)
        end
        write_attribute(field_name, deserialized)
      end
    end
  end
end

#initialize(attributes = {}) ⇒ Object



138
139
140
141
142
143
# File 'lib/elastictastic/properties.rb', line 138

def initialize(attributes = {})
  super()
  @_attributes = {}
  @_embeds = {}
  self.attributes = attributes
end

#inspectObject



155
156
157
158
159
160
161
162
163
# File 'lib/elastictastic/properties.rb', line 155

def inspect
  inspected = "#<#{self.class.name}"
  if attributes.any?
    inspected << ' ' << attributes.each_pair.map do |attr, value|
      "#{attr}: #{value.inspect}"
    end.join(', ')
  end
  inspected << '>'
end