Sha256: 7f9c94c6e6b1b9adca5ae2bbc789923e8fb9e6ded187771fff9626c26b50bb8c

Contents?: true

Size: 1.17 KB

Versions: 1

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
    
    protected
    
    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

1 entries across 1 versions & 1 rubygems

Version Path
dpickett-thinking-sphinx-1.1.4 lib/thinking_sphinx/facet.rb