Sha256: c0e1174dd082226cd8ec600995b2447bf8320a1dec3df33f3b4045469647aada

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module ROM
  class Adapter

    class Memory < Adapter
      attr_reader :connection

      def self.schemes
        [:memory]
      end

      class Dataset
        include Charlatan.new(:data)

        def to_ary
          data
        end
        alias_method :to_a, :to_ary

        def each(&block)
          return to_enum unless block
          data.each(&block)
        end

        def restrict(criteria = nil, &block)
          if criteria
            find_all { |tuple| criteria.all? { |k, v| tuple[k] == v } }
          else
            find_all { |tuple| yield(tuple) }
          end
        end

        def project(*names)
          map { |tuple| tuple.reject { |key,_| names.include?(key) } }
        end

        def order(*names)
          sort_by { |tuple| tuple.values_at(*names) }
        end

        def insert(tuple)
          data << tuple
        end

        def header
          []
        end
      end

      class Storage
        attr_reader :data

        def initialize(*)
          super
          @data = {}
        end

        def [](name)
          data[name] ||= Dataset.new([])
        end
      end

      def initialize(*args)
        super
        @connection = Storage.new
      end

      def [](name)
        connection[name]
      end

      Adapter.register(self)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-0.3.1 lib/rom/adapter/memory.rb