Class: Paradocs::Schema

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

Defined Under Namespace

Classes: MatchContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Schema.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/paradocs/schema.rb', line 11

def initialize(options={}, &block)
  @options = options
  @fields = {}
  @subschemes = {}
  @definitions = []
  @definitions << block if block_given?
  @default_field_policies = []
  @ignored_field_keys = []
  @expansions = {}
  @structure_builder = Paradocs::Extensions::Structure.new(self)
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



9
10
11
# File 'lib/paradocs/schema.rb', line 9

def environment
  @environment
end

#structure_builderObject (readonly)

Returns the value of attribute structure_builder.



10
11
12
# File 'lib/paradocs/schema.rb', line 10

def structure_builder
  @structure_builder
end

#subschemesObject (readonly)

Returns the value of attribute subschemes.



10
11
12
# File 'lib/paradocs/schema.rb', line 10

def subschemes
  @subschemes
end

Instance Method Details

#cloneObject



90
91
92
93
# File 'lib/paradocs/schema.rb', line 90

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

#coerce(val, _, context) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/paradocs/schema.rb', line 153

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

#copy_into(instance) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/paradocs/schema.rb', line 102

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

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

  subschemes.each { |name, subsc| instance.subschema(name, subsc) }

  instance.ignore *ignored_field_keys
  instance
end

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

Returns:

  • (Boolean)


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

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

#example_payloads(&block) ⇒ Object



38
39
40
# File 'lib/paradocs/schema.rb', line 38

def example_payloads(&block)
  @example_payloads ||= Paradocs::Extensions::PayloadBuilder.new(self).build!(&block)
end

#expand(exp, &block) ⇒ Object



129
130
131
132
# File 'lib/paradocs/schema.rb', line 129

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

#field(field_or_key) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/paradocs/schema.rb', line 115

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



69
70
71
72
# File 'lib/paradocs/schema.rb', line 69

def fields
  apply!
  @fields
end

#flush!Object



167
168
169
170
# File 'lib/paradocs/schema.rb', line 167

def flush!
  @fields = {}
  @applied = false
end

#ignore(*field_keys, &block) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/paradocs/schema.rb', line 81

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

  definitions << block if block_given?

  self
end

#merge(other_schema) ⇒ Object



95
96
97
98
99
100
# File 'lib/paradocs/schema.rb', line 95

def merge(other_schema)
  instance = self.class.new(options.merge(other_schema.options))

  copy_into(instance)
  other_schema.copy_into(instance)
end

#meta_dataObject



149
150
151
# File 'lib/paradocs/schema.rb', line 149

def 
  {}
end

#mutation_by!(key, &block) ⇒ Object



27
28
29
30
# File 'lib/paradocs/schema.rb', line 27

def mutation_by!(key, &block)
  f = @fields.keys.include?(key) ? @fields[key] : field(key).transparent
  f.mutates_schema!(&block)
end

#policy(*names, &block) ⇒ Object



74
75
76
77
78
79
# File 'lib/paradocs/schema.rb', line 74

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

  self
end

#resolve(payload, environment = {}) ⇒ Object



134
135
136
137
138
139
# File 'lib/paradocs/schema.rb', line 134

def resolve(payload, environment={})
  @environment = environment
  context = Context.new(nil, Top.new, @environment, subschemes)
  output = coerce(payload, nil, context)
  Results.new(output, context.errors, @environment)
end

#schemaObject



23
24
25
# File 'lib/paradocs/schema.rb', line 23

def schema
  self
end

#structure(ignore_transparent: true) ⇒ Object



32
33
34
35
36
# File 'lib/paradocs/schema.rb', line 32

def structure(ignore_transparent: true)
  flush!
  structure_builder.ignore_transparent = ignore_transparent
  structure_builder
end

#subschema(*args, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/paradocs/schema.rb', line 53

def subschema(*args, &block)
  options = args.last.is_a?(Hash) ? args.last : {}
  name = args.first.is_a?(Symbol) ? args.shift : Paradocs.config.default_schema_name
  current_schema = subschemes.fetch(name) { self.class.new }
  new_schema = if block_given?
    sc = self.class.new(options)
    sc.definitions << block
    sc
  elsif args.first.is_a?(self.class)
    args.first
  else
    self.class.new(options)
  end
  subschemes[name] = current_schema.merge(new_schema)
end

#valid?(*_) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/paradocs/schema.rb', line 145

def valid?(*_)
  true
end

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



47
48
49
50
51
# File 'lib/paradocs/schema.rb', line 47

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



42
43
44
45
# File 'lib/paradocs/schema.rb', line 42

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