Class: A9n::Struct

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/a9n/struct.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Struct

Returns a new instance of Struct.



9
10
11
# File 'lib/a9n/struct.rb', line 9

def initialize(data = {})
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *_args) ⇒ Object



28
29
30
# File 'lib/a9n/struct.rb', line 28

def method_missing(key, *_args)
  find(key)
end

Instance Attribute Details

#dataObject (readonly) Also known as: to_hash, to_h

Returns the value of attribute data.



5
6
7
# File 'lib/a9n/struct.rb', line 5

def data
  @data
end

Instance Method Details

#find(key) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/a9n/struct.rb', line 20

def find(key)
  if key && data.key?(key.to_sym)
    fetch(key.to_sym)
  else
    raise KeyNotFoundError.new, "Could not find #{key} in #{data.keys.inspect}"
  end
end

#merge(another_data) ⇒ Object



16
17
18
# File 'lib/a9n/struct.rb', line 16

def merge(another_data)
  data.merge!(another_data)
end

#set(key, value) ⇒ Object



32
33
34
# File 'lib/a9n/struct.rb', line 32

def set(key, value)
  data[key.to_sym] = value
end