module JsonSchema::Attributes::ClassMethods
Provides class-level methods for the Attributes
module.
Attributes
Attributes
that should be copied between classes when invoking Attributes#copy_from
.
Hash contains instance variable names mapped to a default value for the field.
Attributes
that are part of the JSON schema and hyper-schema specifications. These are allowed to be accessed with the [] operator.
Hash contains the access key mapped to the name of the method that should be invoked to retrieve a value. For example, `type` maps to `type` and `additionalItems` maps to `additional_items`.
Public Instance Methods
identical to attr_accessible, but allows us to copy in values from a target schema to help preserve our hierarchy during reference expansion
# File lib/json_schema/attributes.rb, line 25 def attr_copyable(attr, options = {}) attr_accessor(attr) ref = :"@#{attr}" # Usually the default being assigned here is nil. self.copyable_attrs[ref] = options[:default] if default = options[:default] # remove the reader already created by attr_accessor remove_method(attr) if [Array, Hash, Set].include?(default.class) default = default.freeze end define_method(attr) do val = instance_variable_get(ref) if !val.nil? val else default end end end if options[:clear_cache] remove_method(:"#{attr}=") define_method(:"#{attr}=") do |value| instance_variable_set(options[:clear_cache], nil) instance_variable_set(ref, value) end end end
# File lib/json_schema/attributes.rb, line 59 def attr_schema(attr, options = {}) attr_copyable(attr, :default => options[:default], :clear_cache => options[:clear_cache]) self.schema_attrs[options[:schema_name] || attr] = attr end
Directive indicating that attributes should be inherited from a parent class.
Must appear as first statement in class that mixes in (or whose parent mixes in) the Attributes
module.
# File lib/json_schema/attributes.rb, line 69 def inherit_attrs @copyable_attrs = self.superclass.instance_variable_get(:@copyable_attrs).dup @schema_attrs = self.superclass.instance_variable_get(:@schema_attrs).dup end
Initializes some class instance variables required to make other methods in the Attributes
module work. Run automatically when the module is mixed into another class.
# File lib/json_schema/attributes.rb, line 77 def initialize_attrs @copyable_attrs = {} @schema_attrs = {} end