Class: Azeroth::Decorator::KeyValueExtractor Private
- Inherits:
-
Object
- Object
- Azeroth::Decorator::KeyValueExtractor
- Defined in:
- lib/azeroth/decorator/key_value_extractor.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Class responsible to extract the value for an attribute
The value is extracted by sending a method call to the decorator
A decorator is infered for the value / attribute or used from the options given
Instance Attribute Summary collapse
-
#attribute ⇒ Symbol
readonly
private
attribute to be exposed.
-
#decorator ⇒ Decorator
readonly
private
decorator with object to be decorated.
-
#options ⇒ Decorator::Options
readonly
private
exposing options.
Instance Method Summary collapse
-
#add_attribute? ⇒ Object
private
Check if an attribute should be added to the hash.
-
#as_json ⇒ Hash
private
Return hash for attribute.
-
#decorator_class(object) ⇒ Class<Decorator>
private
Finds the correct decorator class for a value.
-
#decorator_class_for(object) ⇒ Class<Decorator>
private
Finds the correct decorator class for a value.
-
#decorator_from_options ⇒ Class<Decorator>, NilClass
private
returns decorator defined in options.
-
#initialize(decorator, attribute, options) ⇒ KeyValueExtractor
constructor
private
A new instance of KeyValueExtractor.
-
#json_value ⇒ Object
private
returns the value ready to be transformed into json.
-
#value ⇒ Object
private
Retruns the value extracted from decorator.
Constructor Details
#initialize(decorator, attribute, options) ⇒ KeyValueExtractor
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 KeyValueExtractor.
21 22 23 24 25 |
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 21 def initialize(decorator, attribute, ) @decorator = decorator @attribute = attribute = end |
Instance Attribute Details
#attribute ⇒ Symbol (readonly)
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.
attribute to be exposed
43 44 45 |
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 43 def attribute @attribute end |
#decorator ⇒ Decorator (readonly)
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.
decorator with object to be decorated
43 44 45 |
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 43 def decorator @decorator end |
#options ⇒ Decorator::Options (readonly)
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.
exposing options
43 44 45 |
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 43 def end |
Instance Method Details
#add_attribute? ⇒ Object
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.
Check if an attribute should be added to the hash
123 124 125 126 127 128 129 130 |
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 123 def add_attribute? conditional = .if return true unless conditional.present? block = proc(&conditional) block.call(decorator) end |
#as_json ⇒ 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.
Return hash for attribute
31 32 33 34 35 36 37 38 39 |
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 31 def as_json return {} unless add_attribute? key = .as || attribute { key.to_s => json_value } end |
#decorator_class(object) ⇒ Class<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.
Finds the correct decorator class for a value
90 91 92 |
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 90 def decorator_class(object) || decorator_class_for(object) end |
#decorator_class_for(object) ⇒ Class<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.
Finds the correct decorator class for a value
110 111 112 113 114 115 116 |
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 110 def decorator_class_for(object) return object.class::Decorator unless object.is_a?(Enumerable) decorator_class_for(object.first) rescue NameError Azeroth::DummyDecorator end |
#decorator_from_options ⇒ Class<Decorator>, NilClass
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 decorator defined in options
99 100 101 102 103 |
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 99 def return .decorator if .decorator_defined? Azeroth::DummyDecorator unless .decorator end |
#json_value ⇒ Object
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 the value ready to be transformed into json
73 74 75 |
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 73 def json_value decorator_class(value).new(value).as_json end |
#value ⇒ Object
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.
Retruns the value extracted from decorator
81 82 83 |
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 81 def value @value ||= decorator.public_send(attribute) end |