Class: Dynamini::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, 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



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

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)



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/dynamini/base.rb', line 178

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_key ⇒ Object (readonly)

Returns the value of attribute range_key.



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

def range_key
  @range_key
end

Instance Attribute Details

#attributes ⇒ Object (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

Class Method Details

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



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

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

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



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

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

.hash_key ⇒ Object



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

def hash_key
  @hash_key || :id
end

.set_hash_key(key) ⇒ Object



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

def set_hash_key(key)
  @hash_key = key
end

.set_range_key(key) ⇒ Object



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

def set_range_key(key)
  @range_key = key
end

.set_table_name(name) ⇒ Object



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

def set_table_name(name)
  @table_name = name
end

.table_name ⇒ Object



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

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

Instance Method Details

#==(other) ⇒ Object



81
82
83
# File 'lib/dynamini/base.rb', line 81

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

#assign_attributes(attributes) ⇒ Object



85
86
87
88
89
90
# File 'lib/dynamini/base.rb', line 85

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

#delete ⇒ Object



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

def delete
  delete_from_dynamo
  self
end

#keys ⇒ Object



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

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

#save(options = {}) ⇒ Object



102
103
104
105
106
# File 'lib/dynamini/base.rb', line 102

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

#save!(options = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dynamini/base.rb', line 108

def save!(options = {})
  run_callbacks :save do

    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
end

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

Raises:

  • (RuntimeError)


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

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



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

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

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



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

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