Class: Lotus::Utils::Attributes
- Inherits:
-
Object
- Object
- Lotus::Utils::Attributes
- Defined in:
- lib/lotus/utils/attributes.rb
Overview
A set of attributes.
It internally stores the data as a Hash.
All the operations convert keys to strings. This strategy avoids memory attacks due to Symbol abuses when parsing untrusted input.
At the same time, this allows to get/set data with the original key or with the string representation. See the examples below.
Instance Method Summary collapse
-
#get(attribute) ⇒ Object, NilClass
(also: #[])
Get the value associated with the given attribute.
-
#initialize(hash = {}) ⇒ Lotus::Utils::Attributes
constructor
Initialize a set of attributes All the keys of the given Hash are recursively converted to strings.
-
#set(attribute, value) ⇒ NilClass
Set the given value for the given attribute.
-
#to_h ⇒ ::Hash
Returns a deep duplicated copy of the attributes as a Hash.
Constructor Details
#initialize(hash = {}) ⇒ Lotus::Utils::Attributes
Initialize a set of attributes All the keys of the given Hash are recursively converted to strings.
32 33 34 |
# File 'lib/lotus/utils/attributes.rb', line 32 def initialize(hash = {}) @attributes = Utils::Hash.new(hash, &nil).stringify! end |
Instance Method Details
#get(attribute) ⇒ Object, NilClass Also known as: []
Get the value associated with the given attribute
68 69 70 |
# File 'lib/lotus/utils/attributes.rb', line 68 def get(attribute) @attributes[attribute.to_s] end |
#set(attribute, value) ⇒ NilClass
Set the given value for the given attribute
100 101 102 103 |
# File 'lib/lotus/utils/attributes.rb', line 100 def set(attribute, value) @attributes[attribute.to_s] = value nil end |
#to_h ⇒ ::Hash
Returns a deep duplicated copy of the attributes as a Hash
110 111 112 113 114 115 116 |
# File 'lib/lotus/utils/attributes.rb', line 110 def to_h ::Hash[].tap do |result| @attributes.each do |k, v| result[k] = _read_value(v) end end end |