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

attributes[R]

@private

The attributes container.

@return [ Mara::Model::Attributes]

partition_key[RW]

The partition_key key value for the object.

@return [Any, nil]

Public Class Methods

build(attributes = {}) click to toggle source

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
construct(record_hash) click to toggle source

@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
new(partition_key:, sort_key:, attributes:, persisted:) click to toggle source

@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

[](key) click to toggle source

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
[]=(key, value) click to toggle source

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
conditional_sort_key() click to toggle source

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
method_missing(name, *args, &block) click to toggle source

@private

Attribute Magic

Calls superclass method
# 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
model_identifier() click to toggle source
# File lib/mara/model/base.rb, line 157
def model_identifier
   Mara::PrimaryKey.generate(model_primary_key)
end
model_primary_key() click to toggle source
# 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) click to toggle source

@private

Attribute Magic

Calls superclass method
# 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() click to toggle source

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
sort_key=(sort_key) click to toggle source

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