Sha256: 7ade5152c55824221c5bb4ee455658a85850043feda30a0361c3746bb7efa955

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

module ThinkingSphinx
  class Facet
    attr_reader :reference
    
    def initialize(reference)
      @reference = reference
      
      if reference.columns.length != 1
        raise "Can't translate Facets on multiple-column field or attribute"
      end
    end
    
    def name
      reference.unique_name
    end
    
    def attribute_name
      @attribute_name ||= case @reference
      when Attribute
        @reference.unique_name.to_s
      when Field
        @reference.unique_name.to_s + "_sort"
      end
    end
    
    def value(object, attribute_value)
      return translate(object, attribute_value) if @reference.is_a?(Field)
      
      case @reference.type
      when :string, :multi
        translate(object, attribute_value)
      when :datetime
        Time.at(attribute_value)
      when :boolean
        attribute_value > 0
      else
        attribute_value
      end
    end
    
    def to_s
      name
    end
    
    private
    
    def translate(object, attribute_value)
      column.__stack.each { |method|
        object = object.send(method)
      }
      object.send(column.__name)
    end
    
    def column
      @reference.columns.first
    end
  end
end

Version data entries

4 entries across 4 versions & 3 rubygems

Version Path
freelancing-god-thinking-sphinx-1.1.4 lib/thinking_sphinx/facet.rb
freelancing-god-thinking-sphinx-1.1.5 lib/thinking_sphinx/facet.rb
pixeltrix-thinking-sphinx-1.1.5 lib/thinking_sphinx/facet.rb
sherpa99-thinking-sphinx-1.1.4 lib/thinking_sphinx/facet.rb