Class: Mara::PrimaryKey

Inherits:
Object
  • Object
show all
Defined in:
lib/mara/primary_key.rb

Overview

Wraps a primary key.

Author:

  • Maddie Schipper

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ PrimaryKey

Note:

If :model is not present the other three options are required.

Create a new primary key.

Parameters:

  • opts (Hash)

    The options param

Options Hash (opts):

  • :model (Mara::Model::Base)

    The model this key will represent.

  • :class_name (String)

    The class name of the model.

  • :partition_key (String)

    The partition key value.

  • :sort_key (String)

    The sort key value.

Since:

  • 1.0.0



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mara/primary_key.rb', line 82

def initialize(opts)
  if (model = opts.delete(:model)).present?
    @class_name = model.class.name
    @partition_key = if model.class.partition_key.present?
                       model.partition_key
                     end
    @sort_key = if model.class.sort_key.present?
                  model.sort_key
                end
  else
    @class_name = opts.fetch(:class_name).camelize
    @partition_key = opts.fetch(:partition_key)
    @sort_key = opts.fetch(:sort_key, nil).presence
  end
end

Instance Attribute Details

#class_nameString (readonly)

The classname of the model that the primary key represents.

Returns:

  • (String)

Since:

  • 1.0.0



58
59
60
# File 'lib/mara/primary_key.rb', line 58

def class_name
  @class_name
end

#partition_keyString (readonly)

The partion key

Returns:

  • (String)

Since:

  • 1.0.0



64
65
66
# File 'lib/mara/primary_key.rb', line 64

def partition_key
  @partition_key
end

#sort_keyString (readonly)

The sort key

Returns:

  • (String)

Since:

  • 1.0.0



70
71
72
# File 'lib/mara/primary_key.rb', line 70

def sort_key
  @sort_key
end

Class Method Details

.generate(model) ⇒ String

Create a primary key from a model.

Parameters:

Returns:

  • (String)

Since:

  • 1.0.0



19
20
21
22
23
24
25
26
27
28
# File 'lib/mara/primary_key.rb', line 19

def generate(model)
  case model
  when  Mara::PrimaryKey
    model.to_s
  when  Mara::Model::Base
    new(model: model).to_s
  else
    raise ArgumentError, "The value passed into generate isn't expected <#{model}>"
  end
end

.parse(key_str) ⇒ Mara::PrimaryKey

Parse a primary key string.

Parameters:

  • key_str (String)

    The primary key string to return.

Returns:

Since:

  • 1.0.0



36
37
38
39
40
41
42
43
# File 'lib/mara/primary_key.rb', line 36

def parse(key_str)
  parts = JSON.parse(decode(key_str))
  new(
    class_name: parts[0],
    partition_key: parts[1],
    sort_key: parts[2]
  )
end

Instance Method Details

#to_sString

Convert the primary key into a URL safe representation.

Returns:

  • (String)

Since:

  • 1.0.0



102
103
104
105
106
107
108
109
# File 'lib/mara/primary_key.rb', line 102

def to_s
  payload = JSON.dump([
                        (class_name.presence || '').underscore,
                        partition_key.presence || '',
                        sort_key.presence || ''
                      ])
  encode(payload)
end