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.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/super_recursive_open_struct.rb', line 5

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

  patch_object(@target) if @raise_on_missing_methods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



17
18
19
20
21
22
23
# File 'lib/super_recursive_open_struct.rb', line 17

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)


25
26
27
# File 'lib/super_recursive_open_struct.rb', line 25

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

#to_jsonObject



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

def to_json
  ActiveSupport::JSON.encode @target
end