Class: InlineTesting::TestEnvironment::Locals::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/inline_testing/test_environment/locals.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(local, *args, &blk) ⇒ Hash

Returns ‘local` is the key and it holds another Hash with :obj and :msg.

Returns:

  • (Hash)

    ‘local` is the key and it holds another Hash with :obj and :msg.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/inline_testing/test_environment/locals.rb', line 16

def method_missing(local, *args, &blk)
  if args.empty?
    raise(ArgumentError, "No argument given.")
  elsif args.length == 2 && !blk.nil?
    raise(ArgumentError, "You can either give one arg and a block or two args, not both.") 
  end
  @result[local] = { :obj => args.first }
  @result[local][:msg] = if args.length == 1
    blk.call(@result[local][:obj]) if block_given?
  elsif args.length == 2
    args.last
  end
end

Class Method Details

.call(&blk) ⇒ Object



8
9
10
11
12
13
# File 'lib/inline_testing/test_environment/locals.rb', line 8

def self.call(&blk)
  instance = self.new
  instance.instance_variable_set(:@result, {})
  instance.instance_eval(&blk)
  instance.instance_variable_get(:@result) || {}
end