Class: Relish::DynamoHelper
- Inherits:
-
Object
- Object
- Relish::DynamoHelper
- Defined in:
- lib/relish/dynamo_helper.rb
Instance Method Summary collapse
- #db ⇒ Object
- #delete_version(id, version) ⇒ Object
- #get_version(id, version, *attrs) ⇒ Object
-
#initialize(aws_access_key, aws_secret_key, table_name, region = 'us-east-1') ⇒ DynamoHelper
constructor
A new instance of DynamoHelper.
- #inspect ⇒ Object (also: #to_s)
- #put(item) ⇒ Object
- #put_current_version(item) ⇒ Object
- #put_version(id, version, item) ⇒ Object
- #query(id, consistent, limit) ⇒ Object
- #query_current_version(id, *attrs) ⇒ Object
- #query_latest_version(id, *attrs) ⇒ Object
Constructor Details
#initialize(aws_access_key, aws_secret_key, table_name, region = 'us-east-1') ⇒ DynamoHelper
Returns a new instance of DynamoHelper.
6 7 8 9 10 11 |
# File 'lib/relish/dynamo_helper.rb', line 6 def initialize(aws_access_key, aws_secret_key, table_name, region = 'us-east-1') @aws_access_key = aws_access_key @aws_secret_key = aws_secret_key @table_name = table_name @region = region end |
Instance Method Details
#db ⇒ Object
13 14 15 |
# File 'lib/relish/dynamo_helper.rb', line 13 def db @db ||= Fog::AWS::DynamoDB.new(:aws_access_key_id => @aws_access_key, :aws_secret_access_key => @aws_secret_key, :region => @region) end |
#delete_version(id, version) ⇒ Object
59 60 61 |
# File 'lib/relish/dynamo_helper.rb', line 59 def delete_version(id, version) db.delete_item(@table_name, id: {:S => id}, :version => {:N => version}) end |
#get_version(id, version, *attrs) ⇒ Object
54 55 56 57 |
# File 'lib/relish/dynamo_helper.rb', line 54 def get_version(id, version, *attrs) response = db.get_item(@table_name, {:id => {:S => id}, :version => {:N => version}}, :ConsistentRead => true) response.body['Item'] end |
#inspect ⇒ Object Also known as: to_s
89 90 91 |
# File 'lib/relish/dynamo_helper.rb', line 89 def inspect "#<Relish::DynamoHelper>" end |
#put(item) ⇒ Object
72 73 74 |
# File 'lib/relish/dynamo_helper.rb', line 72 def put(item) db.put_item(@table_name, item) end |
#put_current_version(item) ⇒ Object
50 51 52 |
# File 'lib/relish/dynamo_helper.rb', line 50 def put_current_version(item) db.put_item(@table_name, item, {:Expected => {:id => {:Exists => false}, :version => {:Exists => false}}}) end |
#put_version(id, version, item) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/relish/dynamo_helper.rb', line 63 def put_version(id, version, item) db.put_item(@table_name, item, :ConditionExpression => 'id = :id AND version = :version', :ExpressionAttributeValues => { ':id' => {:S => id}, ':version' => {:N => version} }) end |
#query(id, consistent, limit) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/relish/dynamo_helper.rb', line 76 def query(id, consistent, limit) response = db.query(@table_name, :KeyConditionExpression => 'id = :id AND version > :version', :ExpressionAttributeValues => { ':id' => {:S => id}, ':version' => {:N => '0'} }, :ConsistentRead => consistent, :Limit => limit, :ScanIndexForward => false) response.body['Items'] end |
#query_current_version(id, *attrs) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/relish/dynamo_helper.rb', line 17 def query_current_version(id, *attrs) response = db.query(@table_name, :KeyConditionExpression => 'id = :id AND version > :version', :FilterExpression => 'draft <> :isDraft', :ExpressionAttributeValues => { ':id' => {:S => id}, ':version' => {:N => '0'}, ':isDraft' => {:BOOL => true} }, :ConsistentRead => true, :ScanIndexForward => false) count = response.body['Count'] || 0 if count > 0 response.body['Items'].first end end |
#query_latest_version(id, *attrs) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/relish/dynamo_helper.rb', line 34 def query_latest_version(id, *attrs) response = db.query(@table_name, :KeyConditionExpression => 'id = :id AND version > :version', :ExpressionAttributeValues => { ':id' => {:S => id}, ':version' => {:N => '0'}, }, :Limit => 1, :ConsistentRead => true, :ScanIndexForward => false) count = response.body['Count'] || 0 if count > 0 response.body['Items'].first end end |