Class: Marameters::Probe
- Inherits:
-
Object
- Object
- Marameters::Probe
- Extended by:
- Forwardable
- Defined in:
- lib/marameters/probe.rb
Overview
Provides information on a method’s parameters.
Constant Summary collapse
- CATEGORIES =
{positionals: %i[req opt], keywords: %i[keyreq key]}.freeze
Instance Attribute Summary collapse
-
#keywords ⇒ Object
readonly
Returns the value of attribute keywords.
-
#positionals ⇒ Object
readonly
Returns the value of attribute positionals.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
-
#initialize(parameters, categories: CATEGORIES) ⇒ Probe
constructor
A new instance of Probe.
- #keywords? ⇒ Boolean
- #keywords_for(*keys, **attributes) ⇒ Object
- #kind?(value) ⇒ Boolean
- #kinds ⇒ Object
- #name?(value) ⇒ Boolean
- #names ⇒ Object
- #only_bare_splats? ⇒ Boolean
- #only_double_splats? ⇒ Boolean
- #only_single_splats? ⇒ Boolean
- #positionals? ⇒ Boolean
- #positionals_and_maybe_keywords? ⇒ Boolean
Constructor Details
#initialize(parameters, categories: CATEGORIES) ⇒ Probe
Returns a new instance of Probe.
25 26 27 28 29 |
# File 'lib/marameters/probe.rb', line 25 def initialize parameters, categories: CATEGORIES @parameters = parameters categories.each { |category, kinds| define_variable category, kinds } freeze end |
Instance Attribute Details
#keywords ⇒ Object (readonly)
Returns the value of attribute keywords.
23 24 25 |
# File 'lib/marameters/probe.rb', line 23 def keywords @keywords end |
#positionals ⇒ Object (readonly)
Returns the value of attribute positionals.
23 24 25 |
# File 'lib/marameters/probe.rb', line 23 def positionals @positionals end |
Class Method Details
.of(klass, name, collection: []) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/marameters/probe.rb', line 12 def self.of klass, name, collection: [] method = klass.instance_method name collection << new(method.parameters) super_method = method.super_method of super_method.owner, super_method.name, collection: rescue NameError collection end |
Instance Method Details
#<=>(other) ⇒ Object
35 |
# File 'lib/marameters/probe.rb', line 35 def <=>(other) = to_a <=> other.to_a |
#==(other) ⇒ Object Also known as: eql?
31 |
# File 'lib/marameters/probe.rb', line 31 def ==(other) = hash == other.hash |
#keywords? ⇒ Boolean
37 |
# File 'lib/marameters/probe.rb', line 37 def keywords? = keywords.any? |
#keywords_for(*keys, **attributes) ⇒ Object
39 40 41 |
# File 'lib/marameters/probe.rb', line 39 def keywords_for(*keys, **attributes) attributes.select { |key| !keys.include?(key) || keywords.include?(key) } end |
#kind?(value) ⇒ Boolean
43 |
# File 'lib/marameters/probe.rb', line 43 def kind?(value) = parameters.any? { |kind, _name| kind == value } |
#kinds ⇒ Object
45 |
# File 'lib/marameters/probe.rb', line 45 def kinds = parameters.map { |kind, _name| kind } |
#name?(value) ⇒ Boolean
47 |
# File 'lib/marameters/probe.rb', line 47 def name?(value) = parameters.any? { |_kind, name| name == value } |
#names ⇒ Object
49 |
# File 'lib/marameters/probe.rb', line 49 def names = parameters.map { |_kind, name| name } |
#only_bare_splats? ⇒ Boolean
51 52 53 54 55 56 57 58 |
# File 'lib/marameters/probe.rb', line 51 def parameters in [[:rest]] \ | [[:rest, :*]] \ | [[:keyrest]] \ | [[:keyrest, :**]] \ | [[:rest], [:keyrest]] \ | [[:rest, :*], [:keyrest, :**]] end |
#only_double_splats? ⇒ Boolean
60 |
# File 'lib/marameters/probe.rb', line 60 def only_double_splats? = (parameters in [[:keyrest]] | [[:keyrest, *]]) |
#only_single_splats? ⇒ Boolean
62 |
# File 'lib/marameters/probe.rb', line 62 def only_single_splats? = (parameters in [[:rest]] | [[:rest, *]]) |
#positionals? ⇒ Boolean
64 |
# File 'lib/marameters/probe.rb', line 64 def positionals? = positionals.any? |
#positionals_and_maybe_keywords? ⇒ Boolean
66 67 68 |
# File 'lib/marameters/probe.rb', line 66 def positionals_and_maybe_keywords? (positionals? && !keywords?) || (positionals? && keywords?) end |