Sha256: 79cdc0d4dd8b5b2aad62d4a16887c31afaec537e9506f8d9919156da19039cb3

Contents?: true

Size: 859 Bytes

Versions: 1

Compression:

Stored size: 859 Bytes

Contents

require 'jschematic/schema'

module Jschematic
  class Context
    def self.schema_for(ref)
      if ref.relative?
        rel = ref.omit(:scheme, :host, :port)
        if match = schemas.find{ |ref, _| rel == ref.omit(:scheme, :host, :port) }
          match[-1]
        end
      else
        schemas[ref]
      end
    end

    def self.add_schema(id, schema)
      schemas[id] = schema
    end

    def self.schemas
      @schemas ||= {}
    end

    def self.empty
      new
    end

    def initialize(*raw_schemas)
      @schemas = raw_schemas.collect{ |raw_schema| Schema.new(raw_schema) }
    end

    def validate!(instance, raw_schema)
      Schema.new(raw_schema).accepts?(instance)
    end

    def add_schema(id, schema)
      self.class.add_schema(id, schema)
    end

    def schema_for(ref)
      self.class.schema_for(ref)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jschematic-0.1.0 lib/jschematic/context.rb