class ThinkingSphinx::Facet

Attributes

property[R]
value_source[R]

Public Class Methods

attribute_name_for(name) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
new(property, value_source = nil) click to toggle source
# 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
translate?(property) click to toggle source
# 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

Public Instance Methods

attribute_name() click to toggle source
# 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?() click to toggle source
# File lib/thinking_sphinx/facet.rb, line 76
def float?
  @property.type == :float
end
name() click to toggle source
# File lib/thinking_sphinx/facet.rb, line 56
def name
  property.unique_name
end
to_s() click to toggle source
# File lib/thinking_sphinx/facet.rb, line 94
def to_s
  name
end
translate?() click to toggle source
# File lib/thinking_sphinx/facet.rb, line 68
def translate?
  Facet.translate?(@property)
end
type() click to toggle source
# File lib/thinking_sphinx/facet.rb, line 72
def type
  @property.is_a?(Field) ? :string : @property.type
end
value(object, attribute_hash) click to toggle source
# 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

Private Instance Methods

column() click to toggle source
# File lib/thinking_sphinx/facet.rb, line 124
def column
  @property.columns.first
end
source_objects(object) click to toggle source
# File lib/thinking_sphinx/facet.rb, line 113
def source_objects(object)
  column.__stack.each { |method|
    object = Array(object).collect { |item|
      item.send(method)
    }.flatten.compact

    return nil if object.empty?
  }
  Array(object)
end
translate(object, attribute_value) click to toggle source
# File lib/thinking_sphinx/facet.rb, line 100
def translate(object, attribute_value)
  objects = source_objects(object)
  return if objects.blank?

  method = value_source || column.__name
  object = objects.one? ? objects.first : objects.detect { |item|
    result = item.send(method)
    result && result.to_crc32 == attribute_value
  }

  object.try(method)
end