Class: Xapit::FacetBlueprint

Inherits:
Object
  • Object
show all
Defined in:
lib/xapit/facet_blueprint.rb

Overview

A facet blueprint keeps track of the settings for indexing a given facet. You can specify a custom name for a given facet by providing a second argument when defining.

xapit do |index|
  index.facet :category_name, "Category"
end

Multiple facet values are supported for a single record. All you need to do is return an array of values instead of a single string.

def category_names
  categories.map(&:name) # => ["Toys", "Clothing"]
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(member_class, position, attribute, custom_name = nil) ⇒ FacetBlueprint

Returns a new instance of FacetBlueprint.



21
22
23
24
25
26
# File 'lib/xapit/facet_blueprint.rb', line 21

def initialize(member_class, position, attribute, custom_name = nil)
  @member_class = member_class
  @position = position
  @attribute = attribute
  @custom_name = custom_name
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



19
20
21
# File 'lib/xapit/facet_blueprint.rb', line 19

def attribute
  @attribute
end

#member_classObject (readonly)

Returns the value of attribute member_class.



17
18
19
# File 'lib/xapit/facet_blueprint.rb', line 17

def member_class
  @member_class
end

#positionObject (readonly)

Returns the value of attribute position.



18
19
20
# File 'lib/xapit/facet_blueprint.rb', line 18

def position
  @position
end

Instance Method Details

#identifiers_for(member) ⇒ Object



28
29
30
31
32
# File 'lib/xapit/facet_blueprint.rb', line 28

def identifiers_for(member)
  values_for(member).map do |value|
    Digest::SHA1.hexdigest(@attribute.to_s + value)[0..6]
  end
end

#nameObject

The name of the facet. This will return the custom name if given while setting up the index, or default to humanizing the attribute name.



36
37
38
# File 'lib/xapit/facet_blueprint.rb', line 36

def name
  @custom_name || @attribute.to_s.humanize
end

#save_facet_options_for(member) ⇒ Object



40
41
42
43
44
45
# File 'lib/xapit/facet_blueprint.rb', line 40

def save_facet_options_for(member)
  values_for(member).map do |value|
    option = FacetOption.new(member.class.name, @attribute.to_s, value)
    option.save
  end
end