class Mara::Model::Base
The base class for a Mara
Model
@example A basic Person class.
class Person < Mara::Model::Base # Set the Partition Key & Sort Key names. primary_key 'PrimaryKey', 'RangeKey' end
@example Set dynamic attribute values.
person = Person.build person[:first_name] = 'Maddie' person.last_name = 'Schipper'
@author Maddie Schipper @since 1.0.0
Attributes
@private
The attributes container.
@return [ Mara::Model::Attributes]
The partition_key
key value for the object.
@return [Any, nil]
Public Class Methods
Create a new instance of the model.
@example Building a new model.
person = Person.build( partition_key: SecureRandom.uuid, first_name: 'Maddie', last_name: 'Schipper' )
@param attributes [Hash] The default attributes that can be assigned.
If a +partition_key+ is specified it will be used to set the model's +partion_key+ If a +sort_key+ is specified it will be used to set the model's +sort_key+
@return [ Mara::Model::Base]
# 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
@private
# 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
@private
Create a new instance of the model.
@param partition_key
[Any] The partition_key
for the model. @param sort_key
[Any] The sort key for the model. @param attributes [ Mara::Model::Attributes] The already existing
attributes for the model.
# 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
Public Instance Methods
Get an attribute's current value.
@param key [#to_s] The key for the attribute.
@return [Any, nil]
# File lib/mara/model/base.rb, line 149 def [](key) attributes.get(key) end
Set an attribute key value pair.
@param key [#to_s] The key for the attribute. @param value [Any, nil] The value of the attribute.
@return [void]
# File lib/mara/model/base.rb, line 139 def []=(key, value) attributes.set(key, value) end
Checks if the model should have a sort key and returns the value if it does.
@return [Any, nil]
# File lib/mara/model/base.rb, line 179 def conditional_sort_key return nil if self.class.sort_key.blank? sort_key end
@private
Attribute Magic
# 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
# File lib/mara/model/base.rb, line 157 def model_identifier Mara::PrimaryKey.generate(model_primary_key) end
# File lib/mara/model/base.rb, line 153 def model_primary_key Mara::PrimaryKey.new(model: self) end
@private
Attribute Magic
# 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
Fetch the current sort key value.
@return [Any, nil]
# 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
Set a sort key value.
@param sort_key
[String] The sort key value.
@return [void]
# 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