Class: Rdy
- Inherits:
-
Object
show all
- Defined in:
- lib/rdy.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(table, hash_key) ⇒ Rdy
Returns a new instance of Rdy.
6
7
8
9
10
|
# File 'lib/rdy.rb', line 6
def initialize(table, hash_key)
@attributes = {}; @table = table; @hash_key = hash_key; @is_new = true
@_table = Rdy.dynamo_db.tables[@table]
@_table.status
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/rdy.rb', line 62
def method_missing(method, *args, &block)
if method.to_s[-1, 1] == '='
@attributes[method.to_s.gsub('=', '')] = args.first
else
@attributes[method.to_s]
end
end
|
Class Method Details
.create_table(table, read_capacity_units, write_capacity_units, hash_key, range_key = nil) ⇒ Object
23
24
25
26
|
# File 'lib/rdy.rb', line 23
def self.create_table(table, read_capacity_units, write_capacity_units, hash_key, range_key = nil)
dynamo_db.tables.create(table, read_capacity_units, write_capacity_units,
:hash_key => hash_key, :range_key => range_key)
end
|
.dynamo_db ⇒ Object
17
18
19
20
21
22
|
# File 'lib/rdy.rb', line 17
def self.dynamo_db
config = YAML.load(File.read("#{ENV['HOME']}/.rdy.yml"))
raise "Config file expected in ~/.rdy.yml" unless config
@@dynamo_db = AWS::DynamoDB.new(:access_key_id => config['access_key_id'],
:secret_access_key => config['secret_access_key'])
end
|
Instance Method Details
#all ⇒ Object
28
|
# File 'lib/rdy.rb', line 28
def all; @_table.items.collect {|i| i.attributes.to_h }; end
|
#attributes ⇒ Object
13
|
# File 'lib/rdy.rb', line 13
def attributes; @attributes; end
|
#destroy ⇒ Object
54
55
56
57
58
59
|
# File 'lib/rdy.rb', line 54
def destroy
unless is_new?
@_item.delete
@hash_value = nil; @_item = nil; @is_new = true
end
end
|
#find(hash_value) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/rdy.rb', line 29
def find(hash_value)
raise "missing hash value" if hash_value.nil?
@_item = @_table.items[hash_value]
@attributes.clear
if @_item.attributes.any?
@_item.attributes.to_h.each {|k, v| self.send("#{k}=".to_sym, v) unless k == @hash_key }
@hash_value = hash_value; @is_new = false
else
@hash_value = nil
end
@attributes
end
|
#hash_key ⇒ Object
15
|
# File 'lib/rdy.rb', line 15
def hash_key; @hash_key; end
|
#hash_value ⇒ Object
14
|
# File 'lib/rdy.rb', line 14
def hash_value; @hash_value; end
|
#is_new? ⇒ Boolean
42
|
# File 'lib/rdy.rb', line 42
def is_new?; @is_new; end
|
#save(hash_value = nil) ⇒ Object
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/rdy.rb', line 43
def save(hash_value = nil)
raise "missing hash value" if hash_value.nil? and is_new?
@_item = @_table.items.create(@hash_key.to_sym => hash_value) if is_new?
if @_item
@_item.attributes.set(@attributes)
@hash_value = hash_value if is_new?
@is_new = false
@_item.attributes.to_h
end
end
|
#table ⇒ Object
12
|
# File 'lib/rdy.rb', line 12
def table; @table; end
|
#table=(value) ⇒ Object
11
|
# File 'lib/rdy.rb', line 11
def table=(value); @table = value; end
|