Class: Parametric::Field

Inherits:
Object
  • 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
   = {}
  @policies = []
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



14
15
16
# File 'lib/parametric/field.rb', line 14

def key
  @key
end

#meta_dataObject (readonly)

Returns the value of attribute meta_data.



14
15
16
# File 'lib/parametric/field.rb', line 14

def 
  
end

Instance Method Details

#default(value) ⇒ Object



31
32
33
34
35
# File 'lib/parametric/field.rb', line 31

def default(value)
  meta default: value
  @default_block = (value.respond_to?(:call) ? value : ->(key, payload, context) { value })
  self
end

#from(another_field) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/parametric/field.rb', line 55

def from(another_field)
  meta another_field.
  another_field.policies.each do |plc|
    policies << plc
  end

  self
end

#has_policy?(key) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/parametric/field.rb', line 64

def has_policy?(key)
  policies.any? { |pol| pol.key == key }
end

#meta(hash = nil) ⇒ Object



26
27
28
29
# File 'lib/parametric/field.rb', line 26

def meta(hash = nil)
   = .merge(hash) if hash.is_a?(Hash)
  self
end

#policy(key, *args) ⇒ Object Also known as: type



37
38
39
40
41
42
# File 'lib/parametric/field.rb', line 37

def policy(key, *args)
  pol = lookup(key, args)
  meta pol.
  policies << pol
  self
end

#resolve(payload, context) ⇒ Object



77
78
79
80
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
# File 'lib/parametric/field.rb', line 77

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|
    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 # eligible, but has errors
          context.add_error pol.message
          break # only one error at a time
        end
      end
    rescue StandardError => e
      context.add_error e.message
      break
    end
  end

  Result.new(eligible, value)
end

#schema(sc = nil, &block) ⇒ Object



49
50
51
52
53
# File 'lib/parametric/field.rb', line 49

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



45
46
47
# File 'lib/parametric/field.rb', line 45

def tagged_one_of(instance = nil, &block)
  policy(instance || Parametric::TaggedOneOf.new(&block))
end

#visit(meta_key = nil, &visitor) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/parametric/field.rb', line 68

def visit(meta_key = nil, &visitor)
  if sc = [:schema]
    r = sc.schema.visit(meta_key, &visitor)
    ([:type] == :array) ? [r] : r
  else
    meta_key ? [meta_key] : yield(self)
  end
end