Sha256: 6f1d3c7b345136526c939bc56b0993ad913037214c5476c4e46791350aa23490

Contents?: true

Size: 1.32 KB

Versions: 14

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require 'sinclair'

module Azeroth
  class Decorator
    # @api private
    #
    # Class responsible for building the hash on
    # Decorator#as_json calls
    class HashBuilder
      # @param decorator [Decorator] decorator that
      #   will receive all method calls
      def initialize(decorator)
        @decorator = decorator
      end

      # Build hash as defined by decorator
      #
      # The built of the hash keys is defined
      # by decorator attributes and method calls
      # to the same decorator that will then delgate,
      # when needed, to its object
      #
      # @return [Hash]
      def as_json
        attributes_map.inject({}) do |hash, (method, options)|
          new_hash = KeyValueExtractor.new(decorator, method, options).as_json
          hash.merge!(new_hash)
        end
      end

      private

      attr_reader :decorator
      # @method decorator
      # @api private
      # @private
      #
      # decorator with object to be decorated
      #
      # @return [Decorator]

      # attributes to be built to hash
      #
      # The keys are the methods to be called and
      # the values are the exposing options
      #
      # @return [Hash] hash of pairs method,options
      def attributes_map
        decorator.class.attributes_map
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
azeroth-0.10.1 lib/azeroth/decorator/hash_builder.rb
azeroth-0.10.0 lib/azeroth/decorator/hash_builder.rb
azeroth-0.9.0 lib/azeroth/decorator/hash_builder.rb
azeroth-0.8.2 lib/azeroth/decorator/hash_builder.rb
azeroth-0.8.1 lib/azeroth/decorator/hash_builder.rb
azeroth-0.8.0 lib/azeroth/decorator/hash_builder.rb
azeroth-0.7.4 lib/azeroth/decorator/hash_builder.rb
azeroth-0.7.3 lib/azeroth/decorator/hash_builder.rb
azeroth-0.7.2 lib/azeroth/decorator/hash_builder.rb
azeroth-0.7.1 lib/azeroth/decorator/hash_builder.rb
azeroth-0.7.0 lib/azeroth/decorator/hash_builder.rb
azeroth-0.6.5 lib/azeroth/decorator/hash_builder.rb
azeroth-0.6.4 lib/azeroth/decorator/hash_builder.rb
azeroth-0.6.3 lib/azeroth/decorator/hash_builder.rb