Class: ApiSketch::Model::Attribute

Inherits:
Base
  • Object
show all
Defined in:
lib/api_sketch/model/attribute.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#description, #name

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from ApiSketch::Model::Base

Instance Attribute Details

#contentObject

Returns the value of attribute content.



2
3
4
# File 'lib/api_sketch/model/attribute.rb', line 2

def content
  @content
end

#data_typeObject

Returns the value of attribute data_type.



2
3
4
# File 'lib/api_sketch/model/attribute.rb', line 2

def data_type
  @data_type
end

#defaultObject

Returns the value of attribute default.



2
3
4
# File 'lib/api_sketch/model/attribute.rb', line 2

def default
  @default
end

#exampleObject

Returns the value of attribute example.



2
3
4
# File 'lib/api_sketch/model/attribute.rb', line 2

def example
  @example
end

#requiredObject

Returns the value of attribute required.



2
3
4
# File 'lib/api_sketch/model/attribute.rb', line 2

def required
  @required
end

#valueObject

Returns the value of attribute value.



2
3
4
# File 'lib/api_sketch/model/attribute.rb', line 2

def value
  @value
end

Instance Method Details

#content_to_hashObject



34
35
36
37
38
39
40
# File 'lib/api_sketch/model/attribute.rb', line 34

def content_to_hash
  if self.content
    self.content.map do |item|
      item.to_hash
    end
  end
end

#example_value(defaults_allowed = false) ⇒ Object



4
5
6
7
8
9
# File 'lib/api_sketch/model/attribute.rb', line 4

def example_value(defaults_allowed=false)
  value = self.example
  value ||= example_value_default if defaults_allowed

  value.respond_to?(:call) ? value.call : value
end

#example_value_defaultObject

TODO: These default values should be configurable via DSL

Some logic to defer value example from key name, - email from key with email part inside, etc.


13
14
15
16
17
18
19
20
21
22
# File 'lib/api_sketch/model/attribute.rb', line 13

def example_value_default
  {
    integer:   lambda { rand(1000) + 1 },
    string:    lambda { "random_string_#{('A'..'Z').to_a.shuffle.first(8).join}" },
    float:     lambda { rand(100) + rand(100) * 0.01 },
    boolean:   lambda { [true, false].sample },
    datetime:  lambda { Time.now.strftime("%d-%m-%Y %H:%M:%S") },
    timestamp: lambda { Time.now.to_i }
  }[data_type]
end

#to_hashObject



24
25
26
27
28
29
30
31
32
# File 'lib/api_sketch/model/attribute.rb', line 24

def to_hash
  {
    data_type: self.data_type,
    example_value: self.example_value,
    required: !!self.required,
    default: self.default,
    content: self.content_to_hash
  }
end