Sha256: ce3e3235ab7ad55d1c5405f9991e95d38ddb16c9c58d0a03b6dd80b080b2c35f

Contents?: true

Size: 1.28 KB

Versions: 28

Compression:

Stored size: 1.28 KB

Contents

require 'friendly/table'

module Friendly
  class DocumentTable < Table
    attr_reader :klass, :translator

    def initialize(klass, datastore=Friendly.datastore, translator=Translator.new)
      super(datastore)
      @klass      = klass
      @translator = translator
    end

    def table_name
      klass.table_name
    end

    def satisfies?(query)
      query.conditions.keys == [:id] && !query.order
    end

    def create(document)
      record = translator.to_record(document)
      datastore.insert(document, record)
      update_document(document, record)
    end

    def update(document)
      record = translator.to_record(document)
      datastore.update(document, document.id, record)
      update_document(document, record)
    end

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

    def first(query)
      record = datastore.first(klass, query)
      record && to_object(record)
    end

    def all(query)
      datastore.all(klass, query).map { |r| to_object(r) }
    end

    protected
      def update_document(document, record)
        attrs = record.reject { |k,v| k == :attributes }.merge(:new_record => false)
        document.attributes = attrs
      end

      def to_object(record)
        translator.to_object(klass, record)
      end
  end
end

Version data entries

28 entries across 28 versions & 7 rubygems

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