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, #options, #present, #required

Constructor Details

#initialize(key, registry = Parametric.registry) ⇒ Field

Returns a new instance of Field.



15
16
17
18
19
20
21
22
# File 'lib/parametric/field.rb', line 15

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.



12
13
14
# File 'lib/parametric/field.rb', line 12

def key
  @key
end

#meta_dataObject (readonly)

Returns the value of attribute meta_data.



12
13
14
# File 'lib/parametric/field.rb', line 12

def 
  
end

Instance Method Details

#default(value) ⇒ Object



29
30
31
32
33
# File 'lib/parametric/field.rb', line 29

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

#from(another_field) ⇒ Object



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

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

  self
end

#has_policy?(key) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/parametric/field.rb', line 58

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

#meta(hash = nil) ⇒ Object



24
25
26
27
# File 'lib/parametric/field.rb', line 24

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

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



35
36
37
38
39
40
# File 'lib/parametric/field.rb', line 35

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

#resolve(payload, context) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/parametric/field.rb', line 71

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.message
        break # only one error at a time
      end
    end
  end

  Result.new(eligible, value)
end

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



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

def schema(sc = nil, &block)
  sc = (sc ? sc : Schema.new(&block))
  meta schema: sc
  policy sc.schema
end

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



62
63
64
65
66
67
68
69
# File 'lib/parametric/field.rb', line 62

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