Class: Hinge::Resolver::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/hinge/resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, container) ⇒ Node

Returns a new instance of Node.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hinge/resolver.rb', line 56

def initialize(name, container)
  @name = name
  @method_name = "build_#{@name}"
  @method =
    begin
      container.method(@method_name)
    rescue NameError
      nil
    end

  if @method
    @dependency_names = []
    @positional_args_count = 0
    @keyword_arg_names = []

    @method.parameters.each do |kind, argument_name|
      argument_name = argument_name.to_sym
      @dependency_names << argument_name
      case kind
      when :req
        @positional_args_count += 1
      when :keyreq
        @keyword_arg_names << argument_name
      end
    end
  end
end

Instance Attribute Details

#dependency_namesObject (readonly)

Returns the value of attribute dependency_names.



83
84
85
# File 'lib/hinge/resolver.rb', line 83

def dependency_names
  @dependency_names
end

#methodObject (readonly)

Returns the value of attribute method.



83
84
85
# File 'lib/hinge/resolver.rb', line 83

def method
  @method
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



83
84
85
# File 'lib/hinge/resolver.rb', line 83

def method_name
  @method_name
end

#nameObject (readonly)

Returns the value of attribute name.



83
84
85
# File 'lib/hinge/resolver.rb', line 83

def name
  @name
end

Instance Method Details

#invoke(ordered_values) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/hinge/resolver.rb', line 85

def invoke(ordered_values)
  splat = ordered_values.take(@positional_args_count)
  splat << Hash[
    @keyword_arg_names.zip(ordered_values.drop(@positional_args_count))
  ] if @keyword_arg_names.any?
  @method.call(*splat)
end