-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathentity.rb
More file actions
188 lines (159 loc) · 4.85 KB
/
entity.rb
File metadata and controls
188 lines (159 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
require_relative 'dsl'
require_relative 'schema'
require_relative 'attribute'
require_relative 'validation'
require_relative 'helpers'
module JSON
module SchemaBuilder
class Entity
include DSL
include Attribute
include Validation
include Helpers
class_attribute :registered_type
attr_accessor :name, :parent, :children, :options, :fragment, :fragments, :error
attribute :title
attribute :description
attribute :type
attribute :default
attribute :enum, array: true
attribute :all_of, array: true
attribute :any_of, array: true
attribute :one_of, array: true
attribute :not_a, as: :not
attribute :ref, as: :$ref
attribute :definitions
def self.disable_attributes!(*attributes)
attributes.each do |attr|
undef_method attr rescue NameError
undef_method "#{attr}=" rescue NameError
end
end
def initialize(name, opts = { }, &block)
@name = name
@children = []
@fragments = Hash.new { |hash, key| hash[key] = ::Array.new }
@fragments["#/"] << self if opts[:root]
self.type = self.class.registered_type
initialize_parent_with opts
initialize_with opts
eval_block &block
any_of(null) if @nullable
extract_types
@initialized = true
end
def initialized?
!!@initialized
end
def reinitialize
end
def extend(child_name, &block)
children.find { |c| c.name == child_name.to_sym }.tap do |child|
raise "Property #{child_name} does not exist" unless child
child.eval_block(&block) if block_given?
end
end
def add_fragment(child)
@fragments[child.fragment] << child
parent.add_fragment(child) if @parent
end
def reset_fragment
@fragment = [@parent.fragment, name].compact.join("/").gsub(%r(//), "/")
root._reset_fragments
root.fragments["#/"] << root
end
def schema
@schema ||= Schema.new({}, self)
end
def required
schema["required"] || []
end
def required=(*values)
@parent.schema["required"] ||= []
@parent.schema["required"] << @name if values.any?
end
def merge_children!
return if any_of.present?
children.each do |child|
schema.merge! child.schema
end
end
def as_json
schema.as_json
end
def inspect
"#<#{self.class.name}:#{object_id} @schema=#{schema.as_json}>"
end
def respond_to?(method_name, include_all = false)
if @parent_context
@parent_context.respond_to? method_name, include_all
else
super
end
end
def method_missing(method_name, *args, &block)
if @parent_context && respond_to?(method_name, true)
@parent_context.send method_name, *args, &block
else
super
end
end
protected
def root
return @root if @root
node = self
node = node.parent while node.parent
@root = node
end
def _reset_fragments
@fragments = Hash.new { |hash, key| hash[key] = ::Array.new }
@fragment = if parent
[@parent.fragment, name].compact.join("/").gsub(%r(//), "/")
else
"#/"
end
children.each { |child| child._reset_fragments }
parent.add_fragment(self) if parent
end
def extract_types
build_any_of if any_of.present?
end
def build_any_of
initial_object = any_of_options.find { |opt| opt.as_json['type'] == 'object' }
everything_else = schema.data.except("anyOf")
return unless everything_else.present?
schema.data.keep_if { |k| k == "anyOf" }
return any_of_options.unshift(everything_else) unless initial_object
initial_object.deep_merge! everything_else
initial_object['properties'] = children.select { |c| c.name.presence }.map { |c| [c.name, c] }.to_h
end
def any_of_options
schema.data["anyOf"]
end
def initialize_parent_with(opts)
@parent = opts.delete :parent
if parent
@fragment = [@parent.fragment, name].compact.join("/").gsub(%r(//), "/")
parent.children << self
parent.add_fragment(self)
else
@fragment = "#/"
end
end
def initialize_with(opts)
@nullable = opts.delete :null
@options = opts.delete(:root).class.options.to_h if opts[:root]
opts.each_pair do |key, value|
next if value.nil?
send :"#{ key }=", value
end
end
def eval_block(&block)
if block_given?
@parent_context = block.binding.eval 'self'
instance_exec self, &block
end
end
end
end
end