Class: Sunshine::Binder
- Inherits:
-
Object
- Object
- Sunshine::Binder
- Defined in:
- lib/sunshine/binder.rb
Overview
Instantiated per deploy server and used to pass bindings to the config’s ERB build method:
binder.set :server_name, "blah.com"
binder.forward :server_method, ...
binder.get_binding
Instance Method Summary collapse
-
#forward(*method_names) ⇒ Object
Forward a method to the server instance.
-
#get_binding ⇒ Object
Retrieve the object’s binding.
-
#import_hash(hash) ⇒ Object
Takes a hash and assign each hash key/value as an attribute.
-
#initialize(target) ⇒ Binder
constructor
A new instance of Binder.
-
#set(key, value = nil, &block) ⇒ Object
Set the binding instance variable and accessor method.
Constructor Details
#initialize(target) ⇒ Binder
Returns a new instance of Binder.
12 13 14 |
# File 'lib/sunshine/binder.rb', line 12 def initialize target @target = target end |
Instance Method Details
#forward(*method_names) ⇒ Object
Forward a method to the server instance.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sunshine/binder.rb', line 51 def forward *method_names method_names.each do |method_name| instance_eval <<-STR, __FILE__, __LINE__ + 1 undef #{method_name} if defined?(#{method_name}) def #{method_name}(*args, &block) @target.#{method_name}(*args, &block) end STR end end |
#get_binding ⇒ Object
Retrieve the object’s binding.
66 67 68 |
# File 'lib/sunshine/binder.rb', line 66 def get_binding binding end |
#import_hash(hash) ⇒ Object
Takes a hash and assign each hash key/value as an attribute.
43 44 45 |
# File 'lib/sunshine/binder.rb', line 43 def import_hash hash hash.each{|k, v| self.set(k, v)} end |
#set(key, value = nil, &block) ⇒ Object
Set the binding instance variable and accessor method.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sunshine/binder.rb', line 20 def set key, value=nil, &block value ||= block if block_given? instance_variable_set("@#{key}", value) eval_str = <<-STR undef #{key} if defined?(#{key}) def #{key}(*args) if Proc === @#{key} @#{key}.call(*args) else @#{key} end end STR instance_eval eval_str, __FILE__, __LINE__ + 1 end |