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


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

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

Instance Attribute Details

#property_nameSymbol (readonly)

Returns:

  • (Symbol)


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

def property_name
  @property_name
end

Instance Method Details

#as_json_schemaHash{Symbol => Object}

Returns:

  • (Hash{Symbol => Object})


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

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


41
42
43
# File 'lib/json_world/property_definition.rb', line 41

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



46
47
48
# File 'lib/json_world/property_definition.rb', line 46

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

#raw_optionsHash{Symbol => Object}

Returns:

  • (Hash{Symbol => Object})


51
52
53
54
55
# File 'lib/json_world/property_definition.rb', line 51

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