Class: Jschematic::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/jschematic/context.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*raw_schemas) ⇒ Context

Returns a new instance of Context.



28
29
30
# File 'lib/jschematic/context.rb', line 28

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

Class Method Details

.add_schema(id, schema) ⇒ Object



16
17
18
# File 'lib/jschematic/context.rb', line 16

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

.emptyObject



24
25
26
# File 'lib/jschematic/context.rb', line 24

def self.empty
  new
end

.schema_for(ref) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/jschematic/context.rb', line 5

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

.schemasObject



20
21
22
# File 'lib/jschematic/context.rb', line 20

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

Instance Method Details

#add_schema(id, schema) ⇒ Object



36
37
38
# File 'lib/jschematic/context.rb', line 36

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

#schema_for(ref) ⇒ Object



40
41
42
# File 'lib/jschematic/context.rb', line 40

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

#validate!(instance, raw_schema) ⇒ Object



32
33
34
# File 'lib/jschematic/context.rb', line 32

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