class Apipony::ResponseAttribute
A class used to describe an attribute in a response.
Attributes
attributes[RW]
choices[RW]
description[RW]
name[RW]
type[RW]
Public Class Methods
define_type(name, type)
click to toggle source
Allow a common subobject definition for code reuse. Probably use the ‘subtype` method in the `subtype` method of the DSL `define` DSL instead.
# File lib/apipony/response_attribute.rb, line 9 def self.define_type(name, type) @type_definitions[name] = type end
defined_subtypes()
click to toggle source
Get a list of predefined subtypes. Probably use the ‘
# File lib/apipony/response_attribute.rb, line 16 def self.defined_subtypes @type_definitions end
get_defined(name)
click to toggle source
Get a subtype with the given name.
# File lib/apipony/response_attribute.rb, line 21 def self.get_defined(name) @type_definitions[name] end
new(name, type: :string, description: "", array: false, example: nil, &block)
click to toggle source
# 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
Public Instance Methods
attribute(name, **params, &block)
click to toggle source
# 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)
click to toggle source
# File lib/apipony/response_attribute.rb, line 111 def choice(name, **params) @choices ||= [] @type = :enum @choices << EnumChoice.new(name, **params) end
example()
click to toggle source
Build an example from this object
# 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?()
click to toggle source
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
# File lib/apipony/response_attribute.rb, line 79 def is_array? !! @array end
is_enum?()
click to toggle source
# File lib/apipony/response_attribute.rb, line 107 def is_enum? @type == :enum end
is_object?()
click to toggle source
# File lib/apipony/response_attribute.rb, line 103 def is_object? @type == :object end
is_subtype?()
click to toggle source
See if this attribute is a reference to a predefined subtype.
# File lib/apipony/response_attribute.rb, line 85 def is_subtype? !! @is_subtype end
use_defined(type)
click to toggle source
# 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
Private Instance Methods
add_subattribute(atr)
click to toggle source
# File lib/apipony/response_attribute.rb, line 125 def add_subattribute atr @attributes ||= [] @type = :object @attributes << atr end