module Mara::Model::Dsl::ClassMethods

Helper method added at the class level.

@author Maddie Schipper @since 1.0.0

Public Instance Methods

add_gsi(name, partition_key, sort_key = nil) click to toggle source

Add a global secondary index definition.

@note This is only required for querying.

@param name [#to_s] The name of the index.

@param partition_key [#to_s] The name of the GSI partition key.

@param sort_key [String, nil] The name of the GSI sort key.

@return [void]

# File lib/mara/model/dsl.rb, line 175
def add_gsi(name, partition_key, sort_key = nil)
  global_secondary_indices[name.to_s] = GlobalSecondaryIndex.new(name.to_s, partition_key.to_s, sort_key)
end
add_lsi(name, key_name) click to toggle source

Add a local secondary index definition.

@note This is only required for querying.

@param name [String] The name of the index.

@param key_name [String] The name of the LSI sort key.

@return [void]

# File lib/mara/model/dsl.rb, line 132
def add_lsi(name, key_name)
  local_secondary_indices[name.to_s] = LocalSecondaryIndex.new(name.to_s, key_name.to_s)
end
global_secondary_index(name) click to toggle source

Get a defined global secondary index by name.

@param name [#to_s] The name of the GSI to get.

@raise [IndexError] The index is not registered on the model.

@return [GlobalSecondaryIndex]

# File lib/mara/model/dsl.rb, line 197
def global_secondary_index(name)
  index = global_secondary_indices[name.to_s]
  if index.nil?
    raise  Mara::Model::IndexError, "Can't find a GSI with the name `#{name}`"
  end

  index
end
global_secondary_indices() click to toggle source

@private

All registered global secondary indices

@return [Hash<String, GlobalSecondaryIndex>]

# File lib/mara/model/dsl.rb, line 185
def global_secondary_indices
  @global_secondary_indices ||= {}
end
local_secondary_index(name) click to toggle source

Get a defined local secondary index by name.

@param name [#to_s] The name of the LSI to get.

@raise [IndexError] The index is not registered on the model.

@return [LocalSecondaryIndex]

# File lib/mara/model/dsl.rb, line 154
def local_secondary_index(name)
  index = local_secondary_indices[name.to_s]
  if index.nil?
    raise  Mara::Model::IndexError, "Can't find a LSI with the name `#{name}`"
  end

  index
end
local_secondary_indices() click to toggle source

@private

All registered local secondary indices

@return [Hash<String, LocalSecondaryIndex>]

# File lib/mara/model/dsl.rb, line 142
def local_secondary_indices
  @local_secondary_indices ||= {}
end
partition_key(partition_key = nil) click to toggle source

Set the partion key name for the model. This value is required.

@param partition_key [#to_s] The name of the partion key.

@return [String]

# File lib/mara/model/dsl.rb, line 99
def partition_key(partition_key = nil)
  unless partition_key.nil?
    @partition_key = partition_key.to_s
    validates_presence_of :partition_key
  end
  @partition_key
end
primary_key(partition_key, sort_key = nil) click to toggle source

Set a partion_key and sort_key for a model.

@see partition_key

@see sort_key

@example Setting the partition_key & sort_key

class Person <  Mara::Model::Base
  primary_key('PartionKeyName', 'SortKeyName')
  # ...

@param partition_key [#to_s] The name of the DynamoDB table's partion

key.

@param sort_key [#to_s, nil] The name of the DynamoDB table's sort

key.

@return [void]

# File lib/mara/model/dsl.rb, line 88
def primary_key(partition_key, sort_key = nil)
  partition_key(partition_key)
  sort_key(sort_key)
end
sort_key(sort_key = nil) click to toggle source

Set the sort key name for the model.

@param sort_key [#to_s] The name of the sort key.

@return [String]

# File lib/mara/model/dsl.rb, line 113
def sort_key(sort_key = nil)
  unless sort_key.nil?
    @sort_key = sort_key.to_s
    validates_presence_of :sort_key
  end

  @sort_key
end