Class: Dynamini::Base

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

Overview

Core db interface class.

Constant Summary

Constants included from Querying

Querying::OPTIONAL_QUERY_PARAMS

Constants included from TypeHandler

TypeHandler::GETTER_PROCS, TypeHandler::SETTER_PROCS

Constants included from Attributes

Attributes::ADDABLE_TYPES, Attributes::DELETED_TOKEN

Class Attribute Summary collapse

Attributes included from Attributes

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BatchOperations

batch_delete, batch_find, import, scan

Methods included from Querying

exists?, find, find_or_new, find_or_nil, query

Methods included from TypeHandler

attribute_callback, convert_elements, define_handled_getter, define_handled_setter, format_default, handle, handled_as?, handled_key, invalid_enumerable_value?, should_convert_elements?, validate_handle

Methods included from Attributes

#add_to, #assign_attributes, #delete_attribute, #delete_attribute!, #handled_attributes, #inspect, #update_attribute, #update_attributes

Methods included from Increment

#increment!

Methods included from Dirty

#assign_transient_attribute, #changed, #changes, #mark, #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



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

def initialize(attributes = {}, new_record = true)
  @new_record = new_record
  @attributes = {}
  clear_changes
  attributes.each do |k, v|
    write_attribute(k, v, change: new_record)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dynamini::Attributes

Class Attribute Details

.range_keyObject (readonly)

Returns the value of attribute range_key.



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

def range_key
  @range_key
end

.secondary_indexObject (readonly)

Returns the value of attribute secondary_index.



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

def secondary_index
  @secondary_index
end

Class Method Details

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



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

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

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



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

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

.hash_keyObject



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

def hash_key
  @hash_key || :id
end

.set_hash_key(key, format = nil) ⇒ Object



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

def set_hash_key(key, format = nil)
  @hash_key = key
  handle(key, format) if format
end

.set_range_key(key, format = nil) ⇒ Object



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

def set_range_key(key, format = nil)
  @range_key = key
  handle(key, format) if format
end

.set_secondary_index(index_name, args = {}) ⇒ Object



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

def set_secondary_index(index_name, args = {})
  @secondary_index ||= {}
  @secondary_index[index_name.to_s] = {hash_key_name: args[:hash_key] || hash_key, range_key_name: args[:range_key]}
end

.set_table_name(name) ⇒ Object



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

def set_table_name(name)
  @table_name = name
end

.table_nameObject



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

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

Instance Method Details

#==(other) ⇒ Object



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

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

#deleteObject



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

def delete
  delete_from_dynamo
  self
end

#keysObject



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

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

#save(options = {}) ⇒ Object



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

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

#save!(options = {}) ⇒ Object



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

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)


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