Class: Rundeck::ObjectifiedHash

Inherits:
Object
  • Object
show all
Defined in:
lib/rundeck/objectified_hash.rb

Overview

Converts hashes to objects.

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ ObjectifiedHash

Creates a new ObjectifiedHash object.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rundeck/objectified_hash.rb', line 5

def initialize(hash)
  @hash = hash
  @data = hash.each_with_object({}) do |(key, value), data|
    value = if value.is_a?(Hash)
              ObjectifiedHash.new(value)
            elsif value.is_a?(Array)
              value.map { |e| ObjectifiedHash.new(e) }
            else
              value
            end
    data[key.to_s.downcase] = value
    data
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key) ⇒ Object

Respond if the requested method is a key in the data hash.



30
31
32
# File 'lib/rundeck/objectified_hash.rb', line 30

def method_missing(key)
  @data.key?(key.to_s) ? @data[key.to_s] : super
end

Instance Method Details

#respond_to?(method) ⇒ Boolean

Overload the parent method so this properly returns whether the instance of this object responds to the given method.

Returns:

  • (Boolean)


36
37
38
# File 'lib/rundeck/objectified_hash.rb', line 36

def respond_to?(method)
  @data.key?(method.to_s) || super
end

#to_hashHash Also known as: to_h

Return the original hash object

Returns:

  • (Hash)

    the original hash



23
24
25
# File 'lib/rundeck/objectified_hash.rb', line 23

def to_hash
  @hash
end