Class: JsonSchema::Schema
- Inherits:
-
Object
- Object
- JsonSchema::Schema
- Defined in:
- lib/json_schema/schema.rb
Defined Under Namespace
Constant Summary collapse
- @@copyable =
[]
Instance Attribute Summary collapse
-
#fragment ⇒ Object
Fragment of a JSON Pointer that can help us build a pointer back to this schema for debugging.
-
#reference ⇒ Object
Rather than a normal schema, the node may be a JSON Reference.
Class Method Summary collapse
-
.attr_copyable(attr) ⇒ Object
identical to attr_accessible, but allows us to copy in values from a target schema to help preserve our hierarchy during reference expansion.
- .attr_reader_default(attr, default) ⇒ Object
Instance Method Summary collapse
- #copy_from(schema) ⇒ Object
- #expand_references(options = {}) ⇒ Object
- #expand_references!(options = {}) ⇒ Object
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
- #inspect ⇒ Object
- #inspect_schema ⇒ Object
- #inspect_value(value) ⇒ Object
- #original? ⇒ Boolean
- #pointer ⇒ Object
- #validate(data) ⇒ Object
- #validate!(data) ⇒ Object
Constructor Details
#initialize ⇒ Schema
Returns a new instance of Schema.
18 19 20 |
# File 'lib/json_schema/schema.rb', line 18 def initialize @clones = Set.new end |
Instance Attribute Details
#fragment ⇒ Object
Fragment of a JSON Pointer that can help us build a pointer back to this schema for debugging.
24 25 26 |
# File 'lib/json_schema/schema.rb', line 24 def fragment @fragment end |
#reference ⇒ Object
Rather than a normal schema, the node may be a JSON Reference. In this case, no other attributes will be filled in except for #parent.
28 29 30 |
# File 'lib/json_schema/schema.rb', line 28 def reference @reference end |
Class Method Details
.attr_copyable(attr) ⇒ Object
identical to attr_accessible, but allows us to copy in values from a target schema to help preserve our hierarchy during reference expansion
9 10 11 12 |
# File 'lib/json_schema/schema.rb', line 9 def self.attr_copyable(attr) attr_accessor(attr) @@copyable << "@#{attr}".to_sym end |
.attr_reader_default(attr, default) ⇒ Object
14 15 16 |
# File 'lib/json_schema/schema.rb', line 14 def self.attr_reader_default(attr, default) class_eval("def #{attr} ; !@#{attr}.nil? ? @#{attr} : #{default} ; end") end |
Instance Method Details
#copy_from(schema) ⇒ Object
197 198 199 200 201 |
# File 'lib/json_schema/schema.rb', line 197 def copy_from(schema) @@copyable.each do |copyable| instance_variable_set(copyable, schema.instance_variable_get(copyable)) end end |
#expand_references(options = {}) ⇒ Object
203 204 205 206 207 208 209 210 |
# File 'lib/json_schema/schema.rb', line 203 def ( = {}) = ReferenceExpander.new if .(self, ) [true, nil] else [false, .errors] end end |
#expand_references!(options = {}) ⇒ Object
212 213 214 215 |
# File 'lib/json_schema/schema.rb', line 212 def ( = {}) ReferenceExpander.new.(self, ) true end |
#inspect ⇒ Object
217 218 219 |
# File 'lib/json_schema/schema.rb', line 217 def inspect "\#<JsonSchema::Schema pointer=#{pointer}>" end |
#inspect_schema ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/json_schema/schema.rb', line 221 def inspect_schema if reference str = reference.to_s str += ? " [EXPANDED]" : " [COLLAPSED]" str += original? ? " [ORIGINAL]" : " [CLONE]" str else hash = {} @@copyable.each do |copyable| next if [:@clones, :@data, :@parent, :@uri].include?(copyable) if value = instance_variable_get(copyable) if value.is_a?(Array) if !value.empty? hash[copyable] = value.map { |v| inspect_value(v) } end elsif value.is_a?(Hash) if !value.empty? hash[copyable] = Hash[*value.map { |k, v| [k, inspect_value(v)] }.flatten] end else hash[copyable] = inspect_value(value) end end end hash end end |
#inspect_value(value) ⇒ Object
250 251 252 253 254 255 256 |
# File 'lib/json_schema/schema.rb', line 250 def inspect_value(value) if value.is_a?(Schema) value.inspect_schema else value.inspect end end |
#original? ⇒ Boolean
258 259 260 |
# File 'lib/json_schema/schema.rb', line 258 def original? !clones.include?(self) end |
#pointer ⇒ Object
262 263 264 265 266 267 268 |
# File 'lib/json_schema/schema.rb', line 262 def pointer if parent parent.pointer + "/" + fragment else fragment end end |