Sha256: 1a03faf98338f97527a567c84300edcc8cf9805ca2040ff36eb084582e8d3b86

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require 'rib'

module Rib::Underscore
  include Rib::Plugin
  Shell.use(self)

  IVAR = {:_  => :@__rib_result__,
          :__ => :@__rib_exception__}

  def before_loop
    return super if Underscore.disabled?
    eliminate_warnings
    inject_inspecter
    super
  end

  def loop_eval input
    return super if Underscore.disabled?
    bound_object.instance_variable_set(:@__rib_result__, super)
  end

  def print_eval_error e
    return super if Underscore.disabled?
    bound_object.instance_variable_set(:@__rib_exception__, e)
    super
  end

  private
  def eliminate_warnings
    IVAR.values.each{ |ivar| bound_object.instance_variable_set(ivar, nil) }
  end

  def inject_inspecter
    IVAR.each{ |k, v|
      bound_singleton.send(:define_method, k){
        instance_variable_get(v)
      } unless bound_object.respond_to?(k) # only inject for innocences
    }
  end

  def bound_singleton
    if respond_to?(:singleton_class)
      bound_object.singleton_class
    else
      class << bound_object; self; end
    end
  end

  def bound_object
    config[:binding].eval('self')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rib-0.1.0 lib/rib/core/underscore.rb