Class: Parametric::Field
- Inherits:
-
Object
show all
- Includes:
- FieldDSL
- Defined in:
- lib/parametric/field.rb
Defined Under Namespace
Classes: PolicyWithKey, Result
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from FieldDSL
#declared, #nullable, #options, #present, #required
Constructor Details
#initialize(key, registry = Parametric.registry) ⇒ Field
Returns a new instance of Field.
17
18
19
20
21
22
23
24
|
# File 'lib/parametric/field.rb', line 17
def initialize(key, registry = Parametric.registry)
@key = key
@policies = []
@registry = registry
@default_block = nil
@meta_data = {}
@policies = []
end
|
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
14
15
16
|
# File 'lib/parametric/field.rb', line 14
def key
@key
end
|
Returns the value of attribute meta_data.
14
15
16
|
# File 'lib/parametric/field.rb', line 14
def meta_data
@meta_data
end
|
Instance Method Details
#==(other) ⇒ Object
26
27
28
|
# File 'lib/parametric/field.rb', line 26
def ==(other)
other.is_a?(Field) && key == other.key && policies == other.policies && meta_data == other.meta_data
end
|
#default(value) ⇒ Object
35
36
37
38
39
|
# File 'lib/parametric/field.rb', line 35
def default(value)
meta default: value
@default_block = (value.respond_to?(:call) ? value : ->(key, payload, context) { value })
self
end
|
#from(another_field) ⇒ Object
59
60
61
62
63
64
65
66
|
# File 'lib/parametric/field.rb', line 59
def from(another_field)
meta another_field.meta_data
another_field.policies.each do |plc|
policies << plc
end
self
end
|
#has_policy?(key) ⇒ Boolean
68
69
70
|
# File 'lib/parametric/field.rb', line 68
def has_policy?(key)
policies.any? { |pol| pol.key == key }
end
|
30
31
32
33
|
# File 'lib/parametric/field.rb', line 30
def meta(hash = nil)
@meta_data = @meta_data.merge(hash) if hash.is_a?(Hash)
self
end
|
#policy(key, *args) ⇒ Object
Also known as:
type
41
42
43
44
45
46
|
# File 'lib/parametric/field.rb', line 41
def policy(key, *args)
pol = lookup(key, args)
meta pol.meta_data
policies << pol
self
end
|
#resolve(payload, context) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/parametric/field.rb', line 81
def resolve(payload, context)
eligible = payload.key?(key)
value = payload[key]
if !eligible && has_default?
eligible = true
value = default_block.call(key, payload, context)
return Result.new(eligible, value)
end
policies.each do |policy|
begin
pol = policy.build(key, value, payload:, context:)
if !pol.eligible?
eligible = pol.include_non_eligible_in_ouput?
if has_default?
eligible = true
value = default_block.call(key, payload, context)
end
break
else
value = pol.value
if !pol.valid?
eligible = true context.add_error pol.message
break end
end
rescue StandardError => e
context.add_error e.message
break
end
end
Result.new(eligible, value)
end
|
#schema(sc = nil, &block) ⇒ Object
53
54
55
56
57
|
# File 'lib/parametric/field.rb', line 53
def schema(sc = nil, &block)
sc = (sc ? sc : Schema.new(&block))
meta schema: sc
policy sc.schema
end
|
#tagged_one_of(instance = nil, &block) ⇒ Object
49
50
51
|
# File 'lib/parametric/field.rb', line 49
def tagged_one_of(instance = nil, &block)
policy(instance || Parametric::TaggedOneOf.new(&block))
end
|
#visit(meta_key = nil, &visitor) ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/parametric/field.rb', line 72
def visit(meta_key = nil, &visitor)
if sc = meta_data[:schema]
r = sc.schema.visit(meta_key, &visitor)
(meta_data[:type] == :array) ? [r] : r
else
meta_key ? meta_data[meta_key] : yield(self)
end
end
|