Class: Invokr::Builder
- Inherits:
-
Object
- Object
- Invokr::Builder
- Defined in:
- lib/invokr/builder.rb
Instance Attribute Summary collapse
-
#argument_names ⇒ Object
readonly
Returns the value of attribute argument_names.
-
#injector ⇒ Object
readonly
Returns the value of attribute injector.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#missing_args ⇒ Object
readonly
Returns the value of attribute missing_args.
-
#unused_args ⇒ Object
readonly
Returns the value of attribute unused_args.
Class Method Summary collapse
Instance Method Summary collapse
- #build ⇒ Object
- #build_hash_from_extra_args ⇒ Object
- #build_invocation ⇒ Object
- #check_for_missing_args! ⇒ Object
- #check_for_unused_args! ⇒ Object
- #handle_args! ⇒ Object
- #handle_block_arg(identifier) ⇒ Object
- #handle_key_arg(identifier) ⇒ Object
- #handle_keyreq_arg(identifier) ⇒ Object
- #handle_opt_arg(identifier) ⇒ Object
- #handle_req_arg(identifier) ⇒ Object
- #handle_rest_arg(identifier) ⇒ Object (also: #handle_keyrest_arg)
- #hit_opt_arg? ⇒ Boolean
-
#initialize(method, injector, implicit_block, allow_unused = false) ⇒ Builder
constructor
A new instance of Builder.
- #missing_argument!(identifier) ⇒ Object
- #optional_arg_error!(identifier) ⇒ Object
- #set_unused_args ⇒ Object
Constructor Details
#initialize(method, injector, implicit_block, allow_unused = false) ⇒ Builder
Returns a new instance of Builder.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/invokr/builder.rb', line 10 def initialize method, injector, implicit_block, allow_unused = false @argument_names = method.parameters.map &:last @injector = injector @method = method @opt_arg_name = nil @allow_unused = allow_unused @block_arg = nil @implicit_block = implicit_block @keyword_args = {} @positional_args = [] @missing_args = [] set_unused_args end |
Instance Attribute Details
#argument_names ⇒ Object (readonly)
Returns the value of attribute argument_names.
8 9 10 |
# File 'lib/invokr/builder.rb', line 8 def argument_names @argument_names end |
#injector ⇒ Object (readonly)
Returns the value of attribute injector.
8 9 10 |
# File 'lib/invokr/builder.rb', line 8 def injector @injector end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
8 9 10 |
# File 'lib/invokr/builder.rb', line 8 def method @method end |
#missing_args ⇒ Object (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_args ⇒ Object (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
#build ⇒ Object
26 27 28 29 30 31 |
# File 'lib/invokr/builder.rb', line 26 def build handle_args! check_for_unused_args! unless @allow_unused check_for_missing_args! build_invocation end |
#build_hash_from_extra_args ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/invokr/builder.rb', line 96 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_invocation ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/invokr/builder.rb', line 33 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
109 110 111 112 |
# File 'lib/invokr/builder.rb', line 109 def check_for_missing_args! return if missing_args.empty? raise MissingArgumentsError.new self, missing_args end |
#check_for_unused_args! ⇒ Object
104 105 106 107 |
# File 'lib/invokr/builder.rb', line 104 def check_for_unused_args! return if unused_args.empty? raise ExtraArgumentsError.new self, unused_args end |
#handle_args! ⇒ Object
42 43 44 45 46 |
# File 'lib/invokr/builder.rb', line 42 def handle_args! method.parameters.each do |type, identifier| send "handle_#{type}_arg", identifier end end |
#handle_block_arg(identifier) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/invokr/builder.rb', line 77 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
67 68 69 70 |
# File 'lib/invokr/builder.rb', line 67 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
62 63 64 65 |
# File 'lib/invokr/builder.rb', line 62 def handle_keyreq_arg identifier arg = injector.fetch identifier do missing_argument! identifier end @keyword_args[identifier] = arg end |
#handle_opt_arg(identifier) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/invokr/builder.rb', line 53 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
48 49 50 51 |
# File 'lib/invokr/builder.rb', line 48 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
72 73 74 |
# File 'lib/invokr/builder.rb', line 72 def handle_rest_arg identifier raise UnsupportedArgumentsError.new(self, [identifier]) end |
#hit_opt_arg? ⇒ Boolean
86 87 88 |
# File 'lib/invokr/builder.rb', line 86 def hit_opt_arg? @opt_arg_name ? true : false end |
#missing_argument!(identifier) ⇒ Object
123 124 125 |
# File 'lib/invokr/builder.rb', line 123 def missing_argument! identifier missing_args << identifier end |
#optional_arg_error!(identifier) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/invokr/builder.rb', line 114 def optional_arg_error! identifier name = method.respond_to?(:name) ? method.name : 'anonymous' raise OptionalPositionalArgumentError.new( name, @opt_arg_name, identifier, ) end |
#set_unused_args ⇒ Object
90 91 92 93 94 |
# File 'lib/invokr/builder.rb', line 90 def set_unused_args @unused_args = injector.keys.flat_map do |hsh_arg| argument_names.include?(hsh_arg) ? [] : [hsh_arg] end end |