Class: Lotus::Utils::Attributes

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.3.2

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Lotus::Utils::Attributes

Initialize a set of attributes All the keys of the given Hash are recursively converted to strings.

Examples:

require 'lotus/utils/attributes'

attributes = Lotus::Utils::Attributes.new(a: 1, b: { 2 => [3, 4] })
attributes.to_h # => { "a" => 1, "b" => { "2" => [3, 4] } }

Parameters:

  • hash (#to_h) (defaults to: {})

    a Hash or any object that implements #to_h

Since:

  • 0.3.2



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

Examples:

require 'lotus/utils/attributes'

attributes = Lotus::Utils::Attributes.new(a: 1, 'b' => 2, 23 => 'foo')

attributes.get(:a)  # => 1
attributes.get('a') # => 1
attributes[:a]      # => 1
attributes['a']     # => 1

attributes.get(:b)  # => 2
attributes.get('b') # => 2
attributes[:b]      # => 2
attributes['b']     # => 2

attributes.get(23)   # => "foo"
attributes.get('23') # => "foo"
attributes[23]       # => "foo"
attributes['23']     # => "foo"

attributes.get(:unknown)  # => nil
attributes.get('unknown') # => nil
attributes[:unknown]      # => nil
attributes['unknown']     # => nil

Parameters:

  • attribute (#to_s)

    a String or any object that implements #to_s

Returns:

  • (Object, NilClass)

    the associated value, if present

Since:

  • 0.3.2



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

Examples:

require 'lotus/utils/attributes'

attributes = Lotus::Utils::Attributes.new

attributes.set(:a, 1)
attributes.get(:a)  # => 1
attributes.get('a') # => 1

attributes.set('b', 2)
attributes.get(:b)  # => 2
attributes.get('b') # => 2

attributes.set(23, 'foo')
attributes.get(23)   # => "foo"
attributes.get('23') # => "foo"

Parameters:

  • attribute (#to_s)

    a String or any object that implements #to_s

  • value (Object)

    any value

Returns:

  • (NilClass)

Since:

  • 0.3.2



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

Returns:

  • (::Hash)

Since:

  • 0.3.2



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