Sha256: cc7668004d13a5f83c5dc8bbce08899e9adce2021444d55b82bc2fe90616d931

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'roar/decorator'
require 'roar/json'

module Pragma
  module Decorator
    # This is the base decorator that all your resource-specific decorators should extend from.
    #
    # It is already configured to render your resources in JSON.
    #
    # @author Alessandro Desantis
    class Base < Roar::Decorator
      feature Roar::JSON

      # Overrides Representable's default +#to_hash+ to save the last options the method was run
      # with.
      #
      # This allows accessing the options from property getters and is required by {Association}.
      #
      # @param options [Hash]
      #
      # @return [Hash]
      def to_hash(options = {}, *args)
        @last_options = options
        super(options, *args)
      end

      protected

      # Returns the options +#to_hash+ was last run with.
      #
      # @return [Hash]
      def options
        @last_options
      end

      # Returns the user options +#to_hash+ was last run with.
      #
      # @return [Hash]
      #
      # @see #options
      def user_options
        @last_options[:user_options] || {}
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pragma-decorator-1.3.0 lib/pragma/decorator/base.rb
pragma-decorator-1.2.0 lib/pragma/decorator/base.rb