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.



36
37
38
39
40
# File 'lib/everyday-cli-utils/override.rb', line 36

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



68
69
70
# File 'lib/everyday-cli-utils/override.rb', line 68

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

Instance Method Details

#[](ind) ⇒ Object



42
43
44
# File 'lib/everyday-cli-utils/override.rb', line 42

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

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



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

def call_override(method_symbol, *args, &block)
  @overrides.has_key?(method_symbol.to_sym) ? -> {
    overrides, ind, ovin = get_overrides_and_inds(method_symbol)
    call_override_at_index(overrides, ind, ovin, args, &block)
  }.call : @obj.send(method_symbol.to_sym, *args, &block)
end

#call_override_at_index(overrides, ind, ovin, args, &block) ⇒ Object



53
54
55
56
57
58
# File 'lib/everyday-cli-utils/override.rb', line 53

def call_override_at_index(overrides, ind, ovin, args, &block)
  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
end

#get_overrides_and_inds(method_symbol) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/everyday-cli-utils/override.rb', line 60

def get_overrides_and_inds(method_symbol)
  overrides = @overrides[method_symbol.to_sym]
  ind       = overrides.count + (@ind - 1)
  ovin      = Thread.current["overrides_ind_#{@obj.__id__}"] || 0
  ind       += ovin
  return overrides, ind, ovin
end