Class: Mara::Model::Attributes
- Inherits:
-
Object
- Object
- Mara::Model::Attributes
- 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.
Instance Method Summary collapse
-
#each ⇒ Object
Enumerate each key, value pair.
-
#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.
-
#get(key, pre_formatted: false) ⇒ Any?
Get a value an attribute value.
-
#initialize(default) ⇒ Attributes
constructor
Create a new instance of attributes.
-
#key?(key, pre_formatted: false) ⇒ true, false
Check if a key exists.
-
#method_missing(name, *args, &_block) ⇒ Object
Attribute Magic.
-
#respond_to_missing?(_name, _include_private = false) ⇒ Boolean
Attribute Magic.
-
#set(key, value, pre_formatted: false) ⇒ void
Set a key, value pair on the attributes.
-
#to_h ⇒ Hash<String, Any>
Dump the storage backend into a hash.
Constructor Details
#initialize(default) ⇒ Attributes
Create a new instance of attributes.
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
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
#each ⇒ Object
Enumerate each key, value pair.
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.
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.
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.
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
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
If the value is nil
, the key will be deleted.
This method returns an undefined value.
Set a key, value pair on the attributes.
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_h ⇒ Hash<String, Any>
Dump the storage backend into a hash.
133 134 135 |
# File 'lib/mara/model/attributes.rb', line 133 def to_h @storage.dup end |