Sha256: 114ac39c6802432a4b68641dea58d368dc2e8a6651d4a197caa5470d21974150

Contents?: true

Size: 1.33 KB

Versions: 30

Compression:

Stored size: 1.33 KB

Contents

require_relative 'concerns/key'
require_relative 'memory/order'
require_relative 'memory/condition'
require_relative 'memory/query'
require_relative 'memory/dataset'

module Locomotive::Steam

  module Memory
  end

  class MemoryAdapter

    attr_accessor_initialize :collection

    include Locomotive::Steam::Adapters::Concerns::Key

    def all(mapper, scope)
      memoized_dataset(mapper, scope)
    end

    def query(mapper, scope, &block)
      _query(mapper, scope, &block)
    end

    def find(mapper, scope, id)
      _query(mapper, scope) { where(_id: id) }.first
    end

    def base_url(mapper, scope, entity = nil)
      ''
    end

    private

    def _query(mapper, scope, &block)
      Locomotive::Steam::Adapters::Memory::Query.new(all(mapper, scope), scope.locale, &block)
    end

    def memoized_dataset(mapper, scope)
      return @dataset if @dataset
      dataset(mapper, scope)
    end

    def dataset(mapper, scope)
      Locomotive::Steam::Adapters::Memory::Dataset.new(mapper.name).tap do |dataset|
        collection.each do |attributes|
          # Note: very important to not manipulate the original attributes
          # since the attributes might be modified further by the to_entity method
          entity = mapper.to_entity(attributes.dup)
          dataset.insert(entity)
        end
      end
    end

  end

end


Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
locomotivecms_steam-1.4.1 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.4.0 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.4.0.rc2 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.4.0.rc1 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.4.0.pre.rc.1 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.3.0 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.3.0.rc2 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.1.2 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.2.1 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.3.0.rc1 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.2.0 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.2.0.rc3 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.2.0.rc2 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.2.0.rc1 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.2.0.beta1 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.1.1 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.1.0 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.1.0.rc3 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.1.0.rc2 lib/locomotive/steam/adapters/memory.rb
locomotivecms_steam-1.1.0.rc1 lib/locomotive/steam/adapters/memory.rb