Module: Mara::Model::Dsl::ClassMethods

Defined in:
lib/mara/model/dsl.rb

Overview

Helper method added at the class level.

Author:

  • Maddie Schipper

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#add_gsi(name, partition_key, sort_key = nil) ⇒ void

Note:

This is only required for querying.

This method returns an undefined value.

Add a global secondary index definition.

Parameters:

  • name (#to_s)

    The name of the index.

  • partition_key (#to_s)

    The name of the GSI partition key.

  • sort_key (String, nil) (defaults to: nil)

    The name of the GSI sort key.

Since:

  • 1.0.0



175
176
177
# 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) ⇒ void

Note:

This is only required for querying.

This method returns an undefined value.

Add a local secondary index definition.

Parameters:

  • name (String)

    The name of the index.

  • key_name (String)

    The name of the LSI sort key.

Since:

  • 1.0.0



132
133
134
# 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) ⇒ GlobalSecondaryIndex

Get a defined global secondary index by name.

Parameters:

  • name (#to_s)

    The name of the GSI to get.

Returns:

Raises:

  • (IndexError)

    The index is not registered on the model.

Since:

  • 1.0.0



197
198
199
200
201
202
203
204
# 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_indicesHash<String, GlobalSecondaryIndex>

All registered global secondary indices

Returns:

Since:

  • 1.0.0



185
186
187
# File 'lib/mara/model/dsl.rb', line 185

def global_secondary_indices
  @global_secondary_indices ||= {}
end

#local_secondary_index(name) ⇒ LocalSecondaryIndex

Get a defined local secondary index by name.

Parameters:

  • name (#to_s)

    The name of the LSI to get.

Returns:

Raises:

  • (IndexError)

    The index is not registered on the model.

Since:

  • 1.0.0



154
155
156
157
158
159
160
161
# 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_indicesHash<String, LocalSecondaryIndex>

All registered local secondary indices

Returns:

Since:

  • 1.0.0



142
143
144
# File 'lib/mara/model/dsl.rb', line 142

def local_secondary_indices
  @local_secondary_indices ||= {}
end

#partition_key(partition_key = nil) ⇒ String

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

Parameters:

  • partition_key (#to_s) (defaults to: nil)

    The name of the partion key.

Returns:

  • (String)

Since:

  • 1.0.0



99
100
101
102
103
104
105
# 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) ⇒ void

This method returns an undefined value.

Set a partion_key and sort_key for a model.

Examples:

Setting the partition_key & sort_key

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

Parameters:

  • partition_key (#to_s)

    The name of the DynamoDB table’s partion key.

  • sort_key (#to_s, nil) (defaults to: nil)

    The name of the DynamoDB table’s sort key.

See Also:

Since:

  • 1.0.0



88
89
90
91
# 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) ⇒ String

Set the sort key name for the model.

Parameters:

  • sort_key (#to_s) (defaults to: nil)

    The name of the sort key.

Returns:

  • (String)

Since:

  • 1.0.0



113
114
115
116
117
118
119
120
# 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