module Dynorm::Query

Public Instance Methods

find(hash = {}) click to toggle source
# File lib/dynorm/query.rb, line 40
def find(hash = {})
  resp = query(hash)
  Item.new @table_config, resp.items.first
end
get(key = {}) click to toggle source
# File lib/dynorm/query.rb, line 5
def get(key = {})
  params = default_params.merge(key: item_key(key))

  db.get_item(params)
end
put(item = {}) click to toggle source
# File lib/dynorm/query.rb, line 11
def put(item = {})
  params = default_params.merge(item: item)
  resp = db.put_item(params)
  resp.attributes
end
query(key, options = {}) click to toggle source
# File lib/dynorm/query.rb, line 31
def query(key, options = {})
  exp = KeyExpression.new(key)

  params = default_params.merge(exp.params)
  params = params.merge(option_params(options)) if options.any?

  db.query(params)
end
remove_attributes(key, *attrs) click to toggle source
# File lib/dynorm/query.rb, line 24
def remove_attributes(key, *attrs)
  params = default_params.merge(key: item_key(key))
  params = params.merge(attribute_updates: remove_attribute_params(attrs))

  db.update_item(params)
end
update(key, attrs) click to toggle source
# File lib/dynorm/query.rb, line 17
def update(key, attrs)
  params = default_params.merge(key: item_key(key))
  params = params.merge(attribute_updates: update_attribute_params(attrs))

  db.update_item(params)
end
where(key, options = {}) click to toggle source
# File lib/dynorm/query.rb, line 45
def where(key, options = {})
  resp = query(key, options)
  ItemCollection.new @table_config, resp.items
end

Private Instance Methods

item_key(key = {}) click to toggle source
# File lib/dynorm/query.rb, line 58
def item_key(key = {})
  params = { key.keys.first => key[key.keys.first] }
  params = params.merge(key.keys.last => key[key.keys.last]) if key.length != 1
  params
end
option_params(args) click to toggle source
# File lib/dynorm/query.rb, line 52
def option_params(args)
  args[:index_name] = args[:index] if args.keys.include?(:index)
  args.delete(:index)
  args
end
remove_attribute_params(keys, action = 'DELETE') click to toggle source
# File lib/dynorm/query.rb, line 70
def remove_attribute_params(keys, action = 'DELETE')
  keys.each_with_object({}) do |key, params|
    params[key] = { action: action }
  end
end
update_attribute_params(attrs, action = 'PUT') click to toggle source
# File lib/dynorm/query.rb, line 64
def update_attribute_params(attrs, action = 'PUT')
  attrs.keys.each_with_object({}) do |key, params|
    params[key] = { value: attrs[key], action: action }
  end
end