Class: SchemaTest::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_test/property.rb

Direct Known Subclasses

Array, Boolean, Date, DateTime, Float, Integer, Nil, Object, String

Defined Under Namespace

Classes: AnonymousObject, Array, Boolean, Date, DateTime, Float, Integer, Nil, Object, String, UnresolvedProperty, Uri

Constant Summary collapse

NULL_TYPE =
'null'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, description = nil) ⇒ Property

Returns a new instance of Property.



7
8
9
10
11
12
13
# File 'lib/schema_test/property.rb', line 7

def initialize(name, type, description=nil)
  @name = name
  @_type = type
  @description = description
  @optional = false
  @nullable = false
end

Instance Attribute Details

#_typeObject (readonly)

Returns the value of attribute _type.



5
6
7
# File 'lib/schema_test/property.rb', line 5

def _type
  @_type
end

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/schema_test/property.rb', line 5

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/schema_test/property.rb', line 5

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/schema_test/property.rb', line 22

def ==(other)
  return false unless other.is_a?(SchemaTest::Property)

  name == other.name &&
    _type == other._type &&
    description == other.description &&
    optional? == other.optional? &&
    nullable? == other.nullable?
end

#as_json_schemaObject



15
16
17
18
19
20
# File 'lib/schema_test/property.rb', line 15

def as_json_schema
  json_schema = { 'type' => json_schema_type }
  json_schema['description'] = description if description
  json_schema['format'] = json_schema_format if json_schema_format
  { name.to_s => json_schema }
end

#base_json_schema_typeObject



64
65
66
# File 'lib/schema_test/property.rb', line 64

def base_json_schema_type
  @_type.to_s
end

#json_schema_formatObject



76
77
78
# File 'lib/schema_test/property.rb', line 76

def json_schema_format
  nil
end

#json_schema_typeObject



68
69
70
71
72
73
74
# File 'lib/schema_test/property.rb', line 68

def json_schema_type
  if nullable?
    [base_json_schema_type, NULL_TYPE]
  else
    base_json_schema_type
  end
end

#lookup_object(name, *versions) ⇒ Object



56
57
58
# File 'lib/schema_test/property.rb', line 56

def lookup_object(name, *versions)
  UnresolvedProperty.new(name, versions: versions)
end

#nullable(object) ⇒ Object



36
37
38
# File 'lib/schema_test/property.rb', line 36

def nullable(object)
  object.tap(&:nullable!)
end

#nullable!Object



52
53
54
# File 'lib/schema_test/property.rb', line 52

def nullable!
  @nullable = true
end

#nullable?Boolean

Returns:



48
49
50
# File 'lib/schema_test/property.rb', line 48

def nullable?
  @nullable
end

#optional(object) ⇒ Object



32
33
34
# File 'lib/schema_test/property.rb', line 32

def optional(object)
  object.tap(&:optional!)
end

#optional!Object



44
45
46
# File 'lib/schema_test/property.rb', line 44

def optional!
  @optional = true
end

#optional?Boolean

Returns:



40
41
42
# File 'lib/schema_test/property.rb', line 40

def optional?
  @optional
end

#type(name, version: nil) ⇒ Object



60
61
62
# File 'lib/schema_test/property.rb', line 60

def type(name, version: nil)
  lookup_object(name, version || @version)
end