Class: Paradocs::Field
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from FieldDSL
#as, #declared, #description, #example, #length, #options, #present, #required, #transparent, #whitelisted
Constructor Details
#initialize(key) ⇒ Field
Returns a new instance of Field.
10
11
12
13
14
15
16
17
18
|
# File 'lib/paradocs/field.rb', line 10
def initialize(key)
@key = key
@policies = []
@default_block = nil
@meta_data = {}
@policies = []
@mutation_block = nil
@expects_mutation = nil
end
|
Instance Attribute Details
Returns the value of attribute key.
7
8
9
|
# File 'lib/paradocs/field.rb', line 7
def key
@key
end
|
Returns the value of attribute meta_data.
7
8
9
|
# File 'lib/paradocs/field.rb', line 7
def meta_data
@meta_data
end
|
Instance Method Details
#default(value) ⇒ Object
29
30
31
32
33
|
# File 'lib/paradocs/field.rb', line 29
def default(value)
meta default: value
@default_block = (value.respond_to?(:call) ? value : ->(key, payload, context) { value })
self
end
|
#expects_mutation? ⇒ Boolean
46
47
48
|
# File 'lib/paradocs/field.rb', line 46
def expects_mutation?
mutates_schema? && @expects_mutation
end
|
20
21
22
23
|
# File 'lib/paradocs/field.rb', line 20
def meta(hash = nil)
@meta_data = @meta_data.merge(hash) if hash.is_a?(Hash)
self
end
|
#mutates_schema!(&block) ⇒ Object
35
36
37
38
39
40
|
# File 'lib/paradocs/field.rb', line 35
def mutates_schema!(&block)
@mutation_block ||= block if block_given?
@expects_mutation = @expects_mutation.nil? && true
meta mutates_schema: @mutation_block
@mutation_block
end
|
#mutates_schema? ⇒ Boolean
42
43
44
|
# File 'lib/paradocs/field.rb', line 42
def mutates_schema?
!!@mutation_block
end
|
#policy(key, *args) ⇒ Object
Also known as:
type, rule
50
51
52
53
54
55
56
|
# File 'lib/paradocs/field.rb', line 50
def policy(key, *args)
pol = lookup(key, args)
meta pol.meta_data
policies << pol
self
end
|
#possible_errors ⇒ Object
25
26
27
|
# File 'lib/paradocs/field.rb', line 25
def possible_errors
meta_data.map { |_, v| v[:errors] if v.is_a?(Hash) }.flatten.compact
end
|
#resolve(payload, context) ⇒ Object
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/paradocs/field.rb', line 86
def resolve(payload, context)
eligible = payload.key?(key)
value = payload[key]
if !eligible && has_default?
eligible = true
value = default_block.call(key, payload, context)
payload[key] = value
end
policies.each do |policy|
policy.environment = context.environment if policy.respond_to?(:environment=)
if !policy.eligible?(value, key, payload)
eligible = false
if has_default?
eligible = true
value = default_block.call(key, payload, context)
end
break
else
value, valid = resolve_one(policy, value, payload, context)
unless valid
eligible = true break end
end
end
Result.new(eligible, value)
end
|
#schema(sc = nil, &block) ⇒ Object
61
62
63
64
65
|
# File 'lib/paradocs/field.rb', line 61
def schema(sc = nil, &block)
sc = (sc ? sc : Schema.new(&block))
meta schema: sc
policy sc.schema
end
|
#subschema_for_mutation(payload, env) ⇒ Object
80
81
82
83
84
|
# File 'lib/paradocs/field.rb', line 80
def subschema_for_mutation(payload, env)
subschema_name = @mutation_block.call(payload[key], key, payload, env) if @mutation_block
@expects_mutation = false
subschema_name
end
|
#transparent? ⇒ Boolean
67
68
69
|
# File 'lib/paradocs/field.rb', line 67
def transparent?
!!meta_data[:transparent]
end
|
#visit(meta_key = nil, &visitor) ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'lib/paradocs/field.rb', line 71
def visit(meta_key = nil, &visitor)
if sc = meta_data[:schema]
r = sc.visit(meta_key, &visitor)
(meta_data[:type] == :array) ? [r] : r
else
meta_key ? meta_data[meta_key] : yield(self)
end
end
|