Class: DependencyResolvers::ProxyObject

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy, caller = nil) ⇒ ProxyObject

Returns a new instance of ProxyObject.



14
15
16
17
# File 'lib/dependency_resolvers/proxy_object.rb', line 14

def initialize(proxy, caller=nil)
  @proxy = proxy
  @caller = caller
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &block) ⇒ Object

method_missing Because this ProxyObject is responsible for proxying methods to the proxy object, the method_missing method is used to curry methods across



92
93
94
# File 'lib/dependency_resolvers/proxy_object.rb', line 92

def method_missing(m,*a,&block)
  proxy.send(m,*a,&block)
end

Instance Attribute Details

#callerObject (readonly)

Returns the value of attribute caller.



12
13
14
# File 'lib/dependency_resolvers/proxy_object.rb', line 12

def caller
  @caller
end

#current_printing_methodObject (readonly)

Returns the value of attribute current_printing_method.



12
13
14
# File 'lib/dependency_resolvers/proxy_object.rb', line 12

def current_printing_method
  @current_printing_method
end

#proxyObject

Returns the value of attribute proxy.



11
12
13
# File 'lib/dependency_resolvers/proxy_object.rb', line 11

def proxy
  @proxy
end

Instance Method Details

#compile(meth_name) ⇒ Object

Compile Arguments:

* String
  If a string is passed, it is assumed to be an erb template and
  is rendered in the binding of this object (the proxy is contained)
* Symbol
  If a symbol is passed, it is assumed to be a method, primarily
  used for <tt>print_to_<resolver_name></tt> methods

The output of compile is an erb template that is rendered in the context of this proxy object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dependency_resolvers/proxy_object.rb', line 29

def compile(meth_name)
  str = case meth_name
  when String
    meth_name
  when Symbol
    @current_printing_method = meth_name
    self.send(meth_name)
  else
    raise PoolParty::PoolPartyError.create("ProxyObjectError", "Compilation of #{proxy.inspect} error. Strings and symbols are supported")
  end
  
  str = handle_print_variable(str) if proxy.class == PoolParty::Resources::Variable
  
  begin
    ERB.new(str).result(self.send(:binding))
  rescue Exception => e
    ""
  end
end

#handle_print_variable(var) ⇒ Object

Print variables



80
81
82
# File 'lib/dependency_resolvers/proxy_object.rb', line 80

def handle_print_variable(var)
  DependencyResolvers::Base.handle_print_variable(var)
end

#instanceObject



84
85
86
# File 'lib/dependency_resolvers/proxy_object.rb', line 84

def instance
  @caller
end

Print the dsl options in the Erb string format given by the method print_dsl_options(str) To use print_dsl_options, the format is: print_dsl_options(“print :key = ‘:value’”) The string substitution uses the ^ substitution found in string.rb This will substitute the key and the value in the format given by the string passed. For instance

dsl_options = {:to => "world", :message => "hello"}
print_dsl_options(":key => :value") =
  message => hello
  to => world

This should be used if all the dsl_options are to printed in the same format



62
63
64
65
66
# File 'lib/dependency_resolvers/proxy_object.rb', line 62

def print_dsl_options(str)
  dsl_options.map do |k,v|
    v.nil? ? nil : str ^ {:key => k, :value => v}
  end.compact.join("\n")
end

Take all the ordered_resources of the proxy object and print them with the current_printing_method, aka :chef for print_to_chef This creates a new proxy object with each resource and sends it :compile with the current printing method, collects the output and joins them with a newline



73
74
75
76
77
# File 'lib/dependency_resolvers/proxy_object.rb', line 73

def print_resources
  resources.map do |res|
    ProxyObject.new(res).compile(current_printing_method)
  end.join("\n")
end