Class: BatCave::DSL::IvarBinding

Inherits:
Object
  • Object
show all
Defined in:
lib/batcave/dsl.rb

Overview

Metaprogramming to provide instance variables from a class as local-variable-like things in a binding. For use with ERB and such.

This is a fun hack.

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ IvarBinding

Returns a new instance of IvarBinding.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/batcave/dsl.rb', line 224

def initialize(object)
  # Make a new Object subclass
  klass = Class.new(Object)

  # Find all instance variables in 'object' and make
  # them methods in our new class. Each method will
  # simply return that named instance variable.
  object.instance_variables.each do |ivar|
    meth = ivar.to_s.gsub("@", "")
    klass.instance_eval do
      define_method(meth) do
        object.instance_variable_get(ivar)
      end
    end
  end

  # Make a method that returns the binding for this class
  klass.instance_eval do
    define_method(:_binding) do
      return binding
    end
  end
  @instance = klass.new
end

Instance Method Details

#bindingObject

def initialize



249
250
251
# File 'lib/batcave/dsl.rb', line 249

def binding
  return @instance._binding
end