Class: WSDSL::Response::Element
- Inherits:
-
Object
- Object
- WSDSL::Response::Element
- Defined in:
- lib/response.rb
Overview
The Response element class describing each element of a service response. Instances are usually not instiated directly but via the Response#element accessor.
Direct Known Subclasses
Defined Under Namespace
Classes: Attribute, MetaAttribute
Instance Attribute Summary collapse
-
#attributes ⇒ Array<WSDSL::Response::Element::Attribute>
(also: #properties)
readonly
An array of attributes.
-
#doc ⇒ WSDSL::Documentation::ElementDoc
readonly
Response element documentation.
-
#elements ⇒ NilClass, Array<WSDSL::Response::Element>
(also: #objects)
readonly
The optional nested elements.
-
#key(name = nil, opts = {}) ⇒ Object
readonly
Getter/setter for the key meta attribute.
-
#meta_attributes ⇒ Array<WSDSL::Response::Element::MetaAttribute>
readonly
An array of meta attributes.
-
#name ⇒ String, #to_s
readonly
The name of the element.
-
#type(name = nil, opts = {}) ⇒ Object
readonly
Getter/setter for the type meta attribute.
-
#vectors ⇒ Array
readonly
An array of vectors/arrays.
Instance Method Summary collapse
-
#array(name, type = nil) {|Vector| ... } ⇒ Array<WSDSL::Response::Vector>
Defines an array aka vector of elements.
-
#arrays ⇒ Array<WSDSL::Response::Vector>
Returns the arrays/vectors contained in the response.
-
#attribute(opts) ⇒ Array<WSDSL::Response::Attribute>
sets a new attribute and returns the entire list of attributes.
-
#boolean(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute.
-
#datetime(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute.
-
#element(opts = {}) {|WSDSL::Response::Element| ... } ⇒ Array<WSDSL::Response::Element>
Defines a new element and yields the content of an optional block Each new element is then stored in the elements array.
-
#float(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute.
-
#initialize(name, type = nil) ⇒ Element
constructor
param [String, Symbol] name The name of the element param [String, Symbol] type The optional type of the element.
-
#integer(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute.
-
#meta_attribute(opts) ⇒ Array<WSDSL::Response::MetaAttribute>
sets a new meta attribute and returns the entire list of meta attributes.
-
#object(name, opts = {}, &block) ⇒ Object
Shortcut to create a new element.
-
#string(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute.
-
#to_hash ⇒ Hash
Converts an element into a hash representation.
- #to_html ⇒ Object
Constructor Details
#initialize(name, type = nil) ⇒ Element
param [String, Symbol] name The name of the element param [String, Symbol] type The optional type of the element
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/response.rb', line 147 def initialize(name, type=nil) # sets a documentation placeholder since the response doc is defined at the same time # the response is defined. @doc = Documentation::ElementDoc.new(name) @name = name @type = type @attributes = [] @meta_attributes = [] @vectors = [] @key = nil # we don't need to initialize the nested elements, by default they should be nil end |
Instance Attribute Details
#attributes ⇒ Array<WSDSL::Response::Element::Attribute> (readonly) Also known as: properties
Returns An array of attributes.
121 122 123 |
# File 'lib/response.rb', line 121 def attributes @attributes end |
#doc ⇒ WSDSL::Documentation::ElementDoc (readonly)
Returns Response element documentation.
133 134 135 |
# File 'lib/response.rb', line 133 def doc @doc end |
#elements ⇒ NilClass, Array<WSDSL::Response::Element> (readonly) Also known as: objects
Returns The optional nested elements.
136 137 138 |
# File 'lib/response.rb', line 136 def elements @elements end |
#key(name = nil, opts = {}) ⇒ Object (readonly)
Getter/setter for the key meta attribute. A key name can be used to lookup an object by a primary key for instance.
117 118 119 |
# File 'lib/response.rb', line 117 def key @key end |
#meta_attributes ⇒ Array<WSDSL::Response::Element::MetaAttribute> (readonly)
Returns An array of meta attributes.
125 126 127 |
# File 'lib/response.rb', line 125 def @meta_attributes end |
#name ⇒ String, #to_s (readonly)
Returns The name of the element.
111 112 113 |
# File 'lib/response.rb', line 111 def name @name end |
#type(name = nil, opts = {}) ⇒ Object (readonly)
Getter/setter for the type meta attribute.
114 115 116 |
# File 'lib/response.rb', line 114 def type @type end |
#vectors ⇒ Array (readonly)
Returns An array of vectors/arrays.
129 130 131 |
# File 'lib/response.rb', line 129 def vectors @vectors end |
Instance Method Details
#array(name, type = nil) {|Vector| ... } ⇒ Array<WSDSL::Response::Vector>
Defines an array aka vector of elements.
233 234 235 236 237 |
# File 'lib/response.rb', line 233 def array(name, type=nil) vector = Vector.new(name, type) yield(vector) if block_given? @vectors << vector end |
#arrays ⇒ Array<WSDSL::Response::Vector>
Returns the arrays/vectors contained in the response. This is an alias to access @vectors
245 246 247 |
# File 'lib/response.rb', line 245 def arrays @vectors end |
#attribute(opts) ⇒ Array<WSDSL::Response::Attribute>
sets a new attribute and returns the entire list of attributes
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/response.rb', line 176 def attribute(opts) raise ArgumentError unless opts.is_a?(Hash) # extract the documentation part and add it where it belongs new_attribute = Attribute.new(opts) @attributes << new_attribute # document the attribute if description available # we might want to have a placeholder message when a response attribute isn't defined if opts.has_key?(:doc) @doc.attribute(new_attribute.name, opts[:doc]) end @attributes end |
#boolean(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute
325 326 327 |
# File 'lib/response.rb', line 325 def boolean(name=nil, opts={}) attribute({name => :boolean}.merge(opts)) end |
#datetime(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute
333 334 335 |
# File 'lib/response.rb', line 333 def datetime(name=nil, opts={}) attribute({name => :datetime}.merge(opts)) end |
#element(opts = {}) {|WSDSL::Response::Element| ... } ⇒ Array<WSDSL::Response::Element>
Defines a new element and yields the content of an optional block Each new element is then stored in the elements array.
264 265 266 267 268 269 270 |
# File 'lib/response.rb', line 264 def element(opts={}) el = Element.new(opts[:name], opts[:type]) yield(el) if block_given? @elements ||= [] @elements << el el end |
#float(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute
317 318 319 |
# File 'lib/response.rb', line 317 def float(name=nil, opts={}) attribute({name => :float}.merge(opts)) end |
#integer(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute
309 310 311 |
# File 'lib/response.rb', line 309 def integer(name=nil, opts={}) attribute({name => :integer}.merge(opts)) end |
#meta_attribute(opts) ⇒ Array<WSDSL::Response::MetaAttribute>
sets a new meta attribute and returns the entire list of meta attributes
204 205 206 207 208 209 210 |
# File 'lib/response.rb', line 204 def (opts) raise ArgumentError unless opts.is_a?(Hash) # extract the documentation part and add it where it belongs new_attribute = MetaAttribute.new(opts) @meta_attributes << new_attribute @meta_attributes end |
#object(name, opts = {}, &block) ⇒ Object
Shortcut to create a new element.
276 277 278 |
# File 'lib/response.rb', line 276 def object(name, opts={}, &block) element(opts.merge(:name => name), &block) end |
#string(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute
301 302 303 |
# File 'lib/response.rb', line 301 def string(name=nil, opts={}) attribute({name => :string}.merge(opts)) end |
#to_hash ⇒ Hash
Converts an element into a hash representation
340 341 342 343 344 345 346 347 348 349 |
# File 'lib/response.rb', line 340 def to_hash attrs = {} attributes.each{ |attr| attrs[attr.name] = attr.type } elements.each{ |el| attrs[el.name] = el.to_hash } if elements if self.class == Vector name ? {name => [attrs]} : [attrs] else name ? {name => attrs} : attrs end end |
#to_html ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/response.rb', line 351 def to_html output = "" if name output << "<li>" output << "<span class='label notice'>#{name}</span> of type <span class='label success'>#{self.is_a?(Vector) ? 'Array' : 'Object'}</span>" end if self.is_a? Vector output << "<h6>Properties of each array item:</h6>" else output << "<h6>Properties:</h6>" end output << "<ul>" properties.each do |prop| output << "<li><span class='label notice'>#{prop.name}</span> of type <span class='label success'>#{prop.type}</span> " output << prop.doc unless prop.doc.blank? output << "</li>" end arrays.each{ |arr| output << arr.html } elements.each {|el| output << el.to_html } if elements output << "</ul>" output << "</li>" if name output end |