Class: Parametric::Schema
- Inherits:
-
Object
- Object
- Parametric::Schema
- Defined in:
- lib/parametric/schema.rb
Defined Under Namespace
Classes: MatchContext
Instance Method Summary collapse
- #after_resolve(klass = nil, &block) ⇒ Object
- #before_resolve(klass = nil, &block) ⇒ Object
- #clone ⇒ Object
- #coerce(val, _, context) ⇒ Object
- #copy_into(instance) ⇒ Object
- #eligible?(value, key, payload) ⇒ Boolean
- #expand(exp, &block) ⇒ Object
- #field(field_or_key) ⇒ Object
- #fields ⇒ Object
- #ignore(*field_keys, &block) ⇒ Object
-
#initialize(options = {}, &block) ⇒ Schema
constructor
A new instance of Schema.
- #merge(other_schema = nil, &block) ⇒ Object
- #meta_data ⇒ Object
- #policy(*names, &block) ⇒ Object
- #resolve(payload) ⇒ Object
- #schema ⇒ Object
- #structure ⇒ Object
- #valid?(*_) ⇒ Boolean
- #visit(meta_key = nil, &visitor) ⇒ Object
- #walk(meta_key = nil, &visitor) ⇒ Object
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( = {}, &block) = @fields = {} @definitions = [] @definitions << block if block_given? @default_field_policies = [] @ignored_field_keys = [] @expansions = {} @before_hooks = [] @after_hooks = [] end |
Instance Method Details
#after_resolve(klass = nil, &block) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/parametric/schema.rb', line 106 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
99 100 101 102 103 104 |
# File 'lib/parametric/schema.rb', line 99 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 |
#clone ⇒ Object
46 47 48 49 |
# File 'lib/parametric/schema.rb', line 46 def clone instance = self.class.new() copy_into instance end |
#coerce(val, _, context) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/parametric/schema.rb', line 147 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
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/parametric/schema.rb', line 63 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
129 130 131 |
# File 'lib/parametric/schema.rb', line 129 def eligible?(value, key, payload) payload.key? key end |
#expand(exp, &block) ⇒ Object
113 114 115 116 |
# File 'lib/parametric/schema.rb', line 113 def (exp, &block) expansions[exp] = block self end |
#field(field_or_key) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/parametric/schema.rb', line 85 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 |
#fields ⇒ Object
25 26 27 28 |
# File 'lib/parametric/schema.rb', line 25 def fields apply! @fields end |
#ignore(*field_keys, &block) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/parametric/schema.rb', line 37 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
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/parametric/schema.rb', line 51 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(.merge(other_schema.)) copy_into(instance) other_schema.copy_into(instance) else merge(self.class.new(&block)) end end |
#meta_data ⇒ Object
137 138 139 |
# File 'lib/parametric/schema.rb', line 137 def {} end |
#policy(*names, &block) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/parametric/schema.rb', line 30 def policy(*names, &block) @default_field_policies = names definitions << block if block_given? self end |
#resolve(payload) ⇒ Object
118 119 120 121 122 |
# File 'lib/parametric/schema.rb', line 118 def resolve(payload) context = Context.new output = coerce(payload, nil, context) Results.new(output, context.errors) end |
#schema ⇒ Object
21 22 23 |
# File 'lib/parametric/schema.rb', line 21 def schema self end |
#structure ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/parametric/schema.rb', line 74 def structure fields.each_with_object({}) do |(_, field), obj| = field..dup sc = .delete(:schema) [:structure] = sc.schema.structure if sc one_of = .delete(:one_of) [:one_of] = one_of.values.map(&:structure) if one_of obj[field.key] = end end |
#valid?(*_) ⇒ Boolean
133 134 135 |
# File 'lib/parametric/schema.rb', line 133 def valid?(*_) true end |
#visit(meta_key = nil, &visitor) ⇒ Object
141 142 143 144 145 |
# File 'lib/parametric/schema.rb', line 141 def visit( = nil, &visitor) fields.each_with_object({}) do |(_, field), m| m[field.key] = field.visit(, &visitor) end end |
#walk(meta_key = nil, &visitor) ⇒ Object
124 125 126 127 |
# File 'lib/parametric/schema.rb', line 124 def walk( = nil, &visitor) r = visit(, &visitor) Results.new(r, {}) end |