Class: Vk::Schema::Definition::Attribute

Inherits:
Vk::Schema::Definition show all
Defined in:
lib/vk/schema/definition/attribute.rb

Constant Summary collapse

TYPES =
Hash.new do |hash, key|
  hash[key] = "API::Types::Coercible::#{key.capitalize}"
end.merge(
  'integer' => 'API::Types::Coercible::Int',
  'number' => 'API::Types::Coercible::Int',
  'object' => 'API::Types::Coercible::Hash',
  'boolean' => 'API::Types::Form::Bool'
)
SAMPLE_VALUES_REGEXP =
/Sample values: (((?<samples>'([\w_]+)')(,\s*|[\w —\(\)]+[;.])?)+)/

Instance Attribute Summary collapse

Attributes inherited from Vk::Schema::Definition

#definition, #name, #schema

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Vk::Schema::Definition

#description, #to_s

Constructor Details

#initialize(name, definition, schema, object) ⇒ Attribute

Returns a new instance of Attribute.



17
18
19
20
# File 'lib/vk/schema/definition/attribute.rb', line 17

def initialize(name, definition, schema, object)
  super(name, definition, schema)
  @object = object
end

Instance Attribute Details

#objectDefinition::Object (readonly)

Returns:



23
24
25
# File 'lib/vk/schema/definition/attribute.rb', line 23

def object
  @object
end

Class Method Details

.type_for(type) ⇒ Object

Parameters:



26
27
28
# File 'lib/vk/schema/definition/attribute.rb', line 26

def self.type_for(type)
  TYPES[type.to_s]
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/vk/schema/definition/attribute.rb', line 176

def array?
  definition['type'] == 'array'
end

#array_item?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/vk/schema/definition/attribute.rb', line 181

def array_item?
  array? && definition['items'].is_a?(Hash)
end

#attribute_nameString

Returns:

  • (String)


31
32
33
# File 'lib/vk/schema/definition/attribute.rb', line 31

def attribute_name
  ":#{@name}".strip
end

#boolean?String

Returns:

  • (String)


86
87
88
# File 'lib/vk/schema/definition/attribute.rb', line 86

def boolean?
  definition['type'] == 'boolean'
end

#default?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/vk/schema/definition/attribute.rb', line 58

def default?
  definition.key?('default')
end

#default_valueObject

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vk/schema/definition/attribute.rb', line 68

def default_value
  if default?
    value = if enum?
              definition['enum'][
                definition['default']
              ]
            else
              definition['default']
            end
    value = value == 1 if boolean?
    # TODO: fix following after schema fixed
    value = [] if array? && value.zero?
    value = nil if string? && !value.is_a?(String)
    value
  end.inspect
end

#enum?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/vk/schema/definition/attribute.rb', line 63

def enum?
  definition.key?('enum')
end

#inline_object?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/vk/schema/definition/attribute.rb', line 195

def inline_object?
  definition.values.first.is_a?(Hash)
end

#multiple_types?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/vk/schema/definition/attribute.rb', line 172

def multiple_types?
  definition['type'].is_a?(Array)
end

#optional?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/vk/schema/definition/attribute.rb', line 101

def optional?
  !required?
end

#polymorphic?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/vk/schema/definition/attribute.rb', line 191

def polymorphic?
  definition['allOf'].is_a?(Array)
end

#polymorphic_typeString

Returns:

  • (String)


133
134
135
136
137
# File 'lib/vk/schema/definition/attribute.rb', line 133

def polymorphic_type
  definition['allOf'].map do |definition|
    schema.definition_of(definition['$ref']).dry_type
  end.join(' | ')
end

#reference?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/vk/schema/definition/attribute.rb', line 186

def reference?
  definition['$ref'].is_a?(String)
end

#referenced_definitionDefinition

Returns:



205
206
207
# File 'lib/vk/schema/definition/attribute.rb', line 205

def referenced_definition
  schema.definition_of(definition['$ref'])
end

#referenced_item_definitionDefinition

Returns:



200
201
202
# File 'lib/vk/schema/definition/attribute.rb', line 200

def referenced_item_definition
  schema.definition_of(definition['items']['$ref'])
end

#required?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/vk/schema/definition/attribute.rb', line 96

def required?
  object.required_attribute?(@name) || definition['required']
end

#returned_typeString

Returns:

  • (String)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/vk/schema/definition/attribute.rb', line 142

def returned_type
  type =
    if simple?
      definition['type'].capitalize
    elsif multiple_types?
      definition['type'].map(&:capitalize).join(', ')
    elsif reference?
      referenced_definition.referenced_type_name
    elsif polymorphic?
      definition['allOf'].map do |definition|
        schema.definition_of(definition['$ref']).referenced_type_name
      end.join(', ')
    elsif inline_object?
      'Hash' # TODO: make inline struct definition
    else
      puts "Unknown definition: #{definition.inspect}"
    end
  sample_values = SAMPLE_VALUES_REGEXP.match(description)
  if sample_values
    type += ", #{Array(sample_values[:samples]).join(', ')}"
  end
  type
end

#simple?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/vk/schema/definition/attribute.rb', line 167

def simple?
  definition['type'].is_a?(String)
end

#simple_typeString

Returns:

  • (String)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vk/schema/definition/attribute.rb', line 106

def simple_type
  result = self.class.type_for(definition['type'])
  if array_item?
    member = if definition['items']['$ref'].is_a?(String)
               referenced_item_definition.referenced_type_name
             elsif definition['items']['type']
               self.class.type_for(definition['items']['type'])
             end
    result += ".member(#{member})" if member

    if definition['maxItems']
      result += ".constrained(max_size: #{definition['maxItems']})"
    end
  elsif enum?
    result += ".enum(#{definition['enum'].map(&:inspect).join(', ')})"
  end
  result
end

#string?String

Returns:

  • (String)


91
92
93
# File 'lib/vk/schema/definition/attribute.rb', line 91

def string?
  definition['type'] == 'string'
end

#typeString

Returns:

  • (String)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vk/schema/definition/attribute.rb', line 36

def type
  type = if simple?
           simple_type
         elsif multiple_types?
           type_variants
         elsif reference?
           referenced_definition.dry_type
         elsif polymorphic?
           polymorphic_type
         elsif inline_object?
           # TODO: make inline struct definition
           'API::Types::Coercible::Hash'.tap do |hash|
           end
         else
           puts "Unknown definition: #{definition.inspect}"
         end
  type += '.optional' unless required?
  type += ".default(#{default_value})" if default? || optional?
  type
end

#type_variantsString

Returns:

  • (String)


126
127
128
129
130
# File 'lib/vk/schema/definition/attribute.rb', line 126

def type_variants
  definition['type'].map do |variant|
    self.class.type_for(variant)
  end.join(' | ')
end