Module: Dynamodb::Querying::ClassMethods

Defined in:
lib/dynamodb/querying.rb

Instance Method Summary collapse

Instance Method Details

#_key_definitions(h_value, r_value = nil) ⇒ Object



84
85
86
87
88
# File 'lib/dynamodb/querying.rb', line 84

def _key_definitions(h_value, r_value = nil)
  attrs = { hash_key => h_value }
  attrs.merge!({ range_key => r_value }) if r_value
  attrs
end

#create(data = {}, conditions = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dynamodb/querying.rb', line 61

def create(data = {}, conditions = {})
  object = new(data)

  return object unless object.valid?
  # TODO: Need to test conditions - condition_expressions
  object.generate_timestamps
  put_item(table_name, object.data, conditions)

  object
rescue Aws::DynamoDB::Errors::ServiceError => e
  object.handle_error(e)
  object
end

#find(h_key, r_key = nil) ⇒ Object



50
51
52
53
54
# File 'lib/dynamodb/querying.rb', line 50

def find(h_key, r_key = nil)
  get_item(table_name, _key_definitions(h_key, r_key)) || not_found
rescue Aws::DynamoDB::Errors::ServiceError => e
  not_found
end

#find_or_initialize_by(h_key, r_key = nil) ⇒ Object



56
57
58
59
# File 'lib/dynamodb/querying.rb', line 56

def find_or_initialize_by(h_key, r_key = nil)
  response = find(h_key, r_key)
  response.is_a?(Hash) ? new(_key_definitions(h_key, r_key)) : response
end