Class: OpenprojectApi::ObjectifiedHash

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

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ ObjectifiedHash

Returns a new instance of ObjectifiedHash.



5
6
7
8
9
10
11
12
# File 'lib/openproject_api/objectified_hash.rb', line 5

def initialize(hash)
  @hash = hash
  @data = hash.each_with_object({}) do |(key, value), data|
    value          = self.class.new(value) if value.is_a? Hash
    value          = value.map { |v| v.is_a?(Hash) ? self.class.new(v) : v } if value.is_a? Array
    data[key.to_s] = value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



29
30
31
32
33
34
35
36
37
# File 'lib/openproject_api/objectified_hash.rb', line 29

def method_missing(method_name, *args, &block)
  if @data.key?(method_name.to_s)
    @data[method_name.to_s]
  elsif @data.respond_to?(method_name)
    @data.send(method_name, *args, &block)
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  @data[key]
end

#inspectObject



19
20
21
# File 'lib/openproject_api/objectified_hash.rb', line 19

def inspect
  "#<#{self.class}:#{object_id} {hash: #{@hash.inspect}}"
end

#to_hashObject Also known as: to_h



14
15
16
# File 'lib/openproject_api/objectified_hash.rb', line 14

def to_hash
  @hash
end