Class: Dynamini::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/dynamini/base.rb

Constant Summary collapse

BATCH_SIZE =
25
TRANSLATION_PROCS =
{
    integer: Proc.new {|v| v.to_i},
    datetime: Proc.new {|v| Time.at(v.to_f)},
    float: Proc.new {|v| v.to_f},
    symbol: Proc.new {|v| v.to_sym},
    string: Proc.new {|v| v}
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, new_record = true) ⇒ Base

Instance Methods



121
122
123
124
125
126
127
128
129
# File 'lib/dynamini/base.rb', line 121

def initialize(attributes={}, new_record = true)
  @changed = Set.new
  @new_record = new_record
  @attributes = {}

  attributes.each do |k, v|
    write_attribute(k, v, new_record)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/dynamini/base.rb', line 263

def method_missing(name, *args, &block)
  if write_method?(name)
    attribute = name[0..-2].to_sym
    new_value = args.first
    write_attribute(attribute, new_value)
  elsif read_method?(name)
    read_attribute(name)
  else
    super
  end
end

Class Attribute Details

.batch_write_queueObject



43
44
45
# File 'lib/dynamini/base.rb', line 43

def batch_write_queue
  @batch_write_queue ||= []
end

.in_memoryObject



39
40
41
# File 'lib/dynamini/base.rb', line 39

def in_memory
  @in_memory || false
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/dynamini/base.rb', line 4

def attributes
  @attributes
end

Class Method Details

.batch_find(ids = []) ⇒ Object

Raises:

  • (StandardError)


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dynamini/base.rb', line 88

def batch_find(ids = [])
  return [] if ids.length < 1
  objects = []
  raise StandardError, 'Batch find is limited to 100 items' if ids.length > 100
  key_structure = ids.map { |i| {hash_key => i.to_s} }
  response = self.dynamo_batch_get(key_structure)
  response.responses[table_name].each do |item|
    objects << self.new(item.symbolize_keys, false)
  end
  objects
end

.clientObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/dynamini/base.rb', line 47

def client
  if in_memory
    @client ||= Dynamini::TestClient.new(hash_key)
  else
    @client ||= Aws::DynamoDB::Client.new(
        region: Dynamini.configuration.region,
        access_key_id: Dynamini.configuration.access_key_id,
        secret_access_key: Dynamini.configuration.secret_access_key)
  end
end

.create(attributes, options = {}) ⇒ Object



58
59
60
61
# File 'lib/dynamini/base.rb', line 58

def create(attributes, options={})
  model = self.new(attributes, true)
  model if model.save(options)
end

.create!(attributes, options = {}) ⇒ Object



63
64
65
66
# File 'lib/dynamini/base.rb', line 63

def create!(attributes, options={})
  model = self.new(attributes, true)
  model if model.save!(options)
end

.enqueue_for_save(attributes, options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/dynamini/base.rb', line 100

def enqueue_for_save(attributes, options = {})
  model = self.new(attributes, true)
  model.generate_timestamps! unless options[:skip_timestamps]
  if model.valid?
    batch_write_queue << model
    flush_queue! if batch_write_queue.length == BATCH_SIZE
    return true
  end
  false
end

.exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/dynamini/base.rb', line 74

def exists?(key)
  response = client.get_item(table_name: table_name, key: {hash_key => key.to_s})
  response.item.present?
end

.find(key) ⇒ Object



68
69
70
71
72
# File 'lib/dynamini/base.rb', line 68

def find(key)
  response = client.get_item(table_name: table_name, key: {hash_key => key.to_s})
  raise 'Item not found.' unless response.item
  self.new(response.item.symbolize_keys, false)
end

.find_or_new(key) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/dynamini/base.rb', line 79

def find_or_new(key)
  response = client.get_item(table_name: table_name, key: {hash_key => key})
  if response.item
    self.new(response.item.symbolize_keys, false)
  else
    self.new(hash_key => key)
  end
end

.flush_queue!Object



111
112
113
114
115
# File 'lib/dynamini/base.rb', line 111

def flush_queue!
  response = self.dynamo_batch_save(batch_write_queue)
  self.batch_write_queue = []
  response
end

.handle(column, format_class, options = {}) ⇒ Object



30
31
32
33
# File 'lib/dynamini/base.rb', line 30

def handle(column, format_class, options={})
  define_handled_getter(column, format_class, options)
  define_handled_setter(column)
end

.hash_keyObject



35
36
37
# File 'lib/dynamini/base.rb', line 35

def hash_key
  @hash_key || :id
end

.set_hash_key(key) ⇒ Object



26
27
28
# File 'lib/dynamini/base.rb', line 26

def set_hash_key(key)
  @hash_key = key
end

.set_table_name(name) ⇒ Object



22
23
24
# File 'lib/dynamini/base.rb', line 22

def set_table_name(name)
  @table_name = name
end

.table_nameObject



18
19
20
# File 'lib/dynamini/base.rb', line 18

def table_name
  @table_name || name.demodulize.downcase.pluralize
end

Instance Method Details

#==(object) ⇒ Object



132
133
134
# File 'lib/dynamini/base.rb', line 132

def ==(object)
  hash_key == object.hash_key if object.is_a?(self.class)
end

#assign_attributes(attributes) ⇒ Object



136
137
138
139
140
141
# File 'lib/dynamini/base.rb', line 136

def assign_attributes(attributes)
  attributes.each do |key, value|
    write_attribute(key, value)
  end
  nil
end

#changedObject



188
189
190
# File 'lib/dynamini/base.rb', line 188

def changed
  @changed.to_a
end

#changesObject



184
185
186
# File 'lib/dynamini/base.rb', line 184

def changes
  @attributes.select { |attribute| @changed.include?(attribute.to_s) && attribute != self.class.hash_key }
end

#deleteObject



179
180
181
182
# File 'lib/dynamini/base.rb', line 179

def delete
  delete_from_dynamo
  self
end

#new_record?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/dynamini/base.rb', line 192

def new_record?
  @new_record
end

#save(options = {}) ⇒ Object



153
154
155
# File 'lib/dynamini/base.rb', line 153

def save(options = {})
  @changed.empty? || valid? && trigger_save(options)
end

#save!(options = {}) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/dynamini/base.rb', line 157

def save!(options = {})

  options[:validate]= true if options[:validate].nil?

  unless @changed.empty?
    if (options[:validate] && valid?) || !options[:validate]
      trigger_save(options)
    else
      raise StandardError, errors.full_messages
    end
  end
end

#touch(options = {validate: true}) ⇒ Object

Raises:

  • (RuntimeError)


170
171
172
173
174
175
176
177
# File 'lib/dynamini/base.rb', line 170

def touch(options = {validate: true})
  raise RuntimeError, 'Cannot touch a new record.' if new_record?
  if (options[:validate] && valid?) || !options[:validate]
    trigger_touch
  else
    raise StandardError, errors.full_messages
  end
end

#update_attribute(key, value) ⇒ Object



143
144
145
146
# File 'lib/dynamini/base.rb', line 143

def update_attribute(key, value)
  write_attribute(key, value)
  save!
end

#update_attributes(attributes) ⇒ Object



148
149
150
151
# File 'lib/dynamini/base.rb', line 148

def update_attributes(attributes)
  assign_attributes(attributes)
  save!
end