Class: OverridesInstance

Inherits:
Object show all
Defined in:
lib/everyday-cli-utils/override.rb

Instance Method Summary collapse

Constructor Details

#initialize(overrides, obj, ind = 0) ⇒ OverridesInstance

Returns a new instance of OverridesInstance.



21
22
23
24
25
# File 'lib/everyday-cli-utils/override.rb', line 21

def initialize(overrides, obj, ind = 0)
  @overrides = overrides
  @obj       = obj
  @ind       = ind
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



46
47
48
# File 'lib/everyday-cli-utils/override.rb', line 46

def method_missing(symbol, *args, &block)
  call_override(symbol, *args, &block)
end

Instance Method Details

#[](ind) ⇒ Object



27
28
29
# File 'lib/everyday-cli-utils/override.rb', line 27

def [](ind)
  OverridesInstance.new(@overrides, @obj, @ind - ind)
end

#call_override(method_symbol, *args, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/everyday-cli-utils/override.rb', line 31

def call_override(method_symbol, *args, &block)
  if @overrides.has_key?(method_symbol.to_sym)
    overrides = @overrides[method_symbol.to_sym]
    ind       = overrides.count + (@ind - 1)
    ovin      = Thread.current["overrides_ind_#{@obj.__id__}"] || 0
    ind       += ovin
    Thread.current["overrides_ind_#{@obj.__id__}"] = @ind + ovin - 1
    rv                                             = overrides[ind].bind(@obj).call(*args, &block)
    Thread.current["overrides_ind_#{@obj.__id__}"] = ovin
    rv
  else
    @obj.send(method_symbol.to_sym, *args, &block)
  end
end