Class: TSJSON::Schema

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

Constant Summary collapse

BUILT_IN_SCALARS =
{
  'string' => TSJSONString,
  'int' => TSJSONInt,
  'float' => TSJSONFloat,
  'number' => TSJSONFloat,
  'boolean' => TSJSONBoolean,
  'any' => TSJSONAny,
  'null' => TSJSONNull
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



18
19
20
# File 'lib/schema/schema.rb', line 18

def initialize
  @types_map = {}
end

Instance Attribute Details

#types_mapObject (readonly)

Returns the value of attribute types_map.



6
7
8
# File 'lib/schema/schema.rb', line 6

def types_map
  @types_map
end

Class Method Details

.build(source) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/schema/schema.rb', line 36

def self.build(source)
  unless source.is_a?(Array) || source.is_a?(String)
    raise 'Schema.build expects string or array of strings'
  end
  builder = SchemaBuilder.new
  (source.is_a?(Array) ? source : [source]).each do |s|
    builder.parse_source(s)
  end
  builder.schema.compile
  return builder.schema
end

Instance Method Details

#add_type(name, type) ⇒ Object



26
27
28
29
30
# File 'lib/schema/schema.rb', line 26

def add_type(name, type)
  raise 'duplicate type' if type(name)

  @types_map[name] = type
end

#compileObject



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

def compile
  @types_map.values.each(&:compile)
end

#type(name) ⇒ Object



22
23
24
# File 'lib/schema/schema.rb', line 22

def type(name)
  BUILT_IN_SCALARS[name] || @types_map[name]
end