Class: SelectiveBinding

Inherits:
Object show all
Defined in:
lib/selective_binding.rb

Overview

Create a selective binding. Useful for controlling ERB builds. The following will allow MyObj instances to provide a binding that will only expose MyObj#my_attr and MyObj#get_some_value:

require 'selective_binding'

class MyObj
  attr_accessor :my_attr

  def modify_something
    # Don't want this exposed in binding!
  end

  def get_some_value
    # Want this exposed in binding.
    "some value"
  end

  def get_binding
    selective_binding :my_attr, :get_some_value
  end
end

my_obj = MyObj.new

eval "get_some_value", my_obj.get_binding
#=> "some value"

eval "modify_something", my_obj.get_binding
#=> NameError: undefined local variable or method `modify_something'

Additionally, a block can be passed to selective bindings to fill the role of a default value, or method_missing.

Constant Summary collapse

VERSION =

Gem Version

'1.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ SelectiveBinding

Creates a new selective binding with the target object to create a binding to.



45
46
47
# File 'lib/selective_binding.rb', line 45

def initialize target
  @target = target
end

Instance Method Details

#forward(*method_names) ⇒ Object

Forward a method to the object instance.

binder.forward :method1, :method2, ...


88
89
90
91
92
93
94
95
96
97
# File 'lib/selective_binding.rb', line 88

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.send(#{method_name.to_sym.inspect}, *args, &block)
    end
    STR
  end
end

#get_bindingObject

Retrieve the object’s binding.



111
112
113
# File 'lib/selective_binding.rb', line 111

def get_binding
  binding
end

#import_hash(hash) ⇒ Object

Takes a hash and assign each hash key/value as an attribute:

selective_binding.import_hash :attr1 => "value1",
                              :attr2 => "value2"


79
80
81
# File 'lib/selective_binding.rb', line 79

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.

selective_binding.set :server_name, "blah.com"


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/selective_binding.rb', line 54

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, &block)
      if Proc === @#{key}
        @#{key}.call(*args, &block)
      else
        @#{key}
      end
    end
  STR

  instance_eval eval_str, __FILE__, __LINE__ + 1
end

#set_default(value = nil, &block) ⇒ Object

Sets the default value for undefined attributes or methods.



103
104
105
# File 'lib/selective_binding.rb', line 103

def set_default value=nil, &block
  set :method_missing, value, &block if value || block_given?
end