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


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

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

Instance Attribute Details

#property_nameSymbol (readonly)

Returns:

  • (Symbol)


4
5
6
# File 'lib/json_world/property_definition.rb', line 4

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

def as_json_schema
  {
    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

#as_nested_json_schemaHash{Symbol => Object}

Returns:

  • (Hash{Symbol => Object})


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

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



36
37
38
# File 'lib/json_world/property_definition.rb', line 36

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