Module: JsonSchema::Attributes

Included in:
Schema
Defined in:
lib/json_schema/attributes.rb

Overview

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.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



83
84
85
86
# File 'lib/json_schema/attributes.rb', line 83

def self.included(klass)
  klass.extend(ClassMethods)
  klass.send(:initialize_attrs)
end

Instance Method Details

#[](name) ⇒ Object

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).



96
97
98
99
100
101
102
103
# 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) ⇒ Object



105
106
107
108
109
# 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_attrsObject



111
112
113
114
115
# 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