Class: Schai::JsObject

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ JsObject

Returns a new instance of JsObject.



9
10
11
12
13
14
15
16
# File 'lib/schai/json_schema/js_object.rb', line 9

def initialize params
  @all = Hash[params["properties"].map {|k, v|
    [k || 'null', JsRoot.parse_components(v)]
  }.select{|p| !p[1].nil? }]
  @description = params["description"]
  @example = params["example"]
  @optional = params["optional"]
end

Instance Attribute Details

#allObject

Returns the value of attribute all.



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

def all
  @all
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#exampleObject

Returns the value of attribute example.



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

def example
  @example
end

#optionalObject

Returns the value of attribute optional.



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

def optional
  @optional
end

Class Method Details

.parse(params) ⇒ Object



5
6
7
# File 'lib/schai/json_schema/js_object.rb', line 5

def self.parse params
  self.new params
end

Instance Method Details

#to_schemaObject



18
19
20
21
22
23
24
25
26
# File 'lib/schai/json_schema/js_object.rb', line 18

def to_schema
  schema = {}
  schema[:type] = :object
  schema[:description] = @description if @description
  schema[:properties] = Hash[@all.map {|k, p| [k, p.to_schema]}]
  schema[:required]  = @all.select{|k, p| !p.optional}.map{|k, p| k || 'null'}
  schema[:example] = @example if @example
  schema
end