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, value_source = nil) ⇒ Facet

Returns a new instance of Facet.



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

def initialize(property, value_source = nil)
  @property     = property
  @value_source = value_source
  
  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

#value_sourceObject (readonly)

Returns the value of attribute value_source.



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

def value_source
  @value_source
end

Class Method Details

.attribute_name_for(name) ⇒ Object



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

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

.attribute_name_from_value(name, value) ⇒ Object



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

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



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

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)


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

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



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

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

#float?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/thinking_sphinx/facet.rb', line 75

def float?
  @property.type == :float
end

#nameObject



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

def name
  property.unique_name
end

#to_sObject



93
94
95
# File 'lib/thinking_sphinx/facet.rb', line 93

def to_s
  name
end

#translate?Boolean

Returns:

  • (Boolean)


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

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

#typeObject



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

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

#value(object, attribute_hash) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/thinking_sphinx/facet.rb', line 79

def value(object, attribute_hash)
  attribute_value = attribute_hash['@groupby']
  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