Class: Mara::Model::Attributes

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

Overview

The container class for attributes.

This should not be created directly. Instead use the Model convenience methods to have this setup automatically.

Author:

  • Maddie Schipper

Since:

  • 1.0.0

Instance Method Summary collapse

Constructor Details

#initialize(default) ⇒ Attributes

Create a new instance of attributes.

Parameters:

  • default (Hash)

    The default attribute values to set.

Since:

  • 1.0.0



27
28
29
30
# File 'lib/mara/model/attributes.rb', line 27

def initialize(default)
  @storage = {}
  default.each { |k, v| set(k, v) }
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



141
142
143
144
145
146
147
# File 'lib/mara/model/attributes.rb', line 141

def method_missing(name, *args, &_block)
  if name.to_s.end_with?('=')
    set(name.to_s.gsub(/=$/, ''), *args)
  else
    get(name, *args)
  end
end

Instance Method Details

#eachObject

Enumerate each key, value pair.

Since:

  • 1.0.0



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

def each
  return @storage.enum_for(:each) unless block_given?

  @storage.each do |*args|
    yield(*args)
  end
end

#fetch(key, default = nil, pre_formatted: false) ⇒ Any?

Get an attribute value but it that value is nil, return the default passed as the second argument.

Examples:

Get a default value.

attrs.set('foo', nil)
attrs.fetch('foo', 'bar')
# => 'bar'

Parameters:

  • key (#to_s)

    The key for the attribute you’re getting.

  • default (Any, nil) (defaults to: nil)

    The default value to return if the value for the key is nil.

  • pre_formatted (true, false) (defaults to: false)

    If the key is already normalized.

Returns:

  • (Any, nil)

Since:

  • 1.0.0



107
108
109
110
111
112
# File 'lib/mara/model/attributes.rb', line 107

def fetch(key, default = nil, pre_formatted: false)
  value = get(key, pre_formatted: pre_formatted)
  return default if value.nil?

  value
end

#get(key, pre_formatted: false) ⇒ Any?

Get a value an attribute value.

Parameters:

  • key (#to_s)

    The key for the attribute you’re getting.

  • pre_formatted (true, false) (defaults to: false)

    If the key is already normalized.

Returns:

  • (Any, nil)

Raises:

  • (AttributeError)

    The normalized value of the key can’t be blank.

Since:

  • 1.0.0



82
83
84
85
86
87
88
# File 'lib/mara/model/attributes.rb', line 82

def get(key, pre_formatted: false)
  nkey = normalize_key(key, pre_formatted)

  raise AttributeError, "Can't get an attribute without a key" if nkey.blank?

  @storage[nkey]
end

#key?(key, pre_formatted: false) ⇒ true, false

Check if a key exists.

Parameters:

  • key (#to_s)

    The key you want to check to see if it exists.

  • pre_formatted (true, false) (defaults to: false)

    If the key is already normalized.

Returns:

  • (true, false)

Since:

  • 1.0.0



122
123
124
125
# File 'lib/mara/model/attributes.rb', line 122

def key?(key, pre_formatted: false)
  nkey = normalize_key(key, pre_formatted)
  @storage.key?(nkey)
end

#respond_to_missing?(_name, _include_private = false) ⇒ Boolean

Attribute Magic

Returns:

  • (Boolean)

Since:

  • 1.0.0



153
154
155
# File 'lib/mara/model/attributes.rb', line 153

def respond_to_missing?(_name, _include_private = false)
  true
end

#set(key, value, pre_formatted: false) ⇒ void

Note:

If the value is nil, the key will be deleted.

This method returns an undefined value.

Set a key, value pair on the attributes.

Parameters:

  • key (#to_s)

    The key for the attribute you’re trying to set.

    This key will be normalized before being stored as an attribute.

  • value (Any, nil)

    The value to set for the key.

  • pre_formatted (true, false) (defaults to: false)

    If the key is already normalized.

Raises:

  • (AttributeError)

    The normalized value of the key can’t be blank.

Since:

  • 1.0.0



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mara/model/attributes.rb', line 60

def set(key, value, pre_formatted: false)
  nkey = normalize_key(key, pre_formatted)

  raise AttributeError, "Can't set an attribute without a key" if nkey.blank?

  if value.nil?
    @storage.delete(nkey)
  else
    @storage[nkey] = value
  end
end

#to_hHash<String, Any>

Dump the storage backend into a hash.

Returns:

  • (Hash<String, Any>)

Since:

  • 1.0.0



133
134
135
# File 'lib/mara/model/attributes.rb', line 133

def to_h
  @storage.dup
end