Class: ApiSketch::Model::Attribute
- Defined in:
- lib/api_sketch/model/attribute.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#data_type ⇒ Object
Returns the value of attribute data_type.
-
#default ⇒ Object
Returns the value of attribute default.
-
#example ⇒ Object
Returns the value of attribute example.
-
#required ⇒ Object
Returns the value of attribute required.
-
#value ⇒ Object
Returns the value of attribute value.
Attributes inherited from Base
Instance Method Summary collapse
- #content_to_hash ⇒ Object
- #example_value(defaults_allowed = false) ⇒ Object
-
#example_value_default ⇒ Object
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.
- #to_hash ⇒ Object
Methods inherited from Base
Constructor Details
This class inherits a constructor from ApiSketch::Model::Base
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
2 3 4 |
# File 'lib/api_sketch/model/attribute.rb', line 2 def content @content end |
#data_type ⇒ Object
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 |
#default ⇒ Object
Returns the value of attribute default.
2 3 4 |
# File 'lib/api_sketch/model/attribute.rb', line 2 def default @default end |
#example ⇒ Object
Returns the value of attribute example.
2 3 4 |
# File 'lib/api_sketch/model/attribute.rb', line 2 def example @example end |
#required ⇒ Object
Returns the value of attribute required.
2 3 4 |
# File 'lib/api_sketch/model/attribute.rb', line 2 def required @required end |
#value ⇒ Object
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_hash ⇒ Object
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_default ⇒ Object
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_hash ⇒ Object
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 |