Sha256: 2b8abf4de954c59d7b2272ca13a186f4798cdb15fc22d52c85af08a58ac33382

Contents?: true

Size: 783 Bytes

Versions: 3

Compression:

Stored size: 783 Bytes

Contents

# frozen_string_literal: true

module Pragma
  module Decorator
    # Adds a +type+ property containing the machine-readable type of the represented object.
    #
    # @author Alessandro Desantis
    module Type
      TYPE_OVERRIDES = {
        array: 'list'
      }.freeze

      def self.included(klass)
        klass.class_eval do
          property :type, exec_context: :decorator, render_nil: false
        end
      end

      # Returns the type of the decorated object (i.e. its underscored class name).
      #
      # @return [String]
      def type
        type = decorated.class.name
          .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
          .gsub(/([a-z\d])([A-Z])/, '\1_\2')
          .downcase

        TYPE_OVERRIDES[type.to_sym] || type
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pragma-decorator-1.3.0 lib/pragma/decorator/type.rb
pragma-decorator-1.2.0 lib/pragma/decorator/type.rb
pragma-decorator-2.0.0 lib/pragma/decorator/type.rb