Class: Dynamini::Base

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

Constant Summary collapse

BATCH_SIZE =
25

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



99
100
101
102
103
104
# File 'lib/dynamini/base.rb', line 99

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/dynamini/base.rb', line 233

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_queue ⇒ Object



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

def batch_write_queue
  @batch_write_queue ||= []
end

.hash_key ⇒ Object



15
16
17
# File 'lib/dynamini/base.rb', line 15

def hash_key
  @hash_key || :id
end

.in_memory ⇒ Object



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

def in_memory
  @in_memory || false
end

.table_name ⇒ Object



11
12
13
# File 'lib/dynamini/base.rb', line 11

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

Instance Attribute Details

#attributes ⇒ Object (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)


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dynamini/base.rb', line 68

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

.client ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/dynamini/base.rb', line 27

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



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

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

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



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

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

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



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

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)


54
55
56
57
# File 'lib/dynamini/base.rb', line 54

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

.find(key) ⇒ Object



48
49
50
51
52
# File 'lib/dynamini/base.rb', line 48

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



59
60
61
62
63
64
65
66
# File 'lib/dynamini/base.rb', line 59

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



91
92
93
94
95
# File 'lib/dynamini/base.rb', line 91

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

Instance Method Details

#==(object) ⇒ Object



106
107
108
# File 'lib/dynamini/base.rb', line 106

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

#assign_attributes(attributes) ⇒ Object



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

def assign_attributes(attributes)
  attributes.each do |key, value|
    record_change(key, read_attribute(key), value)
  end
  @attributes.merge!(attributes)
  nil
end

#changed ⇒ Object



162
163
164
# File 'lib/dynamini/base.rb', line 162

def changed
  @changed.to_a
end

#changes ⇒ Object



158
159
160
# File 'lib/dynamini/base.rb', line 158

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

#new_record? ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/dynamini/base.rb', line 166

def new_record?
  @new_record
end

#save(options = {}) ⇒ Object



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

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

#save!(options = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dynamini/base.rb', line 136

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)


149
150
151
152
153
154
155
156
# File 'lib/dynamini/base.rb', line 149

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



118
119
120
121
122
# File 'lib/dynamini/base.rb', line 118

def update_attribute(key, value)
  record_change(key, value, @attributes[key])
  @attributes[key] = value
  save!
end

#update_attributes(attributes) ⇒ Object



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

def update_attributes(attributes)
  attributes.each do |key, value|
    record_change(key, read_attribute(key), value)
  end
  @attributes.merge!(attributes)
  save!
end