Sha256: 75762ee66619e115177d1b61d4842a000fa8f203ccb95ae5daf57a2c707a6068

Contents?: true

Size: 1.48 KB

Versions: 54

Compression:

Stored size: 1.48 KB

Contents

module Locomotive::Steam
  module Adapters
    module Memory

      class Dataset

        class PrimaryKey
          def initialize
            @current = 0
          end

          def increment!
            yield(@current += 1)
            @current
          end
        end

        attr_reader :records, :name

        def initialize(name)
          @name = name
          clear!
        end

        def insert(record)
          @primary_key.increment! do |id|
            # if there is already an id, use it
            _id = record[identity] || id
            record[identity] = _id
            records[_id] = record
          end
        end

        def update(record)
          records[record[identity]] = records[record[identity]].deep_merge(record)
        end

        def delete(id)
          records.delete(id)
        end

        def size
          records.size
        end

        def all
          records.values
        end

        def find(id)
          records.fetch(id) do
            raise Locomotive::Steam::Repository::RecordNotFound, "could not find #{name} with #{identity} = #{id}"
          end
        end

        def exists?(id)
          !!id && records.has_key?(id)
        end

        def query(locale = nil, &block)
          Query.new(self, locale, &block)
        end

        def clear!
          @records = {}
          @primary_key = PrimaryKey.new
        end

        private

        def identity
          @identity ||= :_id
        end
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
locomotivecms_steam-1.8.0.alpha2 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.8.0.alpha1 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.7.1 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.7.0 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.6.1 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.6.0 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.6.0.rc1 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.6.0.beta1 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.5.3 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.5.2 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.5.1 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.5.0 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.5.0.rc1 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.5.0.rc0 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.5.0.beta3 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.5.0.beta2 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.5.0.beta1 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.4.1 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.4.0 lib/locomotive/steam/adapters/memory/dataset.rb
locomotivecms_steam-1.4.0.rc2 lib/locomotive/steam/adapters/memory/dataset.rb