Sha256: c73dc34abc8af90afac5b2732527560b85bb476ff0ab61b8e7c73d8b2612c915

Contents?: true

Size: 782 Bytes

Versions: 4

Compression:

Stored size: 782 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

4 entries across 4 versions & 1 rubygems

Version Path
pragma-decorator-1.1.0 lib/pragma/decorator/type.rb
pragma-decorator-1.0.1 lib/pragma/decorator/type.rb
pragma-decorator-1.0.0 lib/pragma/decorator/type.rb
pragma-decorator-0.1.0 lib/pragma/decorator/type.rb