Class: Invokr::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/invokr/builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, injector, implicit_block) ⇒ Builder

Returns a new instance of Builder.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/invokr/builder.rb', line 10

def initialize method, injector, implicit_block
  @argument_names = method.parameters.map &:last
  @injector = injector
  @method = method
  @opt_arg_name = nil

  @block_arg = nil
  @implicit_block = implicit_block
  @keyword_args = {}
  @positional_args = []
  @missing_args = []

  set_unused_args
end

Instance Attribute Details

#argument_namesObject (readonly)

Returns the value of attribute argument_names.



8
9
10
# File 'lib/invokr/builder.rb', line 8

def argument_names
  @argument_names
end

#injectorObject (readonly)

Returns the value of attribute injector.



8
9
10
# File 'lib/invokr/builder.rb', line 8

def injector
  @injector
end

#methodObject (readonly)

Returns the value of attribute method.



8
9
10
# File 'lib/invokr/builder.rb', line 8

def method
  @method
end

#missing_argsObject (readonly)

Returns the value of attribute missing_args.



8
9
10
# File 'lib/invokr/builder.rb', line 8

def missing_args
  @missing_args
end

#unused_argsObject (readonly)

Returns the value of attribute unused_args.



8
9
10
# File 'lib/invokr/builder.rb', line 8

def unused_args
  @unused_args
end

Class Method Details

.build(*args) ⇒ Object



3
4
5
6
# File 'lib/invokr/builder.rb', line 3

def self.build *args
  builder = new *args
  builder.build
end

Instance Method Details

#buildObject



25
26
27
28
29
30
# File 'lib/invokr/builder.rb', line 25

def build
  handle_args!
  check_for_unused_args!
  check_for_missing_args!
  build_invocation
end

#build_hash_from_extra_argsObject



95
96
97
98
99
100
101
# File 'lib/invokr/builder.rb', line 95

def build_hash_from_extra_args
  return nil if unused_args.empty?
  hsh = {}
  unused_args.each do |arg| hsh[arg] = injector.fetch arg end
  unused_args.clear
  hsh
end

#build_invocationObject



32
33
34
35
36
37
38
39
# File 'lib/invokr/builder.rb', line 32

def build_invocation
  @block_arg = @implicit_block if @implicit_block
  if method.respond_to? :name
    Invocation.new method.name, @positional_args, @keyword_args, @block_arg
  else
    Invocation.new :call, @positional_args, @keyword_args, @block_arg
  end
end

#check_for_missing_args!Object



108
109
110
111
# File 'lib/invokr/builder.rb', line 108

def check_for_missing_args!
  return if missing_args.empty?
  raise MissingArgumentsError.new self, missing_args
end

#check_for_unused_args!Object



103
104
105
106
# File 'lib/invokr/builder.rb', line 103

def check_for_unused_args!
  return if unused_args.empty?
  raise ExtraArgumentsError.new self, unused_args
end

#handle_args!Object



41
42
43
44
45
# File 'lib/invokr/builder.rb', line 41

def handle_args!
  method.parameters.each do |type, identifier|
    send "handle_#{type}_arg", identifier
  end
end

#handle_block_arg(identifier) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/invokr/builder.rb', line 76

def handle_block_arg identifier
  if injector.has_key? identifier and @implicit_block
    unused_args << identifier and return
  end
  @block_arg = injector.fetch identifier do
    @implicit_block or missing_argument! identifier
  end
end

#handle_key_arg(identifier) ⇒ Object



66
67
68
69
# File 'lib/invokr/builder.rb', line 66

def handle_key_arg identifier
  return unless injector.has_key? identifier
  @keyword_args[identifier] = injector.fetch identifier do nil end
end

#handle_keyreq_arg(identifier) ⇒ Object



61
62
63
64
# File 'lib/invokr/builder.rb', line 61

def handle_keyreq_arg identifier
  arg = injector.fetch identifier do missing_argument! identifier end
  @keyword_args[identifier] = arg
end

#handle_opt_arg(identifier) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/invokr/builder.rb', line 52

def handle_opt_arg identifier
  optional_arg_error! identifier if hit_opt_arg?
  @opt_arg_name = identifier
  arg = injector.fetch identifier do
    build_hash_from_extra_args or return
  end
  @positional_args << arg
end

#handle_req_arg(identifier) ⇒ Object



47
48
49
50
# File 'lib/invokr/builder.rb', line 47

def handle_req_arg identifier
  arg = injector.fetch identifier do missing_argument! identifier end
  @positional_args << arg
end

#handle_rest_arg(identifier) ⇒ Object Also known as: handle_keyrest_arg



71
72
73
# File 'lib/invokr/builder.rb', line 71

def handle_rest_arg identifier
  raise UnsupportedArgumentsError.new(self, [identifier])
end

#hit_opt_arg?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/invokr/builder.rb', line 85

def hit_opt_arg?
  @opt_arg_name ? true : false
end

#missing_argument!(identifier) ⇒ Object



122
123
124
# File 'lib/invokr/builder.rb', line 122

def missing_argument! identifier
  missing_args << identifier
end

#optional_arg_error!(identifier) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/invokr/builder.rb', line 113

def optional_arg_error! identifier
  name = method.respond_to?(:name) ? method.name : 'anonymous'
  raise OptionalPositionalArgumentError.new(
    name,
    @opt_arg_name,
    identifier,
  )
end

#set_unused_argsObject



89
90
91
92
93
# File 'lib/invokr/builder.rb', line 89

def set_unused_args
  @unused_args = injector.keys.flat_map do |hsh_arg|
    argument_names.include?(hsh_arg) ? [] : [hsh_arg]
  end
end