Class: Dynamini::Base

Inherits:
Object
  • Object
show all
Extended by:
BatchOperations, Querying
Includes:
ActiveModel::Validations, ClientInterface, Dirty, Increment, TypeHandler
Defined in:
lib/dynamini/base.rb

Overview

Core db interface class.

Constant Summary

Constants included from TypeHandler

TypeHandler::GETTER_PROCS, TypeHandler::SETTER_PROCS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BatchOperations

batch_find, dynamo_batch_save, import

Methods included from Querying

exists?, find, find_or_new, query

Methods included from TypeHandler

#handles, included

Methods included from Increment

#increment!

Methods included from Dirty

#changed, #changes, #new_record?

Methods included from ClientInterface

#delete_from_dynamo, included, #increment_to_dynamo, #save_to_dynamo, #touch_to_dynamo

Constructor Details

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

Instance Methods



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

def initialize(attributes = {}, new_record = true)
  @new_record = new_record
  @attributes = {}
  clear_changes
  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)



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

def method_missing(name, *args, &block)
  if write_method?(name)
    write_attribute(attribute_name(name), args.first)
  elsif was_method?(name)
    __was(name)
  elsif read_method?(name)
    read_attribute(name)
  else
    super
  end
end

Class Attribute Details

.range_keyObject (readonly)

Returns the value of attribute range_key.



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

def range_key
  @range_key
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

Class Method Details

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



52
53
54
55
# File 'lib/dynamini/base.rb', line 52

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

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



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

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

.hash_keyObject



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

def hash_key
  @hash_key || :id
end

.set_hash_key(key) ⇒ Object



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

def set_hash_key(key)
  @hash_key = key
end

.set_range_key(key) ⇒ Object



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

def set_range_key(key)
  @range_key = key
end

.set_table_name(name) ⇒ Object



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

def set_table_name(name)
  @table_name = name
end

.table_nameObject



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

def table_name
  @table_name ||= name.demodulize.tableize
end

Instance Method Details

#==(other) ⇒ Object



78
79
80
# File 'lib/dynamini/base.rb', line 78

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

#assign_attributes(attributes) ⇒ Object



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

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

#deleteObject



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

def delete
  delete_from_dynamo
  self
end

#keysObject



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

def keys
  [self.class.hash_key, self.class.range_key]
end

#save(options = {}) ⇒ Object



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

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

#save!(options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/dynamini/base.rb', line 103

def save!(options = {})
  options[:validate] = true if options[:validate].nil?

  unless @changes.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)


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

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, options = {}) ⇒ Object



89
90
91
92
# File 'lib/dynamini/base.rb', line 89

def update_attribute(key, value, options = {})
  write_attribute(key, value)
  save!(options)
end

#update_attributes(attributes, options = {}) ⇒ Object



94
95
96
97
# File 'lib/dynamini/base.rb', line 94

def update_attributes(attributes, options = {})
  assign_attributes(attributes)
  save!(options)
end