Class: Cmds::ERBContext

Inherits:
BasicObject
Defined in:
lib/cmds/erb_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, kwds, tokenize_options_opts: {}) ⇒ ERBContext

Returns a new instance of ERBContext.



5
6
7
8
9
10
# File 'lib/cmds/erb_context.rb', line 5

def initialize args, kwds, tokenize_options_opts: {}
  @args = args
  @kwds = kwds
  @arg_index = 0
  @tokenize_options_opts = tokenize_options_opts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cmds/erb_context.rb', line 12

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/cmds/erb_context.rb', line 3

def args
  @args
end

Instance Method Details

#argObject



40
41
42
# File 'lib/cmds/erb_context.rb', line 40

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

#get_bindingObject



36
37
38
# File 'lib/cmds/erb_context.rb', line 36

def get_binding
  ::Kernel.send :binding
end