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 },
    boolean: Proc.new   { |v| ['true', '1', '1.0'].include? 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



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

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)



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

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



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

def batch_write_queue
  @batch_write_queue ||= []
end

.in_memoryObject



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

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)


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

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



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

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



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

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

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



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

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

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



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/dynamini/base.rb', line 284

def self.define_handled_getter(column, format_class, options={})
  proc = TRANSLATION_PROCS[format_class]
  raise 'Unsupported data type: ' + format_class.to_s if proc.nil?
  define_method(column) do
    if @attributes.has_key?(column)
      proc.call(read_attribute(column))
    else
      options[:default] || nil
    end
  end
end

.define_handled_setter(column) ⇒ Object



296
297
298
299
300
301
302
# File 'lib/dynamini/base.rb', line 296

def self.define_handled_setter(column)
  setter_symbol = (column.to_s + '=').to_sym

  define_method(setter_symbol) do |value|
    write_attribute(column, value)
  end
end

.dynamo_batch_get(key_structure) ⇒ Object



234
235
236
# File 'lib/dynamini/base.rb', line 234

def self.dynamo_batch_get(key_structure)
  client.batch_get_item(request_items: {table_name => {keys: key_structure}})
end

.dynamo_batch_save(model_array) ⇒ Object



238
239
240
241
242
243
244
245
246
247
# File 'lib/dynamini/base.rb', line 238

def self.dynamo_batch_save(model_array)
  put_requests = []
  model_array.each do |model|
    put_requests << {put_request: {item: model.attributes.reject{|k, v| v.blank?}.stringify_keys}}
  end
  request_options = {request_items: {
      "#{table_name}" => put_requests}
  }
  client.batch_write_item(request_options)
end

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



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

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



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

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

.find(key) ⇒ Object



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

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



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

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

.flush_queue!Object



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

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

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



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

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

.hash_keyObject



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

def hash_key
  @hash_key || :id
end

.set_hash_key(key) ⇒ Object



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

def set_hash_key(key)
  @hash_key = key
end

.set_table_name(name) ⇒ Object



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

def set_table_name(name)
  @table_name = name
end

.table_nameObject



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

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

Instance Method Details

#==(object) ⇒ Object



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

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

#assign_attributes(attributes) ⇒ Object



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

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

#changedObject



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

def changed
  @changed.to_a
end

#changesObject



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

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

#deleteObject



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

def delete
  delete_from_dynamo
  self
end

#new_record?Boolean



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

def new_record?
  @new_record
end

#save(options = {}) ⇒ Object



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

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

#save!(options = {}) ⇒ Object



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

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)


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

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



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

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

#update_attributes(attributes) ⇒ Object



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

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