Module: JsonWorld::DSL::ClassMethods

Defined in:
lib/json_world/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Returns:



63
64
65
# File 'lib/json_world/dsl.rb', line 63

def link_definitions
  @link_definitions ||= []
end

#property_definitionsArray<JsonWorld::PropertyDefinition>

Returns:



79
80
81
# File 'lib/json_world/dsl.rb', line 79

def property_definitions
  @property_definitions ||= []
end

Instance Method Details

#as_json_schemaHash

Returns:

  • (Hash)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/json_world/dsl.rb', line 24

def as_json_schema
  {
    '$schema': schema,
    description: description,
    links: links_as_json_schema,
    properties: properties_as_json_schema,
    required: required_property_names,
    title: title,
  }.reject do |_key, value|
    value.nil? || value.empty?
  end
end

Returns:

  • (Hash)


38
39
40
41
42
# File 'lib/json_world/dsl.rb', line 38

def as_json_schema_without_links
  as_json_schema.reject do |key, _value|
    key == :links
  end
end

#inherited(child) ⇒ Object

Note:

Override



45
46
47
48
49
# File 'lib/json_world/dsl.rb', line 45

def inherited(child)
  super
  child.link_definitions = link_definitions.clone
  child.property_definitions = property_definitions.clone
end

Parameters:

  • link_name (Symbol)
  • options (Hash{Symbol => Object}) (defaults to: {})


53
54
55
56
57
58
59
60
# File 'lib/json_world/dsl.rb', line 53

def link(link_name, options = {})
  link_definitions << JsonWorld::LinkDefinition.new(
    options.merge(
      parent: self,
      link_name: link_name,
    ),
  )
end

#property(property_name, options = {}) ⇒ Object

Parameters:

  • property_name (Symbol)
  • options (Hash{Symbol => Object}) (defaults to: {})


69
70
71
72
73
74
75
76
# File 'lib/json_world/dsl.rb', line 69

def property(property_name, options = {})
  property_definitions << JsonWorld::PropertyDefinition.new(
    options.merge(
      parent: self,
      property_name: property_name,
    )
  )
end

#property_namesArray<Symbol>

Returns:

  • (Array<Symbol>)


84
85
86
# File 'lib/json_world/dsl.rb', line 84

def property_names
  property_definitions.map(&:property_name)
end

#required_property_namesArray<Symbol>

Returns:

  • (Array<Symbol>)


89
90
91
# File 'lib/json_world/dsl.rb', line 89

def required_property_names
  property_definitions.reject(&:optional?).map(&:property_name)
end

#to_json_schemaString

Note:

.as_json_schema wrapper

Returns:

  • (String)


95
96
97
# File 'lib/json_world/dsl.rb', line 95

def to_json_schema
  JSON.pretty_generate(as_json_schema)
end