Class: ThinkingSphinx::Facet

Inherits:
Object
  • Object
show all
Defined in:
lib/thinking_sphinx/facet.rb

Direct Known Subclasses

ClassFacet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property) ⇒ Facet

Returns a new instance of Facet.



5
6
7
8
9
10
11
# File 'lib/thinking_sphinx/facet.rb', line 5

def initialize(property)
  @property = property
  
  if property.columns.length != 1
    raise "Can't translate Facets on multiple-column field or attribute"
  end
end

Instance Attribute Details

#propertyObject (readonly)

Returns the value of attribute property.



3
4
5
# File 'lib/thinking_sphinx/facet.rb', line 3

def property
  @property
end

Class Method Details

.attribute_name_for(name) ⇒ Object



22
23
24
# File 'lib/thinking_sphinx/facet.rb', line 22

def self.attribute_name_for(name)
  name.to_s == 'class' ? 'class_crc' : "#{name}_facet"
end

.attribute_name_from_value(name, value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/thinking_sphinx/facet.rb', line 26

def self.attribute_name_from_value(name, value)
  case value
  when String
    attribute_name_for(name)
  when Array
    if value.all? { |val| val.is_a?(Integer) }
      name
    else
      attribute_name_for(name)
    end
  else
    name
  end
end

.name_for(facet) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/thinking_sphinx/facet.rb', line 13

def self.name_for(facet)
  case facet
  when Facet
    facet.name
  when String, Symbol
    facet.to_s.gsub(/(_facet|_crc)$/,'').to_sym
  end
end

.translate?(property) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/thinking_sphinx/facet.rb', line 41

def self.translate?(property)
  return true if property.is_a?(Field)
  
  case property.type
  when :string
    true
  when :integer, :boolean, :datetime, :float
    false
  when :multi
    !property.all_ints?
  end
end

Instance Method Details

#attribute_nameObject



58
59
60
61
62
63
64
# File 'lib/thinking_sphinx/facet.rb', line 58

def attribute_name
  if translate?
    Facet.attribute_name_for(@property.unique_name)
  else
    @property.unique_name.to_s
  end
end

#nameObject



54
55
56
# File 'lib/thinking_sphinx/facet.rb', line 54

def name
  property.unique_name
end

#to_sObject



87
88
89
# File 'lib/thinking_sphinx/facet.rb', line 87

def to_s
  name
end

#translate?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/thinking_sphinx/facet.rb', line 66

def translate?
  Facet.translate?(@property)
end

#typeObject



70
71
72
# File 'lib/thinking_sphinx/facet.rb', line 70

def type
  @property.is_a?(Field) ? :string : @property.type
end

#value(object, attribute_value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/thinking_sphinx/facet.rb', line 74

def value(object, attribute_value)
  return translate(object, attribute_value) if translate? || float?
  
  case @property.type
  when :datetime
    Time.at(attribute_value)
  when :boolean
    attribute_value > 0
  else
    attribute_value
  end
end