Class: Parametric::Schema
- Inherits:
-
Object
- Object
- Parametric::Schema
- Defined in:
- lib/parametric/schema.rb
Defined Under Namespace
Classes: MatchContext
Instance Method Summary collapse
- #==(other) ⇒ Object
- #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) @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
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
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 |
#clone ⇒ Object
50 51 52 53 |
# File 'lib/parametric/schema.rb', line 50 def clone instance = self.class.new() 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
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 (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 |
#fields ⇒ Object
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
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(.merge(other_schema.)) copy_into(instance) other_schema.copy_into(instance) else merge(self.class.new(&block)) end end |
#meta_data ⇒ Object
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 |
#schema ⇒ Object
25 26 27 |
# File 'lib/parametric/schema.rb', line 25 def schema self end |
#structure ⇒ Object
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| = 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
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( = nil, &visitor) fields.each_with_object({}) do |(_, field), m| m[field.key] = field.visit(, &visitor) end end |