Class: ActiveDynamo::Base
- Inherits:
-
Object
- Object
- ActiveDynamo::Base
- Includes:
- Persistence, Query
- Defined in:
- lib/active_dynamo/base.rb
Class Method Summary collapse
Instance Method Summary collapse
Methods included from Persistence
Methods included from Query
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( = {}) @@table_name = .fetch(:name, snake_name) @@partition_key = .fetch(:partition_key, nil) @@sort_key = .fetch(:sort_key, nil) @@primary_key = [@@partition_key, @@sort_key].compact @@db_conn = .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
#attributes ⇒ Object
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_attributes ⇒ Object
53 54 55 |
# File 'lib/active_dynamo/base.rb', line 53 def primary_key_attributes self.attributes.slice(*@@primary_key) end |