Sha256: 5e1da939ad1aba8b3686567950a987b3aea2691e6d50606cd59cfc538f71f068

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module Cmds
  class ERBContext < BasicObject
    def initialize args, kwds
      @args = args
      @kwds = kwds
      @arg_index = 0
    end

    def method_missing sym, *args, &block
      if args.empty? && block.nil?
        if sym.to_s[-1] == '?'
          key = sym.to_s[0...-1].to_sym
          # allow `false` to be omitted as well as missing and `nil`
          # by returning `nil` if the value is "false-y"
          @kwds[key] if @kwds[key]
        else
          if @kwds.key? sym
            @kwds[sym]
          elsif @kwds.key? sym.to_s
            @kwds[sym.to_s]
          else
            ::Kernel.raise ::KeyError.new ::NRSER.squish <<-END
              couldn't find keys #{ sym.inspect } or #{ sym.to_s.inspect }
              in keywords #{ @kwds.inspect }
            END
          end
        end
      else
        super sym, *args, &block
      end
    end

    def get_binding
      ::Kernel.send :binding
    end

    def arg
      @args.fetch(@arg_index).tap {@arg_index += 1}
    end
  end # end ERBContext
end # module Cmds

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cmds-0.1.5 lib/cmds/erb_context.rb
cmds-0.1.4 lib/cmds/erb_context.rb
cmds-0.1.3 lib/cmds/erb_context.rb