Sha256: 58a44888e0e0d077085f113a0d890f140bd0c80f804ed1881e4e5dc03f729db4

Contents?: true

Size: 1.77 KB

Versions: 28

Compression:

Stored size: 1.77 KB

Contents

require 'friendly/table'

module Friendly
  class Index < Table
    attr_reader :klass, :fields, :datastore

    def initialize(klass, fields, datastore = Friendly.datastore)
      @klass     = klass
      @fields    = fields
      @datastore = datastore
    end

    def table_name
      ["index", klass.table_name, "on", fields.join("_and_")].join("_")
    end

    def satisfies?(query)
      exact_match?(query) || valid_partial_match?(query)
    end

    def first(query)
      row = datastore.first(self, query)
      row && klass.first(:id => row[:id])
    end

    def all(query)
      ids = datastore.all(self, query).map { |row| row[:id] }
      klass.all(:id => ids, :preserve_order! => !query.order.nil?)
    end

    def count(query)
      datastore.count(self, query)
    end

    def create(document)
      datastore.insert(self, record(document))
    end

    def update(document)
      datastore.update(self, document.id, record(document))
    end

    def destroy(document)
      datastore.delete(self, document.id)
    end

    protected
      def exact_match?(query)
        query.conditions.keys.map { |f| f.to_s }.sort == 
          fields.map { |f| f.to_s }.sort && 
            valid_order?(query.order)
      end

      def valid_partial_match?(query)
        condition_fields = query.conditions.keys
        sorted = condition_fields.sort { |a,b| field_index(a) <=> field_index(b) }
        sorted << query.order.expression if query.order
        sorted.zip(fields).all? { |a,b| a == b }
      end

      def valid_order?(order)
        order.nil? || order.expression == fields.last
      end

      def field_index(attr)
        fields.index(attr) || 0
      end

      def record(document)
        Hash[*(fields + [:id]).map { |f| [f, document.send(f)] }.flatten]
      end
  end
end

Version data entries

28 entries across 28 versions & 7 rubygems

Version Path
ihoka-friendly-0.8.0.pre lib/friendly/index.rb
ihoka-friendly-0.7.1.2 lib/friendly/index.rb
ihoka-friendly-0.7.1.1 lib/friendly/index.rb
ihoka-friendly-0.7.1 lib/friendly/index.rb
ihoka-friendly-0.7.0 lib/friendly/index.rb
ihoka-friendly-0.6.3 lib/friendly/index.rb
ihoka-friendly-0.6.2 lib/friendly/index.rb
friendly-0.6.0 lib/friendly/index.rb
honkster-friendly-0.5.3 lib/friendly/index.rb
honkster-friendly-0.5.2 lib/friendly/index.rb
honkster-friendly-0.5.1 lib/friendly/index.rb
wayne-friendly-0.5.1 lib/friendly/index.rb
wego-friendly-0.5.1 lib/friendly/index.rb
arunthampi-friendly-0.5.1 lib/friendly/index.rb
friendly_postgres-0.5.1 lib/friendly/index.rb
friendly-0.5.1 lib/friendly/index.rb
friendly-0.5.0 lib/friendly/index.rb
friendly_postgres-0.4.5 lib/friendly/index.rb
friendly-0.4.5 lib/friendly/index.rb
friendly-0.4.4 lib/friendly/index.rb