Class: Rdy

Inherits:
Object
  • Object
show all
Defined in:
lib/rdy.rb

Direct Known Subclasses

RdyItem

Constant Summary collapse

@@_tables =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, hash_key, range_key = nil) ⇒ Rdy

Returns a new instance of Rdy.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rdy.rb', line 10

def initialize(table, hash_key, range_key = nil)
  @attributes = {}; @table = table.to_s; @hash_key = hash_key[0].to_s
  @range_key = range_key[0].to_s if range_key
  @is_new = true
  self.hash_key_conditional_check = false
  self.check_table_status = false
  if @@_tables[table]
    @_table = @@_tables[table]
  else
    @_table = Rdy.dynamo_db.tables[@table]
    if self.check_table_status
      if @_table.status == :active
        @@_tables[table] = @_table
      else
        raise "Table not active yet!"
      end
    else
      @_table.hash_key = [@hash_key.to_sym, hash_key[1].to_sym]
      @_table.range_key = [@range_key.to_sym, range_key[1].to_sym] if @range_key
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



141
142
143
144
145
146
147
# File 'lib/rdy.rb', line 141

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

Instance Attribute Details

#check_table_statusObject

Returns the value of attribute check_table_status.



7
8
9
# File 'lib/rdy.rb', line 7

def check_table_status
  @check_table_status
end

#hash_key_conditional_checkObject

Returns the value of attribute hash_key_conditional_check.



7
8
9
# File 'lib/rdy.rb', line 7

def hash_key_conditional_check
  @hash_key_conditional_check
end

Class Method Details

.create_table(table, read_capacity_units, write_capacity_units, hash_key, range_key = nil) ⇒ Object



49
50
51
52
# File 'lib/rdy.rb', line 49

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_dbObject



42
43
44
45
46
47
48
# File 'lib/rdy.rb', line 42

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'],
                                 :dynamo_db_endpoint => config['dynamo_db_endpoint'])
end

.find(table, hash_key_value, range_key_value = nil) ⇒ Object



63
64
65
66
67
# File 'lib/rdy.rb', line 63

def self.find(table, hash_key_value, range_key_value = nil)
  rdy = Rdy.new(table, hash_key_value[0..1], range_key_value ? range_key_value[0..1] : nil)
  rdy.find(hash_key_value[2], range_key_value ? range_key_value[2] : nil)
  rdy
end

.generate_keyObject



40
# File 'lib/rdy.rb', line 40

def self.generate_key; Digest::SHA1.hexdigest((0...50).map{ ('a'..'z').to_a[rand(26)] }.join); end

Instance Method Details

#allObject



62
# File 'lib/rdy.rb', line 62

def all; @_table.items.collect {|i| i.attributes.to_h }; end

#attributesObject



35
# File 'lib/rdy.rb', line 35

def attributes; @attributes; end

#build(attrs) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/rdy.rb', line 54

def build(attrs)
  if attrs
    @attributes.clear
    attrs.each {|k, v| self.send("#{k.to_s}=".to_sym, v) unless k == @hash_key }
    return self
  end
end

#countObject



85
# File 'lib/rdy.rb', line 85

def count; @_table.items.count; end

#destroyObject



133
134
135
136
137
138
# File 'lib/rdy.rb', line 133

def destroy
  unless is_new?
    @_item.delete
    @hash_value = nil; @_item = nil; @is_new = true
  end
end

#find(hash_value, range_value = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rdy.rb', line 68

def find(hash_value, range_value = nil)
  raise "missing hash value" if hash_value.nil?
  if @range_key and range_value
    @_item = @_table.items.at(hash_value, range_value)
  else
    @_item = @_table.items[hash_value]
  end
  @attributes.clear
  if @_item and @_item.attributes and @_item.attributes.any?
    self.build(@_item.attributes.to_h)
    @hash_value = hash_value; @is_new = false
    @range_value = range_value if range_value
  else
    @hash_value = nil
  end
  @attributes
end

#hash_keyObject



37
# File 'lib/rdy.rb', line 37

def hash_key; @hash_key; end

#hash_valueObject



36
# File 'lib/rdy.rb', line 36

def hash_value; @hash_value; end

#is_new?Boolean

Returns:

  • (Boolean)


87
# File 'lib/rdy.rb', line 87

def is_new?; @is_new; end

#query(options = {}) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/rdy.rb', line 122

def query(options = {})
  if options and options.any?
    values = []
    @_table.items.query(options).each do |item|
      values << item.attributes.to_h
    end
    values
  end
end

#query_by_range_value(value) ⇒ Object



131
# File 'lib/rdy.rb', line 131

def query_by_range_value(value); query(:hash_value => self.hash_value.to_s, :range_value => value); end

#range_keyObject



38
# File 'lib/rdy.rb', line 38

def range_key; @range_key; end

#range_valueObject



39
# File 'lib/rdy.rb', line 39

def range_value; @range_value; end

#save(hash_value = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rdy.rb', line 88

def save(hash_value = nil)
  hash_value = Rdy.generate_key if hash_value.nil? and is_new?
  if is_new?
    if @range_key
      values = { @hash_key.to_sym => hash_value, @range_key.to_sym => @attributes[@range_key] }
    else
      values = { @hash_key.to_sym => hash_value }
    end
    options = {}
    options[:unless_exists] = @hash_key if hash_key_conditional_check
    @_item = @_table.items.create(values, options)
  end
  if @_item
    if @range_key
      attrs = @attributes; attrs.delete(@range_key)
      @_item.attributes.set(attrs)
    else
      @_item.attributes.set(@attributes)
    end
    @hash_value = hash_value if is_new?
    @is_new = false
    @_item.attributes.to_h
  end
end

#scan(attrs, limit = nil) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/rdy.rb', line 113

def scan(attrs, limit = nil)
  values = []; options = {}
  options[:limit] = limit if limit
  @_table.items.where(attrs).each(options) do |item|
    values << item.attributes.to_h
  end
  values
end

#tableObject



33
# File 'lib/rdy.rb', line 33

def table; @table; end

#table=(value) ⇒ Object



32
# File 'lib/rdy.rb', line 32

def table=(value); @table = value.to_s; end

#table_exists?Boolean

Returns:

  • (Boolean)


34
# File 'lib/rdy.rb', line 34

def table_exists?; @_table.exists?; end