Class: JsonWorld::PropertyDefinition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property_name: nil, **options) ⇒ PropertyDefinition

Returns a new instance of PropertyDefinition.

Parameters:

  • property_name (Symbol, nil) (defaults to: nil)

    Note that unnamed property can be passed

  • options (Hash{Symbol => Object})


13
14
15
16
# File 'lib/json_world/property_definition.rb', line 13

def initialize(property_name: nil, **options)
  @options = options
  @property_name = property_name
end

Instance Attribute Details

#optionsHash{Symbol => Object} (readonly)

Returns:

  • (Hash{Symbol => Object})


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

def options
  @options
end

#property_nameSymbol (readonly)

Returns:

  • (Symbol)


9
10
11
# File 'lib/json_world/property_definition.rb', line 9

def property_name
  @property_name
end

Instance Method Details

#as_json_schemaHash{Symbol => Object}

Returns:

  • (Hash{Symbol => Object})


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/json_world/property_definition.rb', line 19

def as_json_schema
  if has_json_schema_compatible_type?
    if @options[:links]
      @options[:type].as_json_schema
    else
      @options[:type].as_json_schema_without_links
    end
  else
    {
      description: description,
      example: example,
      format: format_type,
      items: items_as_json_schema,
      pattern: pattern_in_string,
      properties: properties_as_json_schema,
      required: required_property_names,
      type: type,
      uniqueItems: unique_flag,
    }.reject do |_key, value|
      value.nil? || value.respond_to?(:empty?) && value.empty?
    end
  end
end

#as_nested_json_schemaHash{Symbol => Object}

Returns:

  • (Hash{Symbol => Object})


44
45
46
# File 'lib/json_world/property_definition.rb', line 44

def as_nested_json_schema
  { property_name => as_json_schema }
end

#optional?false, true

Returns True if explicitly this property is defined as optional.

Returns:

  • (false, true)

    True if explicitly this property is defined as optional



49
50
51
# File 'lib/json_world/property_definition.rb', line 49

def optional?
  !!@options[:optional]
end

#raw_optionsHash{Symbol => Object}

Returns:

  • (Hash{Symbol => Object})


54
55
56
57
58
# File 'lib/json_world/property_definition.rb', line 54

def raw_options
  @options.merge(
    property_name: @property_name,
  )
end