Module: Metaractor::Parameters::ClassMethods

Defined in:
lib/metaractor/parameters.rb

Instance Method Summary collapse

Instance Method Details

#optional(*params, **options) ⇒ Object



101
102
103
# File 'lib/metaractor/parameters.rb', line 101

def optional(*params, **options)
  parameters(*params, **options)
end

#parameter(name, **options) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/metaractor/parameters.rb', line 67

def parameter(name, **options)
  if (param = parameter_hash[name.to_sym])
    param.merge!(**options)
  else
    Parameter.new(name, **options).tap do |parameter|
      parameter_hash[parameter.name] = parameter
    end
  end
end

#parameter_hashObject



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, **options)
  names.each do |name|
    parameter(name, **options)
  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, **options)
  if params.empty?
    tree = options
    requirement_trees << tree
    parameters(*parameters_in_tree(tree), required: tree)
  else
    parameters(*params, required: true, **options)
  end
end

#requirement_treesObject



87
88
89
# File 'lib/metaractor/parameters.rb', line 87

def requirement_trees
  @requirement_trees ||= []
end

#validate_hooksObject



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