Class: WeaselDiesel::Response::Element
- Inherits:
-
Object
- Object
- WeaselDiesel::Response::Element
- Defined in:
- lib/response.rb
Overview
The Response element class describing each element of a service response. Instances are usually not instantiated directly but via the Response#element accessor.
Direct Known Subclasses
Defined Under Namespace
Classes: Attribute, MetaAttribute
Instance Attribute Summary collapse
-
#attributes ⇒ Array<WeaselDiesel::Response::Element::Attribute>
(also: #properties)
readonly
An array of attributes.
-
#doc ⇒ WeaselDiesel::Documentation::ElementDoc
readonly
Response element documentation.
-
#elements ⇒ NilClass, Array<WeaselDiesel::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<WeaselDiesel::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<WeaselDiesel::Response::Vector>
Defines an array aka vector of elements.
-
#arrays ⇒ Array<WeaselDiesel::Response::Vector>
Returns the arrays/vectors contained in the response.
-
#attribute(opts) ⇒ Array<WeaselDiesel::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 = {}) {|WeaselDiesel::Response::Element| ... } ⇒ Array<WeaselDiesel::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<WeaselDiesel::Response::MetaAttribute>
sets a new meta attribute and returns the entire list of meta attributes.
-
#object(name = nil, opts = {}, &block) ⇒ Object
Shortcut to create a new element.
-
#string(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute.
-
#to_hash(root_node = true) ⇒ Hash
Converts an element into a hash representation.
- #to_html ⇒ Object
-
#to_json ⇒ String
Converts an element into a json representation.
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
148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/response.rb', line 148 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 = [] @elements = [] @vectors = [] @key = nil # we don't need to initialize the nested elements, by default they should be nil end |
Instance Attribute Details
#attributes ⇒ Array<WeaselDiesel::Response::Element::Attribute> (readonly) Also known as: properties
Returns An array of attributes.
122 123 124 |
# File 'lib/response.rb', line 122 def attributes @attributes end |
#doc ⇒ WeaselDiesel::Documentation::ElementDoc (readonly)
Returns Response element documentation.
134 135 136 |
# File 'lib/response.rb', line 134 def doc @doc end |
#elements ⇒ NilClass, Array<WeaselDiesel::Response::Element> (readonly) Also known as: objects
Returns The optional nested elements.
137 138 139 |
# File 'lib/response.rb', line 137 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.
118 119 120 |
# File 'lib/response.rb', line 118 def key @key end |
#meta_attributes ⇒ Array<WeaselDiesel::Response::Element::MetaAttribute> (readonly)
Returns An array of meta attributes.
126 127 128 |
# File 'lib/response.rb', line 126 def @meta_attributes end |
#name ⇒ String, #to_s (readonly)
Returns The name of the element.
112 113 114 |
# File 'lib/response.rb', line 112 def name @name end |
#type(name = nil, opts = {}) ⇒ Object (readonly)
Getter/setter for the type meta attribute.
115 116 117 |
# File 'lib/response.rb', line 115 def type @type end |
#vectors ⇒ Array (readonly)
Returns An array of vectors/arrays.
130 131 132 |
# File 'lib/response.rb', line 130 def vectors @vectors end |
Instance Method Details
#array(name, type = nil) {|Vector| ... } ⇒ Array<WeaselDiesel::Response::Vector>
Defines an array aka vector of elements.
234 235 236 237 238 |
# File 'lib/response.rb', line 234 def array(name, type=nil) vector = Vector.new(name, type) yield(vector) if block_given? @vectors << vector end |
#arrays ⇒ Array<WeaselDiesel::Response::Vector>
Returns the arrays/vectors contained in the response. This is an alias to access @vectors
246 247 248 |
# File 'lib/response.rb', line 246 def arrays @vectors end |
#attribute(opts) ⇒ Array<WeaselDiesel::Response::Attribute>
sets a new attribute and returns the entire list of attributes
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/response.rb', line 178 def attribute(opts) raise ArgumentError unless opts.is_a?(Hash) 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
326 327 328 |
# File 'lib/response.rb', line 326 def boolean(name=nil, opts={}) attribute({name => :boolean}.merge(opts)) end |
#datetime(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute
334 335 336 |
# File 'lib/response.rb', line 334 def datetime(name=nil, opts={}) attribute({name => :datetime}.merge(opts)) end |
#element(opts = {}) {|WeaselDiesel::Response::Element| ... } ⇒ Array<WeaselDiesel::Response::Element>
Defines a new element and yields the content of an optional block Each new element is then stored in the elements array.
265 266 267 268 269 270 271 |
# File 'lib/response.rb', line 265 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
318 319 320 |
# File 'lib/response.rb', line 318 def float(name=nil, opts={}) attribute({name => :float}.merge(opts)) end |
#integer(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute
310 311 312 |
# File 'lib/response.rb', line 310 def integer(name=nil, opts={}) attribute({name => :integer}.merge(opts)) end |
#meta_attribute(opts) ⇒ Array<WeaselDiesel::Response::MetaAttribute>
sets a new meta attribute and returns the entire list of meta attributes
205 206 207 208 209 210 211 |
# File 'lib/response.rb', line 205 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 = nil, opts = {}, &block) ⇒ Object
Shortcut to create a new element.
277 278 279 |
# File 'lib/response.rb', line 277 def object(name=nil, opts={}, &block) element(opts.merge(:name => name), &block) end |
#string(name = nil, opts = {}) ⇒ Object
Shortcut to create a string attribute
302 303 304 |
# File 'lib/response.rb', line 302 def string(name=nil, opts={}) attribute({name => :string}.merge(opts)) end |
#to_hash(root_node = true) ⇒ Hash
Converts an element into a hash representation
342 343 344 345 346 347 348 349 350 351 |
# File 'lib/response.rb', line 342 def to_hash(root_node=true) attrs = {} attributes.each{ |attr| attrs[attr.name] = attr.type } (vectors + elements).each{ |el| attrs[el.name] = el.to_hash(false) } if self.class == Vector (root_node && name) ? {name => [attrs]} : [attrs] else (root_node && name) ? {name => attrs} : attrs end end |
#to_html ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/response.rb', line 360 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> #{'(Can be blank or missing) ' if prop.opts && prop.opts.respond_to?(:[]) && prop.opts[:null]} " output << prop.doc unless prop.doc.nil? or prop.doc.empty? output << "</li>" end arrays.each{ |arr| output << arr.to_html } elements.each {|el| output << el.to_html } if elements output << "</ul>" output << "</li>" if name output end |
#to_json ⇒ String
Converts an element into a json representation
356 357 358 |
# File 'lib/response.rb', line 356 def to_json to_hash.to_json end |