Class: Teyu::ArgsSorter
- Inherits:
-
Object
- Object
- Teyu::ArgsSorter
- Defined in:
- lib/teyu.rb
Constant Summary collapse
- REQUIRED_SYMBOL =
'!'.freeze
Instance Method Summary collapse
-
#initialize(names) ⇒ ArgsSorter
constructor
A new instance of ArgsSorter.
-
#key_args ⇒ Hash
method(a: ‘a’, b: ‘b’) => { a: ‘a’, b: ‘b’ }.
-
#keyreq_args ⇒ Array<Symbol>
method(a!:, b!:) => [:a, :b].
-
#keyword_args ⇒ Array<Symbol>
method(a!:, b: ‘b’) => [:a, :b].
-
#req_args ⇒ Array<Symbol>
method(a, b) => [:a, :b].
Constructor Details
#initialize(names) ⇒ ArgsSorter
Returns a new instance of ArgsSorter.
62 63 64 |
# File 'lib/teyu.rb', line 62 def initialize(names) @names = names end |
Instance Method Details
#key_args ⇒ Hash
method(a: ‘a’, b: ‘b’) => { a: ‘a’, b: ‘b’ }
87 88 89 |
# File 'lib/teyu.rb', line 87 def key_args @key_args ||= @names.filter { |arg| arg.is_a?(Hash) }&.inject(:merge) || {} end |
#keyreq_args ⇒ Array<Symbol>
method(a!:, b!:) => [:a, :b]
80 81 82 83 |
# File 'lib/teyu.rb', line 80 def keyreq_args @keyreq_args ||= @names.map(&:to_s).filter { |arg| arg.end_with?(REQUIRED_SYMBOL) } .map { |arg| arg.delete_suffix(REQUIRED_SYMBOL).to_sym } end |
#keyword_args ⇒ Array<Symbol>
method(a!:, b: ‘b’) => [:a, :b]
74 75 76 |
# File 'lib/teyu.rb', line 74 def keyword_args @keyword_args ||= keyreq_args + key_args.keys end |
#req_args ⇒ Array<Symbol>
method(a, b) => [:a, :b]
68 69 70 |
# File 'lib/teyu.rb', line 68 def req_args @req_args ||= @names.take_while { |arg| !arg.is_a?(Hash) && !arg.to_s.end_with?(REQUIRED_SYMBOL) } end |