Class: Sunspot::Schema::DynamicField

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

Overview

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

Instance Method Summary (collapse)

Constructor Details

- (DynamicField) initialize(type, field_variants)

A new instance of DynamicField



115
116
117
# File 'sunspot/lib/sunspot/schema.rb', line 115

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

- (Object) method_missing(name, *args, &block)

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.



138
139
140
141
142
143
144
145
146
147
148
# File 'sunspot/lib/sunspot/schema.rb', line 138

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

- (Object) name

Name of the field in the schema



122
123
124
125
# File 'sunspot/lib/sunspot/schema.rb', line 122

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

- (Object) type

Name of the type as defined in the schema



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

def type
  @type.name
end