Module: JsonWorld::DSL::ClassMethods

Defined in:
lib/json_world/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Returns:



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

def link_definitions
  @link_definitions ||= []
end

#property_definitionsArray<JsonWorld::PropertyDefinition>

Returns:



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

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
# File 'lib/json_world/dsl.rb', line 24

def as_json_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)


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

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

#inherited(child) ⇒ Object

Note:

Override



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

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: {})


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

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: {})


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

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>)


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

def property_names
  property_definitions.map(&:property_name)
end

#required_property_namesArray<Symbol>

Returns:

  • (Array<Symbol>)


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

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

#to_json_schemaString

Note:

.as_json_schema wrapper

Returns:

  • (String)


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

def to_json_schema
  JSON.pretty_generate(as_json_schema)
end