module Elastictastic::Properties

Public Class Methods

new(attributes = {}) click to toggle source
Calls superclass method
# File lib/elastictastic/properties.rb, line 138
def initialize(attributes = {})
  super()
  @_attributes = {}
  @_embeds = {}
  self.attributes = attributes
end

Public Instance Methods

_routing() click to toggle source
# File lib/elastictastic/properties.rb, line 202
def _routing
end
attributes() click to toggle source
Calls superclass method
# File lib/elastictastic/properties.rb, line 145
def attributes
  super.merge(@_attributes).with_indifferent_access
end
attributes=(attributes) click to toggle source
# File lib/elastictastic/properties.rb, line 149
def attributes=(attributes)
  attributes.each_pair do |field, value|
    __send__(:"#{field}=", value)
  end
end
elasticsearch_doc() click to toggle source
# 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) click to toggle source
# 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
inspect() click to toggle source
# 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

Protected Instance Methods

read_attribute(field) click to toggle source
# File lib/elastictastic/properties.rb, line 207
def read_attribute(field)
  @_attributes[field.to_s]
end
read_attributes() click to toggle source
# File lib/elastictastic/properties.rb, line 219
def read_attributes
  @_attributes
end
read_embed(field) click to toggle source
# File lib/elastictastic/properties.rb, line 235
def read_embed(field)
  @_embeds[field.to_s]
end
read_embeds() click to toggle source
# File lib/elastictastic/properties.rb, line 223
def read_embeds
  @_embeds
end
write_attribute(field, value) click to toggle source
# File lib/elastictastic/properties.rb, line 211
def write_attribute(field, value)
  if value.nil?
    @_attributes.delete(field.to_s)
  else
    @_attributes[field.to_s] = value
  end
end
write_attributes(attributes) click to toggle source
# File lib/elastictastic/properties.rb, line 227
def write_attributes(attributes)
  @_attributes = attributes
end
write_embed(field, value) click to toggle source
# File lib/elastictastic/properties.rb, line 239
def write_embed(field, value)
  @_embeds[field.to_s] = value
end
write_embeds(embeds) click to toggle source
# File lib/elastictastic/properties.rb, line 231
def write_embeds(embeds)
  @_embeds = embeds
end

Private Instance Methods

deserialize_value(field_name, value) click to toggle source
# File lib/elastictastic/properties.rb, line 262
def deserialize_value(field_name, value)
  return nil if value.nil?
  if self.class.properties_for_field(field_name)['type'].to_s == 'date'
    case value
    when Fixnum, Bignum
      sec, usec = value / 1000, (value % 1000) * 1000
      Time.at(sec, usec).utc
    else
      Time.parse(value)
    end
  else
    value
  end
end
serialize_value(field_name, value) click to toggle source
# File lib/elastictastic/properties.rb, line 245
def serialize_value(field_name, value)
  type = self.class.properties_for_field(field_name)['type'].to_s
  case type
  when 'date'
    time = value.to_time
    time.to_i * 1000 + time.usec / 1000
  when 'integer', 'byte', 'short', 'long'
    value.to_i
  when 'float', 'double'
    value.to_f
  when 'boolean'
    !!value
  else
    value
  end
end