lib/render/schema.rb in render-0.0.9 vs lib/render/schema.rb in render-0.1.0

- old
+ new

@@ -64,11 +64,16 @@ end private def require_attributes! - definition.fetch(:required, []).each do |required_attribute| + return unless definition.has_key?(:required) + + required_attributes = definition.fetch(:required) + return if [true, false].include?(required_attributes) + + required_attributes.each do |required_attribute| attribute = attributes.detect { |attribute| attribute.name == required_attribute.to_sym } attribute.required = true end rescue raise Errors::Schema::InvalidRequire.new(definition) @@ -95,18 +100,28 @@ working_definition.each do |(instance_name, instance_value)| next unless instance_value.is_a?(Hash) if instance_value.has_key?(:$ref) ref = instance_value.fetch(:$ref) - ref_definition = Definition.find(ref, false) || find_local_schema(ref, current_scope) + ref_definition = find_foreign_definition(ref) + ref_definition ||= find_local_schema(ref, current_scope) instance_value.replace(ref_definition) end interpolate_refs!(instance_value, current_scope.dup << instance_name) end end + def find_foreign_definition(ref) + exact_match = Definition.find(ref, false) + return exact_match if !exact_match.nil? + + foreign_root_path, foreign_root_scope = ref.split(ROOT_POINTER) + fuzzy_match = Definition.instances.detect { |id, definition| id.match(%r{^#{foreign_root_path}}) } + find_at_path(foreign_root_scope.split(POINTER_SEPARATOR), fuzzy_match[1]) if fuzzy_match + end + def find_local_schema(ref, scopes) paths = ref.split(POINTER_SEPARATOR) if (paths.first == ROOT_POINTER) paths.shift find_at_path(paths) || {} @@ -118,11 +133,11 @@ def find_at_closest_scope(path, scopes) return if scopes.empty? find_at_path(scopes + path) || find_at_closest_scope(path, scopes[0...-1]) end - def find_at_path(paths) - paths.reduce(definition) do |reduction, path| + def find_at_path(paths, working_definition = definition) + paths.reduce(working_definition) do |reduction, path| reduction[path.to_sym] || return end end def container?(definition)