Class: Azeroth::Decorator::KeyValueExtractor Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • decorator (Decorator)

    decorator object

  • attribute (Symbol)

    attribute to be used on output hash

  • options (Decorator::Options)

    decoration options

Options Hash (options):

  • if (Proc, Symbol)

    conditional to be checked when exposing field (see Options#if)



21
22
23
24
25
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 21

def initialize(decorator, attribute, options)
  @decorator = decorator
  @attribute = attribute
  @options = options
end

Instance Attribute Details

#attributeSymbol (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

Returns:

  • (Symbol)


43
44
45
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 43

def attribute
  @attribute
end

#decoratorDecorator (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

Returns:



43
44
45
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 43

def decorator
  @decorator
end

#optionsDecorator::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

Returns:



43
44
45
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 43

def options
  @options
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

Returns:

  • (Object)

    result of method call from decorator



123
124
125
126
127
128
129
130
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 123

def add_attribute?
  conditional = options.if
  return true unless conditional.present?

  block = proc(&conditional)

  block.call(decorator)
end

#as_jsonHash

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

Returns:

  • (Hash)

    hash in the format { attribute => decorated_value }



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 = options.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

Returns:



90
91
92
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 90

def decorator_class(object)
  decorator_from_options || 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

Returns:



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_optionsClass<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

Returns:



99
100
101
102
103
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 99

def decorator_from_options
  return options.decorator if options.decorator_defined?

  Azeroth::DummyDecorator unless options.decorator
end

#json_valueObject

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

Returns:

  • (Object)


73
74
75
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 73

def json_value
  decorator_class(value).new(value).as_json
end

#valueObject

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

Returns:

  • (Object)


81
82
83
# File 'lib/azeroth/decorator/key_value_extractor.rb', line 81

def value
  @value ||= decorator.public_send(attribute)
end