Class: ActiveDynamo::Base

Inherits:
Object
  • Object
show all
Includes:
Persistence, Query
Defined in:
lib/active_dynamo/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

included, #save, #update

Methods included from Query

included

Class Method Details

.attributes(**attrs) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_dynamo/base.rb', line 24

def attributes(**attrs)
  @@attr_types = attrs
  @@attrs      = attrs.keys
  attr_reader(*@@attrs)

  define_method(:initialize) do |**args|
    @@attrs.each do |name|
      _type_parser = method(@@attr_types.fetch(name).to_s)
      _value       = _type_parser.call(args[name])
      instance_variable_set("@#{name}", _value)
    end
  end

  define_singleton_method('attrs') do
    class_variable_get('@@attrs')
  end

  define_singleton_method('attr_types') do
    class_variable_get('@@attr_types')
  end
end

.table(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_dynamo/base.rb', line 10

def table(options = {})
  @@table_name    = options.fetch(:name, snake_name)
  @@partition_key = options.fetch(:partition_key, nil)
  @@sort_key      = options.fetch(:sort_key, nil)
  @@primary_key   = [@@partition_key, @@sort_key].compact
  @@db_conn       = options.fetch(:db_conn, Aws::DynamoDB::Client.new)

  class_variables.each do |var|
    define_singleton_method(var.to_s.delete('@')) do
      class_variable_get(var)
    end
  end
end

Instance Method Details

#attributesObject



47
48
49
50
51
# File 'lib/active_dynamo/base.rb', line 47

def attributes
  @@attrs.inject({}) do |h, attr|
    h.update(attr => self.instance_variable_get("@#{attr}"))
  end
end

#primary_key_attributesObject



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

def primary_key_attributes
  self.attributes.slice(*@@primary_key)
end