Sha256: f9cf7e250e499432ef97722a10285eef89e3ecf795733bba1df41899652bfbfc

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

require_relative './filter_builder'

module DynamoidAdvancedWhere
  class QueryMaterializer
    include Enumerable
    attr_accessor :query_builder


    delegate :klass, to: :query_builder
    delegate :table_name, to: :klass
    delegate :to_a, :first, to: :each

    delegate :must_scan?, to: :filter_builder

    def initialize(query_builder:)
      self.query_builder = query_builder
    end

    def all
      each.to_a
    end

    def each(&blk)
      return enum_for(:each) unless blk

      if must_scan?
        each_via_scan(&blk)
      else
        each_via_query(&blk)
      end
    end

    def each_via_query
      query = {
        table_name: table_name,
      }.merge(filter_builder.to_query_filter)

      results = client.query(query)

      if results.items
        results.items.each do |item|
          yield klass.from_database(item.symbolize_keys)
        end
      end
    end

    def each_via_scan
      query = {
        table_name: table_name
      }.merge(filter_builder.to_scan_filter)

      results = client.scan(query)

      if results.items
        results.items.each do |item|
          yield klass.from_database(item.symbolize_keys)
        end
      end
    end

    def filter_builder
      @filter_builder ||= FilterBuilder.new(
        root_node: query_builder.root_node,
        klass: klass,
      )
    end

    private
    def client
      Dynamoid.adapter.client
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
dynamoid_advanced_where-1.0.1 lib/dynamoid_advanced_where/query_materializer.rb
dynamoid_advanced_where-1.0.0 lib/dynamoid_advanced_where/query_materializer.rb
dynamoid-advanced-where-1.0.0 lib/dynamoid_advanced_where/query_materializer.rb