Class: SuperRecursiveOpenStruct

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

Instance Method Summary collapse

Constructor Details

#initialize(hash, args = {}) ⇒ SuperRecursiveOpenStruct

Returns a new instance of SuperRecursiveOpenStruct.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/super_recursive_open_struct.rb', line 4

def initialize(hash, args = {})
  @raise_on_missing_methods = args.fetch(:raise_on_missing_methods, true)

  @target = if hash.is_a?(Array)
    hash.map { |item| self.class.new(item) }
  else
    RecursiveOpenStruct.new(hash, recurse_over_arrays: true)
  end

  if @raise_on_missing_methods
    @target.instance_eval do
      def method_missing(method, *_args)
        raise NoMethodError, "undefined method \`#{method}' for #{self.inspect}"
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



22
23
24
25
26
27
28
# File 'lib/super_recursive_open_struct.rb', line 22

def method_missing(method, *args, &block)
  if !@raise_on_missing_methods || @target.respond_to?(method)
    @target.__send__(method, *args, &block)
  else
    super
  end
end

Instance Method Details

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

Returns:

  • (Boolean)


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

def respond_to_missing?(method, include_private = false)
  @target.respond_to?(method, include_private)
end