Class: Sunspot::Schema::DynamicField

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/schema.rb

Overview

Represents a dynamic field (in the Solr schema sense, not the Sunspot sense).

Instance Method Summary collapse

Constructor Details

#initialize(type, field_variants) ⇒ DynamicField

Returns a new instance of DynamicField.



129
130
131
# File 'lib/sunspot/schema.rb', line 129

def initialize(type, field_variants)
  @type, @field_variants = type, field_variants
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Implement magic methods to ask if a field is of a particular variant. Returns “true” if the field is of that variant and “false” otherwise.



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sunspot/schema.rb', line 152

def method_missing(name, *args, &block)
  if name.to_s =~ /\?$/ && args.empty?
    if @field_variants.any? { |variant| "#{variant.attribute}?" == name.to_s }
      'true'
    else
      'false'
    end
  else
    super(name.to_sym, *args, &block)
  end
end

Instance Method Details

#nameObject

Name of the field in the schema



136
137
138
139
# File 'lib/sunspot/schema.rb', line 136

def name
  variant_suffixes = @field_variants.map { |variant| variant.suffix }.join
  "*_#{@type.suffix}#{variant_suffixes}"
end

#typeObject

Name of the type as defined in the schema



144
145
146
# File 'lib/sunspot/schema.rb', line 144

def type
  @type.name
end