Class: Parametric::Field
- Inherits:
-
Object
- Object
- Parametric::Field
- Includes:
- FieldDSL
- Defined in:
- lib/parametric/field.rb
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#meta_data ⇒ Object
readonly
Returns the value of attribute meta_data.
Instance Method Summary collapse
- #default(value) ⇒ Object
-
#initialize(key, registry = Parametric.registry) ⇒ Field
constructor
A new instance of Field.
- #meta(hash = nil) ⇒ Object
- #policy(key, *args) ⇒ Object (also: #type)
- #resolve(payload, context) ⇒ Object
- #schema(sc = nil, &block) ⇒ Object
- #visit(meta_key = nil, &visitor) ⇒ Object
Methods included from FieldDSL
#declared, #options, #present, #required
Constructor Details
#initialize(key, registry = Parametric.registry) ⇒ Field
Returns a new instance of Field.
12 13 14 15 16 17 18 19 |
# File 'lib/parametric/field.rb', line 12 def initialize(key, registry = Parametric.registry) @key = key @policies = [] @registry = registry @default_block = nil @meta_data = {} @policies = [] end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/parametric/field.rb', line 9 def key @key end |
#meta_data ⇒ Object (readonly)
Returns the value of attribute meta_data.
9 10 11 |
# File 'lib/parametric/field.rb', line 9 def @meta_data end |
Instance Method Details
#default(value) ⇒ Object
26 27 28 29 30 |
# File 'lib/parametric/field.rb', line 26 def default(value) default: value @default_block = (value.respond_to?(:call) ? value : ->(key, payload, context) { value }) self end |
#meta(hash = nil) ⇒ Object
21 22 23 24 |
# File 'lib/parametric/field.rb', line 21 def (hash = nil) @meta_data = @meta_data.merge(hash) if hash.is_a?(Hash) self end |
#policy(key, *args) ⇒ Object Also known as: type
32 33 34 35 36 37 |
# File 'lib/parametric/field.rb', line 32 def policy(key, *args) pol = lookup(key, args) pol. policies << pol self end |
#resolve(payload, context) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/parametric/field.rb', line 55 def resolve(payload, context) eligible = payload.key?(key) value = payload[key] # might be nil if !eligible && has_default? eligible = true value = default_block.call(key, payload, context) return Result.new(eligible, value) end policies.each do |policy| if !policy.eligible?(value, key, payload) eligible = false if has_default? eligible = true value = default_block.call(key, payload, context) end break else value = resolve_one(policy, value, context) if !policy.valid?(value, key, payload) eligible = true # eligible, but has errors context.add_error policy. break # only one error at a time end end end Result.new(eligible, value) end |
#schema(sc = nil, &block) ⇒ Object
40 41 42 43 44 |
# File 'lib/parametric/field.rb', line 40 def schema(sc = nil, &block) sc = (sc ? sc : Schema.new(&block)) schema: sc policy sc end |
#visit(meta_key = nil, &visitor) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/parametric/field.rb', line 46 def visit( = nil, &visitor) if sc = [:schema] r = sc.visit(, &visitor) ([:type] == :array) ? [r] : r else ? [] : yield(self) end end |