Class: DotRuby::Constant

Inherits:
BasicObject
Defined in:
lib/dotruby/constant.rb

Overview

The virtual constant class is a simple recorder. Every method called on it is recorded for later recall on the actual constant given by name.

To invoke the recordeed calls on the real constant use ‘to_proc.call`.

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Constant

Initialize configuration.

Parameters:

  • Name (Symbol)

    of constant.



15
16
17
18
# File 'lib/dotruby/constant.rb', line 15

def initialize(name)
  @name  = name
  @calls = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *a, &b) ⇒ Object



28
29
30
# File 'lib/dotruby/constant.rb', line 28

def method_missing(s, *a, &b)
  @calls << [s, a, b]
end

Instance Method Details

#inspectString

An inspection string for the Configuration class.

Returns:

  • (String)


23
24
25
# File 'lib/dotruby/constant.rb', line 23

def inspect
  "#<Constant #{@name}>"
end

#to_procProc

Create a Proc instance that will recall the method invocations on the actual constant.

Returns:

  • (Proc)


43
44
45
46
47
48
49
50
51
# File 'lib/dotruby/constant.rb', line 43

def to_proc
  name, calls = @name, @calls
  ::Proc.new do
    const = ::Object.const_get(name)
    calls.each do |s, a, b|
      const.public_send(s, *a, &b)
    end
  end
end