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.respond_to?(method_name) || method_name =~ /=$/
    @open_struct.send(method_name, *args, &block)
  else
    fail NoMethodError, "undefined method `#{method_name}' for #{self}"
  end
end

Instance Method Details

#==(other) ⇒ Object



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

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