Class: Structifier::Base

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

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Base



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

def initialize(klass)
  @klass = klass
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/structifier.rb', line 9

def method_missing(method_name, *args)
  if @klass.respond_to?(method_name) && @klass.method(method_name).call.is_a?(Hash)
    unless self.respond_to?(method_name)
      define_singleton_method(method_name) do
        instance_variable_get("@#{method_name}") || instance_variable_set("@#{method_name}", OpenStruct.new(@klass.method(method_name).call)) 
      end
    end
    method(method_name).call
  else
    super
  end
end