Sha256: 69b6410c112db409bffebcfd860c1270f80e4a035a3d03dac9d659a4f6fa22e6

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Pragma
  module Decorator
    # Supports rendering timestamps as UNIX times.
    #
    # @author Alessandro Desantis
    module Timestamp
      def self.included(klass)
        klass.extend ClassMethods
      end

      module ClassMethods # rubocop:disable Style/Documentation
        # Defines a timestamp property which will be rendered as UNIX time.
        #
        # @param name [Symbol] the name of the property
        # @param options [Hash] the options of the property
        def timestamp(name, options = {})
          create_timestamp_getter(name, options)
          create_timestamp_property(name, options)
        end

        private

        def create_timestamp_getter(name, options = {})
          define_method "_#{name}_timestamp" do
            if options[:exec_context] && options[:exec_context].to_sym == :decorator
              send(name)
            else
              decorated.send(name)
            end&.to_i
          end
        end

        def create_timestamp_property(name, options = {})
          property "_#{name}_timestamp", options.merge(
            as: name,
            exec_context: :decorator
          )
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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