module JsonSchema::Attributes
Attributes
mixes in some useful attribute-related methods for use in defining schema classes in a spirit similar to Ruby's attr_accessor and friends.
Public Class Methods
included(klass)
click to toggle source
# File lib/json_schema/attributes.rb, line 83 def self.included(klass) klass.extend(ClassMethods) klass.send(:initialize_attrs) end
Public Instance Methods
[](name)
click to toggle source
Allows the values of schema attributes to be accessed with a symbol or a string. So for example, the value of `schema.additional_items` could be procured with `schema`. This only works for attributes that are part of the JSON schema specification; other methods on the class are not available (e.g. `expanded`.)
This is implemented so that `JsonPointer::Evaluator` can evaluate a reference on an sintance of this class (as well as plain JSON data).
# File lib/json_schema/attributes.rb, line 96 def [](name) name = name.to_sym if self.class.schema_attrs.key?(name) send(self.class.schema_attrs[name]) else raise NoMethodError, "Schema does not respond to ##{name}" end end
copy_from(schema)
click to toggle source
# File lib/json_schema/attributes.rb, line 105 def copy_from(schema) self.class.copyable_attrs.each do |copyable, _| instance_variable_set(copyable, schema.instance_variable_get(copyable)) end end
initialize_attrs()
click to toggle source
# File lib/json_schema/attributes.rb, line 111 def initialize_attrs self.class.copyable_attrs.each do |attr, _| instance_variable_set(attr, nil) end end