Class: Parametric::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/parametric/schema.rb

Defined Under Namespace

Classes: MatchContext

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Schema

Returns a new instance of Schema.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/parametric/schema.rb', line 9

def initialize(options = {}, &block)
  @options = options
  @fields = {}
  @definitions = []
  @definitions << block if block_given?
  @default_field_policies = []
  @ignored_field_keys = []
  @expansions = {}
  @before_hooks = []
  @after_hooks = []
end

Instance Method Details

#==(other) ⇒ Object



21
22
23
# File 'lib/parametric/schema.rb', line 21

def ==(other)
  other.is_a?(Schema) && fields == other.fields
end

#after_resolve(klass = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


110
111
112
113
114
115
# File 'lib/parametric/schema.rb', line 110

def after_resolve(klass = nil, &block)
  raise ArgumentError, '#after_resolve expects a callable object, or a block' if !klass && !block_given?
  callable = klass || block
  after_hooks << callable
  self
end

#before_resolve(klass = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


103
104
105
106
107
108
# File 'lib/parametric/schema.rb', line 103

def before_resolve(klass = nil, &block)
  raise ArgumentError, '#before_resolve expects a callable object, or a block' if !klass && !block_given?
  callable = klass || block
  before_hooks << callable
  self
end

#cloneObject



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

def clone
  instance = self.class.new(options)
  copy_into instance
end

#coerce(val, _, context) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/parametric/schema.rb', line 151

def coerce(val, _, context)
  if val.is_a?(Array)
    val.map.with_index{|v, idx|
      subcontext = context.sub(idx)
      out = coerce_one(v, subcontext)
      resolve_expansions(v, out, subcontext)
    }
  else
    out = coerce_one(val, context)
    resolve_expansions(val, out, context)
  end
end

#copy_into(instance) ⇒ Object



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

def copy_into(instance)
  instance.policy(*default_field_policies) if default_field_policies.any?

  definitions.each do |d|
    instance.definitions << d
  end

  instance.ignore *ignored_field_keys
  instance
end

#eligible?(value, key, payload) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/parametric/schema.rb', line 133

def eligible?(value, key, payload)
  payload.key? key
end

#expand(exp, &block) ⇒ Object



117
118
119
120
# File 'lib/parametric/schema.rb', line 117

def expand(exp, &block)
  expansions[exp] = block
  self
end

#field(field_or_key) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/parametric/schema.rb', line 89

def field(field_or_key)
  f, key = if field_or_key.kind_of?(Field)
    [field_or_key, field_or_key.key]
  else
    [Field.new(field_or_key), field_or_key.to_sym]
  end

  if ignored_field_keys.include?(f.key)
    f
  else
    @fields[key] = apply_default_field_policies_to(f)
  end
end

#fieldsObject



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

def fields
  apply!
  @fields
end

#ignore(*field_keys, &block) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/parametric/schema.rb', line 41

def ignore(*field_keys, &block)
  @ignored_field_keys += field_keys
  @ignored_field_keys.uniq!

  definitions << block if block_given?

  self
end

#merge(other_schema = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/parametric/schema.rb', line 55

def merge(other_schema = nil, &block)
  raise ArgumentError, '#merge takes either a schema instance or a block' if other_schema.nil? && !block_given?

  if other_schema
    instance = self.class.new(options.merge(other_schema.options))
    copy_into(instance)
    other_schema.copy_into(instance)
  else
    merge(self.class.new(&block))
  end
end

#meta_dataObject



141
142
143
# File 'lib/parametric/schema.rb', line 141

def 
  {}
end

#policy(*names, &block) ⇒ Object



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

def policy(*names, &block)
  @default_field_policies = names
  definitions << block if block_given?

  self
end

#resolve(payload) ⇒ Object



122
123
124
125
126
# File 'lib/parametric/schema.rb', line 122

def resolve(payload)
  context = Context.new
  output = coerce(payload, nil, context)
  Results.new(output, context.errors)
end

#schemaObject



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

def schema
  self
end

#structureObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/parametric/schema.rb', line 78

def structure
  fields.each_with_object({}) do |(_, field), obj|
    meta = field..dup
    sc = meta.delete(:schema)
    meta[:structure] = sc.schema.structure if sc
    one_of = meta.delete(:one_of)
    meta[:one_of] = one_of.values.map(&:structure) if one_of
    obj[field.key] = meta
  end
end

#valid?(*_) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/parametric/schema.rb', line 137

def valid?(*_)
  true
end

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



145
146
147
148
149
# File 'lib/parametric/schema.rb', line 145

def visit(meta_key = nil, &visitor)
  fields.each_with_object({}) do |(_, field), m|
    m[field.key] = field.visit(meta_key, &visitor)
  end
end

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



128
129
130
131
# File 'lib/parametric/schema.rb', line 128

def walk(meta_key = nil, &visitor)
  r = visit(meta_key, &visitor)
  Results.new(r, {})
end