Sha256: 33d24b579616fe06319f3ad150980cfd5ab2a4b96f118bb86e973808260862a5

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require 'rib'

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

  def loop_eval(str)
    case obj_or_binding = (Rib.vars[:anchor] ||= []).last
      when NilClass
        super

      when Binding
        @binding = obj_or_binding
        super

      else
        obj_or_binding.instance_eval(str, "(#{config[:name]})", config[:line])
    end
  end

  def prompt
    if Rib.const_defined?(:Color) && kind_of?(Rib::Color) &&
       obj_or_binding = (Rib.vars[:anchor] ||= []).last

      super.sub(config[:name], format_color(obj_or_binding, config[:name]))
    else
      super
    end
  end

  # if the object is the same, then we're exiting from an anchor,
  # so don't print anything.
  def print_result result
    super unless !result.nil? &&
                 result.object_id == Rib.vars[:anchor_last].object_id
  end

  module Imp
    def short_inspect obj_or_binding
      obj_or_binding.inspect[0..9]
    end
  end

  module AnchorImp
    def anchor obj_or_binding
      return if Rib::Anchor.disabled?

      (Rib.vars[:anchor] ||= []) << obj_or_binding
      name = Rib::P.short_inspect(obj_or_binding)

      Rib.shells <<
        Rib::Shell.new(Rib.config.merge(
          :name   => name,
          :prompt => "#{name}(#{Rib.vars[:anchor].size})" +
                     (Rib.config[:prompt] || '>> ')))

      Rib.shell.loop

    ensure
      # stores to check if we're exiting from an anchor
      Rib.vars[:anchor_last] = Rib.vars[:anchor].pop
      Rib.shells.pop
    end
  end

  Plugin.extend(Imp)
  Rib   .extend(AnchorImp)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rib-0.1.0 lib/rib/zore/anchor.rb