Class: Schai::JsRoot

Inherits:
Object
  • Object
show all
Defined in:
lib/schai/json_schema/js_root.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#schemaObject

Returns the value of attribute schema.



3
4
5
# File 'lib/schai/json_schema/js_root.rb', line 3

def schema
  @schema
end

Class Method Details

.parse(params) ⇒ Object



5
6
7
8
9
# File 'lib/schai/json_schema/js_root.rb', line 5

def self.parse params
  self.new.tap do |js_root|
    js_root.schema = parse_components params
  end
end

.parse_components(params) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/schai/json_schema/js_root.rb', line 11

def self.parse_components params
  # include other .yaml file
  if Schai.current_parsing_file?(params['include']) and params['optional']
    return nil
  end

  if params.has_key?('include')
    included_schema = Schai.parse_file(params.delete('include')).schema
    params.each do |k, v|
      setter = "#{k}=".to_sym
      included_schema.send(setter, v)
    end
    return included_schema
  end

  raise "typeは必須(#{params})" unless params.has_key?('type')
  case params['type']
  when 'object'
    JsObject.parse params
  when 'array'
    JsArray.parse params
  else
    JsProperty.parse params
  end
end

Instance Method Details

#to_schemaObject



37
38
39
40
41
42
# File 'lib/schai/json_schema/js_root.rb', line 37

def to_schema
  schema = {
    '$schema': "http://json-schema.org/draft-04/schema#"
  }
  schema.merge @schema.to_schema
end