Class: StrictOpenStruct

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ StrictOpenStruct

Returns a new instance of StrictOpenStruct.



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

def initialize(*args)
  @open_struct = OpenStruct.new(*args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Raises NoMethodError unless the underlying OpenStruct responds to the method or the method is a setter



11
12
13
14
15
16
17
# File 'lib/strict_open_struct.rb', line 11

def method_missing(method_name, *args, &block)
  if open_struct_responds_to?(method_name)
    @open_struct.send(method_name, *args, &block)
  else
    failure(method_name)
  end
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  @open_struct == other.instance_variable_get("@open_struct")
end

#[](key) ⇒ Object



27
28
29
# File 'lib/strict_open_struct.rb', line 27

def [](key)
  @open_struct.to_h.fetch(key.to_sym)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(method_name, include_private = false)
  open_struct_responds_to?(method_name)
end