Class: Cloudsearchable::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudsearchable/field.rb

Overview

Represents a single field in a CloudSearch index.

Constant Summary collapse

FieldTypes =
[:literal, :uint, :text].freeze
FieldTypeOptionsNames =

Maps the type of field to the name of the options hash when defining the field

{:literal => :literal_options, :uint => :u_int_options, :text => :text_options}.freeze
FieldTypeOptionsKeys =

Maps from field type to the allowed set of options for the field

{
  literal: [:default_value, :facet_enabled, :search_enabled, :result_enabled].freeze,
  uint:    [:default_value].freeze,
  text:    [:default_value, :facet_enabled, :result_enabled].freeze
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ Field

Returns a new instance of Field.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
# File 'lib/cloudsearchable/field.rb', line 23

def initialize(name, type, options = {})
  raise ArgumentError, "Invalid field type '#{type}'" unless FieldTypes.include?(type)
  @name = name.to_sym
  @type = type.to_sym
  @source = options[:source] || @name
  @options = options.slice(*FieldTypeOptionsKeys[@type])
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/cloudsearchable/field.rb', line 21

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



21
22
23
# File 'lib/cloudsearchable/field.rb', line 21

def options
  @options
end

#sourceObject (readonly)

Returns the value of attribute source.



21
22
23
# File 'lib/cloudsearchable/field.rb', line 21

def source
  @source
end

#typeObject (readonly)

Returns the value of attribute type.



21
22
23
# File 'lib/cloudsearchable/field.rb', line 21

def type
  @type
end

Instance Method Details

#define_in_domain(domain_name) ⇒ Object



39
40
41
42
43
44
# File 'lib/cloudsearchable/field.rb', line 39

def define_in_domain domain_name
  CloudSearch.client.define_index_field(
    :domain_name => domain_name,
    :index_field => definition
  )
end

#value_for(record) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cloudsearchable/field.rb', line 31

def value_for record
  if @source.respond_to?(:call)
    record.instance_exec &@source
  else
    record.send @source
  end
end