Class: Apipony::ResponseAttribute
- Inherits:
-
Object
- Object
- Apipony::ResponseAttribute
- Defined in:
- lib/apipony/response_attribute.rb
Overview
A class used to describe an attribute in a response.
Defined Under Namespace
Classes: EnumChoice
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#choices ⇒ Object
Returns the value of attribute choices.
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
-
.define_type(name, type) ⇒ Object
Allow a common subobject definition for code reuse.
-
.defined_subtypes ⇒ Object
Get a list of predefined subtypes.
-
.get_defined(name) ⇒ Object
Get a subtype with the given name.
Instance Method Summary collapse
- #attribute(name, **params, &block) ⇒ Object
- #choice(name, **params) ⇒ Object
-
#example ⇒ Object
Build an example from this object.
-
#initialize(name, type: :string, description: "", array: false, example: nil, &block) ⇒ ResponseAttribute
constructor
A new instance of ResponseAttribute.
-
#is_array? ⇒ Boolean
Is this attribute an array? Note that marking an attribute as an array does not over-ride the top-level type.
- #is_enum? ⇒ Boolean
- #is_object? ⇒ Boolean
-
#is_subtype? ⇒ Boolean
See if this attribute is a reference to a predefined subtype.
- #use_defined(type) ⇒ Object
Constructor Details
#initialize(name, type: :string, description: "", array: false, example: nil, &block) ⇒ ResponseAttribute
Returns a new instance of ResponseAttribute.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/apipony/response_attribute.rb', line 26 def initialize(name, type: :string, description: "", array: false, example: nil, &block) @name = name @description = description @type = type @array = array @example = example if block_given? instance_eval(&block) ## This attribute is of a predefined subtype elsif (subtype = self.class.get_defined(@type)) @attributes = subtype.attributes # If the subtype is an array, this is also an array @array = subtype.is_array? unless @array @is_subtype = true end end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
25 26 27 |
# File 'lib/apipony/response_attribute.rb', line 25 def attributes @attributes end |
#choices ⇒ Object
Returns the value of attribute choices.
25 26 27 |
# File 'lib/apipony/response_attribute.rb', line 25 def choices @choices end |
#description ⇒ Object
Returns the value of attribute description.
25 26 27 |
# File 'lib/apipony/response_attribute.rb', line 25 def description @description end |
#name ⇒ Object
Returns the value of attribute name.
25 26 27 |
# File 'lib/apipony/response_attribute.rb', line 25 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
25 26 27 |
# File 'lib/apipony/response_attribute.rb', line 25 def type @type end |
Class Method Details
.define_type(name, type) ⇒ Object
Allow a common subobject definition for code reuse. Probably use the ‘subtype` method in the `subtype` method of the DSL `define` DSL instead.
9 10 11 |
# File 'lib/apipony/response_attribute.rb', line 9 def self.define_type(name, type) @type_definitions[name] = type end |
.defined_subtypes ⇒ Object
Get a list of predefined subtypes. Probably use the ‘
16 17 18 |
# File 'lib/apipony/response_attribute.rb', line 16 def self.defined_subtypes @type_definitions end |
.get_defined(name) ⇒ Object
Get a subtype with the given name.
21 22 23 |
# File 'lib/apipony/response_attribute.rb', line 21 def self.get_defined(name) @type_definitions[name] end |
Instance Method Details
#attribute(name, **params, &block) ⇒ Object
99 100 101 |
# File 'lib/apipony/response_attribute.rb', line 99 def attribute(name, **params, &block) add_subattribute self.class.new(name, **params, &block) end |
#choice(name, **params) ⇒ Object
111 112 113 114 115 |
# File 'lib/apipony/response_attribute.rb', line 111 def choice(name, **params) @choices ||= [] @type = :enum @choices << EnumChoice.new(name, **params) end |
#example ⇒ Object
Build an example from this object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/apipony/response_attribute.rb', line 50 def example sub = nil if is_object? || is_subtype? sub = Hash[@attributes.select(&:example).map{|a| [a.name, a.example]}] elsif is_enum? sub = choices.first.name else sub = @example end ## # If we have an example an are an array if sub and is_array? [sub] else sub end end |
#is_array? ⇒ Boolean
Is this attribute an array? Note that marking an attribute as an array does not over-ride the top-level type. For example, a definition like:
attribute :aliases, type: :string, array: true
denotates an array of strings. Also note that subattributes are not over-ridden. This lets you make an array of objects.
attribute :users, type: :object, array: true do
attribute :id, type: :integer
attribute :name, type: :integer
end
79 80 81 |
# File 'lib/apipony/response_attribute.rb', line 79 def is_array? !! @array end |
#is_enum? ⇒ Boolean
107 108 109 |
# File 'lib/apipony/response_attribute.rb', line 107 def is_enum? @type == :enum end |
#is_object? ⇒ Boolean
103 104 105 |
# File 'lib/apipony/response_attribute.rb', line 103 def is_object? @type == :object end |
#is_subtype? ⇒ Boolean
See if this attribute is a reference to a predefined subtype.
85 86 87 |
# File 'lib/apipony/response_attribute.rb', line 85 def is_subtype? !! @is_subtype end |
#use_defined(type) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/apipony/response_attribute.rb', line 89 def use_defined(type) a = self.class.get_defined(name) raise "Tried to use an undefined subtype" unless a ## # Shallow clone so we can alias the name a = a.clone a.name = as add_subattribute a end |