Class: Teyu::ArgsSorter

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

Constant Summary collapse

REQUIRED_SYMBOL =
'!'.freeze

Instance Method Summary collapse

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_argsHash

method(a: ‘a’, b: ‘b’) => { a: ‘a’, b: ‘b’ }

Returns:

  • (Hash)

    keyword args with default value



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_argsArray<Symbol>

method(a!:, b!:) => [:a, :b]

Returns:

  • (Array<Symbol>)

    keyreq arg names



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_argsArray<Symbol>

method(a!:, b: ‘b’) => [:a, :b]

Returns:

  • (Array<Symbol>)

    keyword arg names



74
75
76
# File 'lib/teyu.rb', line 74

def keyword_args
  @keyword_args ||= keyreq_args + key_args.keys
end

#req_argsArray<Symbol>

method(a, b) => [:a, :b]

Returns:

  • (Array<Symbol>)

    req arg names



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