Module: Metaractor::Parameters::ClassMethods
- Defined in:
- lib/metaractor/parameters.rb
Instance Method Summary collapse
- #optional(*params, **options) ⇒ Object
- #parameter(name, **options) ⇒ Object
- #parameter_hash ⇒ Object
- #parameters(*names, **options) ⇒ Object
- #parameters_in_tree(tree) ⇒ Object
- #required(*params, **options) ⇒ Object
- #requirement_trees ⇒ Object
- #validate_hooks ⇒ Object
- #validate_parameters(*hooks, &block) ⇒ Object
Instance Method Details
#optional(*params, **options) ⇒ Object
101 102 103 |
# File 'lib/metaractor/parameters.rb', line 101 def optional(*params, **) parameters(*params, **) end |
#parameter(name, **options) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/metaractor/parameters.rb', line 67 def parameter(name, **) if (param = parameter_hash[name.to_sym]) param.merge!(**) else Parameter.new(name, **).tap do |parameter| parameter_hash[parameter.name] = parameter end end end |
#parameter_hash ⇒ Object
83 84 85 |
# File 'lib/metaractor/parameters.rb', line 83 def parameter_hash @parameters ||= {} end |
#parameters(*names, **options) ⇒ Object
77 78 79 80 81 |
# File 'lib/metaractor/parameters.rb', line 77 def parameters(*names, **) names.each do |name| parameter(name, **) end end |
#parameters_in_tree(tree) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/metaractor/parameters.rb', line 114 def parameters_in_tree(tree) if tree.respond_to?(:to_h) tree.to_h.values.first.to_a.flat_map { |t| parameters_in_tree(t) } else [tree] end end |
#required(*params, **options) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/metaractor/parameters.rb', line 91 def required(*params, **) if params.empty? tree = requirement_trees << tree parameters(*parameters_in_tree(tree), required: tree) else parameters(*params, required: true, **) end end |
#requirement_trees ⇒ Object
87 88 89 |
# File 'lib/metaractor/parameters.rb', line 87 def requirement_trees @requirement_trees ||= [] end |
#validate_hooks ⇒ Object
110 111 112 |
# File 'lib/metaractor/parameters.rb', line 110 def validate_hooks @validate_hooks ||= [] end |
#validate_parameters(*hooks, &block) ⇒ Object
105 106 107 108 |
# File 'lib/metaractor/parameters.rb', line 105 def validate_parameters(*hooks, &block) hooks << block if block hooks.each { |hook| validate_hooks.push(hook) } end |