Class: Parametric::Field

Inherits:
Object
  • Object
show all
Includes:
FieldDSL
Defined in:
lib/parametric/field.rb

Defined Under Namespace

Classes: 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.



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

def initialize(key, registry = Parametric.registry)
  @key = key
  @policies = []
  @registry = registry
  @default_block = nil
  @meta_data = {}
  @policies = []
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#meta_dataObject (readonly)

Returns the value of attribute meta_data.



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

def 
  @meta_data
end

Instance Method Details

#default(value) ⇒ Object



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

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

#meta(hash = nil) ⇒ Object



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

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



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

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

#resolve(payload, context) ⇒ Object



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
85
86
# File 'lib/parametric/field.rb', line 57

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



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

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

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



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

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