Class: Azeroth::Decorator

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/azeroth/decorator.rb,
lib/azeroth/decorator/options.rb,
lib/azeroth/decorator/hash_builder.rb,
lib/azeroth/decorator/class_methods.rb,
lib/azeroth/decorator/method_builder.rb,
lib/azeroth/decorator/key_value_extractor.rb

Overview

Class to be used when decorating outputs

Examples:

class DummyModel
  include ActiveModel::Model

  attr_accessor :id, :first_name, :last_name, :age,
                :favorite_pokemon

  class Decorator < Azeroth::Decorator
    expose :name
    expose :age
    expose :favorite_pokemon, as: :pokemon

    def name
      [object.first_name, object.last_name].join(' ')
    end
  end
end

model = DummyModel.new(
  id: 100,
  first_name: 'John',
  last_name: 'Wick',
  favorite_pokemon: 'Arcanine'
)

decorator = DummyModel::Decorator.new(model)

decorator.as_json # returns {
                  #   'name'    => 'John Wick',
                  #   'age'     => nil,
                  #   'pokemon' => 'Arcanine'
                  # }

Defined Under Namespace

Modules: ClassMethods Classes: HashBuilder, KeyValueExtractor, MethodBuilder, Options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

attributes_map, expose

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.

Overloads:

  • #initialize(object) ⇒ Decorator

    Parameters:

    • object (Object)

      to be decorated

  • #initialize(array) ⇒ Decorator

    Parameters:

    • array (Array)

      array of objects to be decorated



86
87
88
# File 'lib/azeroth/decorator.rb', line 86

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

Parameters:

  • method_name (Symbol)

    name of the method called

  • args (Array<Object>)

    arguments of the method called

Returns:

  • (Object)


152
153
154
155
156
157
158
# File 'lib/azeroth/decorator.rb', line 152

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

.expose(attribute, **options_hash) ⇒ Array<Symbol>

Expose attributes on json decorated

Parameters:

  • attribute (Symbol, String)

    attribute to be exposed

  • options_hash (Hash)

    exposing options

Options Hash (**options_hash):

  • as (Symbol, String)

    custom key to expose the value as

  • if (Symbol, Proc)

    method/block to be called checking if an attribute should or should not be exposed

  • decorator (FalseClass, TrueClass, Class)

    flag to use or not a decorator or decorator class to be used

  • reader (Boolean)

    Flag indicating if a reader to access the attribute should be created. usefull if you want method_missing to take over

  • override (Boolean)

    Flag indicating if an existing method should be overriden. This is useful when a method acessor was included from another module

Returns:

  • (Array<Symbol>)

See Also:



# File 'lib/azeroth/decorator.rb', line 51

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

Parameters:

  • args (Hash)

    options (to be implemented)

Returns:

  • (Hash)


100
101
102
103
104
105
106
# File 'lib/azeroth/decorator.rb', line 100

def as_json(*args)
  return nil if object.nil?

  return array_as_json(*args) if enum?

  HashBuilder.new(self).as_json
end