module Elastictastic::Properties::ClassMethods

Public Instance Methods

all_fields() click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_field() { |"#{field}.#{embed_field}", embed_properties| ... } click to toggle source
# 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) click to toggle source
# 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
embeds() click to toggle source
# File lib/elastictastic/properties.rb, line 58
def embeds
  @_embeds ||= {}
end
field(*field_names, &block) click to toggle source
# 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() click to toggle source
# File lib/elastictastic/properties.rb, line 40
def field_properties
  @_field_properties ||= {}
end
properties() click to toggle source
# 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) click to toggle source
# File lib/elastictastic/properties.rb, line 54
def properties_for_field(field_name)
  properties[field_name.to_s]
end
route(instance) click to toggle source
# 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 = {}) click to toggle source
# File lib/elastictastic/properties.rb, line 73
def route_with(field, options = {})
  @_routing_field = field
  @_routing_required = !!options[:required]
end
routing_required?() click to toggle source
# File lib/elastictastic/properties.rb, line 78
def routing_required?
  !!@_routing_required
end
select_fields() { |field, properties| ... } click to toggle source
# 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