Class: Mara::Model::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Dsl, Persistence, Query
Defined in:
lib/mara/model/base.rb

Overview

The base class for a Mara Model

Examples:

A basic Person class.

class Person <  Mara::Model::Base
  # Set the Partition Key & Sort Key names.
  primary_key 'PrimaryKey', 'RangeKey'
end

Set dynamic attribute values.

person = Person.build
person[:first_name] = 'Maddie'
person.last_name = 'Schipper'

Author:

  • Maddie Schipper

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

#destroy, #destroy!, included, #primary_key, #save, #save!, #to_dynamo, #to_item

Methods included from Query

#exist?, included

Methods included from Dsl

included

Constructor Details

#initialize(partition_key:, sort_key:, attributes:, persisted:) ⇒ Base

Create a new instance of the model.

Parameters:

  • partition_key (Any)

    The partition_key for the model.

  • sort_key (Any)

    The sort key for the model.

  • attributes (Mara::Model::Attributes)

    The already existing attributes for the model.

Since:

  • 1.0.0



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

Since:

  • 1.0.0



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

#attributesMara::Model::Attributes (readonly)

The attributes container.

Returns:

Since:

  • 1.0.0



47
48
49
# File 'lib/mara/model/base.rb', line 47

def attributes
  @attributes
end

#partition_keyAny?

The partition_key key value for the object.

Returns:

  • (Any, nil)

Since:

  • 1.0.0



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.

Examples:

Building a new model.

person = Person.build(
  partition_key: SecureRandom.uuid,
  first_name: 'Maddie',
  last_name: 'Schipper'
)

Parameters:

  • attributes (Hash) (defaults to: {})

    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

Returns:

Since:

  • 1.0.0



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

Since:

  • 1.0.0



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.

Parameters:

  • key (#to_s)

    The key for the attribute.

Returns:

  • (Any, nil)

Since:

  • 1.0.0



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.

Parameters:

  • key (#to_s)

    The key for the attribute.

  • value (Any, nil)

    The value of the attribute.

Since:

  • 1.0.0



139
140
141
# File 'lib/mara/model/base.rb', line 139

def []=(key, value)
  attributes.set(key, value)
end

#conditional_sort_keyAny?

Checks if the model should have a sort key and returns the value if it does.

Returns:

  • (Any, nil)

Since:

  • 1.0.0



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_identifierObject

Since:

  • 1.0.0



157
158
159
# File 'lib/mara/model/base.rb', line 157

def model_identifier
   Mara::PrimaryKey.generate(model_primary_key)
end

#model_primary_keyObject

Since:

  • 1.0.0



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

Returns:

  • (Boolean)

Since:

  • 1.0.0



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_keyAny?

Fetch the current sort key value.

Returns:

  • (Any, nil)

Since:

  • 1.0.0



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.

Parameters:

  • sort_key (String)

    The sort key value.

Since:

  • 1.0.0



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