class GoldenFleece::Schema
Attributes
attribute[R]
context[R]
default[R]
format[R]
json_path[R]
name[R]
normalizers[R]
path[R]
subschemas[R]
types[R]
value[R]
Public Class Methods
new(context, path, definitions)
click to toggle source
# File lib/golden_fleece/schema.rb, line 11 def initialize(context, path, definitions) @context = context @path = path @name = path.split("/").last @attribute = path.split("/")[1] @json_path = path.split("/")[2..-1] @json_path = @json_path.join("/") if @json_path @subschemas = {} # .count == 1 means we're at the root # .count == 2 means we're at the attribute # .count >= 3 means we're cookin' if path.split("/").count <= 2 @types = [Definitions::TYPES[:object]] map_subschemas(definitions) else map_value map_types(definitions[:type], definitions[:types]) map_normalizers(definitions[:normalizer], definitions[:normalizers]) map_format(definitions[:format]) map_default(definitions[:default]) map_subschemas(definitions[:subschemas]) end end
Public Instance Methods
[](subschema_name)
click to toggle source
# File lib/golden_fleece/schema.rb, line 36 def [](subschema_name) subschemas[subschema_name] end
[]=(subschema_name, subschema_definition)
click to toggle source
# File lib/golden_fleece/schema.rb, line 40 def []=(subschema_name, subschema_definition) subschemas[subschema_name] = Schema.new(context, build_json_path(path, subschema_name), subschema_definition) end
each(&block)
click to toggle source
# File lib/golden_fleece/schema.rb, line 44 def each(&block) subschemas.each(&block) end
keys()
click to toggle source
# File lib/golden_fleece/schema.rb, line 56 def keys subschemas.keys end
parent?()
click to toggle source
# File lib/golden_fleece/schema.rb, line 52 def parent? subschemas.count > 0 end
reduce(memo, &block)
click to toggle source
# File lib/golden_fleece/schema.rb, line 48 def reduce(memo, &block) subschemas.reduce(memo, &block) end
values()
click to toggle source
# File lib/golden_fleece/schema.rb, line 60 def values subschemas.values end
Private Instance Methods
map_default(default)
click to toggle source
# File lib/golden_fleece/schema.rb, line 102 def map_default(default) @default = default end
map_format(fmt)
click to toggle source
# File lib/golden_fleece/schema.rb, line 92 def map_format(fmt) unless fmt.nil? fmt = fmt.to_sym raise ArgumentError.new("Invalid format '#{fmt}' specified for #{error_suffix(attribute, json_path)}") unless context.formats.include?(fmt) @format = context.formats[fmt] end end
map_normalizers(*args)
click to toggle source
# File lib/golden_fleece/schema.rb, line 82 def map_normalizers(*args) @normalizers = args.flatten.compact.map { |normalizer| normalizer = normalizer.to_sym raise ArgumentError.new("Invalid normalizer(s) '#{normalizer}' specified for #{error_suffix(attribute, json_path)}") unless context.normalizers.include?(normalizer) context.normalizers[normalizer] }.uniq end
map_subschemas(subschema_definitions)
click to toggle source
# File lib/golden_fleece/schema.rb, line 106 def map_subschemas(subschema_definitions) @subschemas = subschema_definitions.reduce({}) { |memo, (subschema_name, subschema_definition)| raise ArgumentError.new("'subschemas' option can only be set for 'object' type schemas, attempted to provide subschemas for #{error_suffix(attribute, json_path)}") unless types.include? Definitions::TYPES[:object] raise ArgumentError.new("The 'subschemas' option must be passed a hash, please check #{error_suffix(attribute, json_path)}") unless subschema_definition.is_a?(Hash) subschema_path = build_json_path(path, subschema_name) memo[subschema_name] = Schema.new(context, subschema_path, subschema_definition) memo } if subschema_definitions.present? end
map_types(*args)
click to toggle source
# File lib/golden_fleece/schema.rb, line 72 def map_types(*args) @types = args.flatten.compact.map { |type| type = type.to_sym raise ArgumentError.new("Invalid type '#{type}' specified for #{error_suffix(attribute, json_path)}}") unless Definitions::TYPES.include? type Definitions::TYPES[type] }.uniq end
map_value()
click to toggle source
# File lib/golden_fleece/schema.rb, line 68 def map_value @value = Value.new self end