Sha256: a11cb9195584ecf36d0f84e0c8099ad4a2fdde231444373d522805d88a495178

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 KB

Contents

module Dynomite
  class Query
    include Enumerable

    def initialize(item, params)
      @item = item
      @params = params
    end

    def <<(item)
      raise NotImplementedError
    end

    def inspect
      "#<Dynomite::Query [#{first(2).map(&:inspect).join(', ')}, ...]>"
    end

    def each(&block)
      run_query.each(&block)
    end

    def index_name(name)
      self.class.new(@item, @params.merge(index_name: name))
    end

    def where(attributes)
      raise "attributes.size == 1 only supported for now" if attributes.size != 1

      attr_name = attributes.keys.first
      attr_value = attributes[attr_name]

      name_key, value_key = "##{attr_name}_name", ":#{attr_name}_value"
      params = {
        expression_attribute_names: { name_key => attr_name },
        expression_attribute_values: { value_key => attr_value },
        key_condition_expression: "#{name_key} = #{value_key}",
      }

      self.class.new(@item, @params.merge(params))
    end

    private

    def run_query
      @query ||= @item.query(@params)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dynomite-2.0.3 lib/dynomite/query.rb
dynomite-2.0.2 lib/dynomite/query.rb
dynomite-2.0.1 lib/dynomite/query.rb
dynomite-2.0.0 lib/dynomite/query.rb
dynomite-1.2.7 lib/dynomite/query.rb