Class: Azeroth::Decorator
- Inherits:
-
Object
- Object
- Azeroth::Decorator
- Defined in:
- lib/azeroth/decorator.rb,
lib/azeroth/decorator/options.rb,
lib/azeroth/decorator/hash_builder.rb,
lib/azeroth/decorator/key_value_extractor.rb
Overview
Class to be used when decorating outputs
Defined Under Namespace
Classes: HashBuilder, KeyValueExtractor, Options
Class Method Summary collapse
-
.attributes_map ⇒ Hash<Symbol,Hash>
private
All attributes exposed.
-
.expose(attribute, **options_hash) ⇒ Array<Symbol>
Expose attributes on json decorated.
Instance Method Summary collapse
-
#as_json(*args) ⇒ Hash
private
Builds hash / Json from the given object.
-
#initialize(object) ⇒ Decorator
constructor
private
A new instance of Decorator.
Constructor Details
#initialize(object) ⇒ Decorator #initialize(array) ⇒ Decorator
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Decorator.
245 246 247 |
# File 'lib/azeroth/decorator.rb', line 245 def initialize(object) @object = object end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Method called when method is missing
This delegates method calls to the given object
311 312 313 314 315 316 317 |
# File 'lib/azeroth/decorator.rb', line 311 def method_missing(method_name, *args) if object.respond_to?(method_name) object.public_send(method_name, *args) else super end end |
Class Method Details
.attributes_map ⇒ Hash<Symbol,Hash>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
All attributes exposed
53 54 55 |
# File 'lib/azeroth/decorator.rb', line 53 def attributes_map @attributes_map ||= build_attributes_map end |
.expose(attribute, **options_hash) ⇒ Array<Symbol>
Expose attributes on json decorated
228 229 230 231 232 233 234 235 236 |
# File 'lib/azeroth/decorator.rb', line 228 def expose(attribute, **) = Decorator::Options.new() builder = Sinclair.new(self) builder.add_method(attribute, "@object.#{attribute}") builder.build attributes_map[attribute] = end |
Instance Method Details
#as_json(*args) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds hash / Json from the given object
When object is an iterator, decoration is applied to each and an array is returned
259 260 261 262 263 264 265 |
# File 'lib/azeroth/decorator.rb', line 259 def as_json(*args) return nil if object.nil? return array_as_json(*args) if enum? HashBuilder.new(self).as_json end |