Class: Vagrant::Prison::ConfigProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/prison/config_proxy.rb

Overview

this is a sort-of proc-to-ast with certain constraints only available to “dsls” which configure things

  • blocks yielded to never care about the return value

  • blocks only yield one argument

  • it’s presumed the yielded argument will be acted on through method calls

  • method chains can be re-evaluated in any order, as long as the chain is preserved

Instance Method Summary collapse

Constructor Details

#initializeConfigProxy

Returns a new instance of ConfigProxy.



11
12
13
# File 'lib/vagrant/prison/config_proxy.rb', line 11

def initialize
  @hash = { }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vagrant/prison/config_proxy.rb', line 15

def method_missing(sym, *args)
  @hash[sym] ||= []
  yield_proxy = ConfigProxy.new
  new_proxy   = ConfigProxy.new
  @hash[sym].push(
    { 
      :args => args,
      :yields => yield_proxy, 
      :retval => new_proxy 
    }
  )

  if block_given?
    yield yield_proxy
  end

  return new_proxy
end

Instance Method Details

#__has_statements__Object



52
53
54
# File 'lib/vagrant/prison/config_proxy.rb', line 52

def __has_statements__
  @hash.keys.count > 0
end

#eval(object) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant/prison/config_proxy.rb', line 34

def eval(object)
  @hash.each do |key, values|
    values.each do |value|
      retval =  if value[:yields].__has_statements__
                  object.send(key, *value[:args]) do |obj|
                    value[:yields].eval(obj)
                  end
                else
                  object.send(key, *value[:args])
                end

      if value[:retval].__has_statements__
        value[:retval].eval(retval)
      end
    end
  end
end

#inspect(indent = 0) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vagrant/prison/config_proxy.rb', line 56

def inspect(indent = 0)
  ret_str = ""
  do_indent = " " * indent
  @hash.each do |key, values|
    ret_str += do_indent + key.inspect
    ret_str += do_indent + " => [\n"
    values.each do |value|
      ret_str += do_indent + "{\n"
      if value[:args]
        ret_str += do_indent + " " + :args.inspect + " => " + value[:args].inspect + "\n"
      end
      [:yields, :retval].each do |blah|
        if value[blah].__has_statements__
          ret_str += do_indent + " " + blah.inspect + " =>\n" + value[blah].inspect(indent + 3) + "\n"
        end
      end
      ret_str += do_indent + "}\n"
    end
  end

  return ret_str
end