Class: Mara::Model::Base
- Inherits:
-
Object
- Object
- Mara::Model::Base
- Includes:
- ActiveModel::Validations, Dsl, Persistence, Query
- Defined in:
- lib/mara/model/base.rb
Overview
The base class for a Mara Model
Instance Attribute Summary collapse
-
#attributes ⇒ Mara::Model::Attributes
readonly
The attributes container.
-
#partition_key ⇒ Any?
The partition_key key value for the object.
Class Method Summary collapse
-
.build(attributes = {}) ⇒ Mara::Model::Base
Create a new instance of the model.
- .construct(record_hash) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Any?
Get an attribute’s current value.
-
#[]=(key, value) ⇒ void
Set an attribute key value pair.
-
#conditional_sort_key ⇒ Any?
Checks if the model should have a sort key and returns the value if it does.
-
#initialize(partition_key:, sort_key:, attributes:, persisted:) ⇒ Base
constructor
Create a new instance of the model.
-
#method_missing(name, *args, &block) ⇒ Object
Attribute Magic.
- #model_identifier ⇒ Object
- #model_primary_key ⇒ Object
-
#respond_to_missing?(name, include_private = false) ⇒ Boolean
Attribute Magic.
-
#sort_key ⇒ Any?
Fetch the current sort key value.
-
#sort_key=(sort_key) ⇒ void
Set a sort key value.
Methods included from Persistence
#destroy, #destroy!, included, #primary_key, #save, #save!, #to_dynamo, #to_item
Methods included from Query
Methods included from Dsl
Constructor Details
#initialize(partition_key:, sort_key:, attributes:, persisted:) ⇒ Base
Create a new instance of the model.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mara/model/base.rb', line 109 def initialize(partition_key:, sort_key:, attributes:, persisted:) if self.class.partition_key.blank? raise Mara::Model::PrimaryKeyError, "Can't create instance of #{self.class.name} without a `partition_key` set." end unless attributes.is_a?( Mara::Model::Attributes) raise ArgumentError, 'attributes is not Mara::Model::Attributes' end @persisted = persisted == true @attributes = attributes self.partition_key = partition_key self.sort_key = sort_key if sort_key end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Attribute Magic
204 205 206 207 208 209 210 |
# File 'lib/mara/model/base.rb', line 204 def method_missing(name, *args, &block) if attributes.respond_to?(name) attributes.send(name, *args, &block) else super end end |
Instance Attribute Details
#attributes ⇒ Mara::Model::Attributes (readonly)
The attributes container.
47 48 49 |
# File 'lib/mara/model/base.rb', line 47 def attributes @attributes end |
#partition_key ⇒ Any?
The partition_key key value for the object.
130 131 132 |
# File 'lib/mara/model/base.rb', line 130 def partition_key @partition_key end |
Class Method Details
.build(attributes = {}) ⇒ Mara::Model::Base
Create a new instance of the model.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mara/model/base.rb', line 69 def build(attributes = {}) partition_key = attributes.delete(:partition_key) sort_key = attributes.delete(:sort_key) attrs = Mara::Model::Attributes.new(attributes) new( partition_key: partition_key, sort_key: sort_key, attributes: attrs, persisted: false ) end |
.construct(record_hash) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mara/model/base.rb', line 85 def construct(record_hash) partition_key = record_hash.delete(self.partition_key) sort_key = record_hash.delete(self.sort_key) attrs = Mara::Model::Attributes.new(record_hash) new( partition_key: partition_key, sort_key: sort_key, attributes: attrs, persisted: true ) end |
Instance Method Details
#[](key) ⇒ Any?
Get an attribute’s current value.
149 150 151 |
# File 'lib/mara/model/base.rb', line 149 def [](key) attributes.get(key) end |
#[]=(key, value) ⇒ void
This method returns an undefined value.
Set an attribute key value pair.
139 140 141 |
# File 'lib/mara/model/base.rb', line 139 def []=(key, value) attributes.set(key, value) end |
#conditional_sort_key ⇒ Any?
Checks if the model should have a sort key and returns the value if it does.
179 180 181 182 183 |
# File 'lib/mara/model/base.rb', line 179 def conditional_sort_key return nil if self.class.sort_key.blank? sort_key end |
#model_identifier ⇒ Object
157 158 159 |
# File 'lib/mara/model/base.rb', line 157 def model_identifier Mara::PrimaryKey.generate(model_primary_key) end |
#model_primary_key ⇒ Object
153 154 155 |
# File 'lib/mara/model/base.rb', line 153 def model_primary_key Mara::PrimaryKey.new(model: self) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
Attribute Magic
216 217 218 219 220 221 222 |
# File 'lib/mara/model/base.rb', line 216 def respond_to_missing?(name, include_private = false) if attributes.respond_to?(name) true else super end end |
#sort_key ⇒ Any?
Fetch the current sort key value.
165 166 167 168 169 170 171 172 |
# File 'lib/mara/model/base.rb', line 165 def sort_key if self.class.sort_key.blank? raise Mara::Model::PrimaryKeyError, "Model #{self.class.name} does not specify a sort_key." end @sort_key end |
#sort_key=(sort_key) ⇒ void
This method returns an undefined value.
Set a sort key value.
191 192 193 194 195 196 197 198 |
# File 'lib/mara/model/base.rb', line 191 def sort_key=(sort_key) if self.class.sort_key.blank? raise Mara::Model::PrimaryKeyError, "Model #{self.class.name} does not specify a sort_key." end @sort_key = sort_key end |