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



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

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

.attribute_name_from_value(name, value) ⇒ Object



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

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
22
# File 'lib/thinking_sphinx/facet.rb', line 14

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

.translate?(property) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#float?Boolean

Returns:

  • (Boolean)


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

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

#nameObject



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

def name
  property.unique_name
end

#to_sObject



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

def to_s
  name
end

#translate?Boolean

Returns:

  • (Boolean)


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

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

#typeObject



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

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

#value(object, attribute_hash) ⇒ Object



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

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