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.



111
112
113
# File 'lib/sunspot/schema.rb', line 111

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.



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sunspot/schema.rb', line 134

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



118
119
120
121
# File 'lib/sunspot/schema.rb', line 118

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



126
127
128
# File 'lib/sunspot/schema.rb', line 126

def type
  @type.name
end