Sha256: 355b68ea160bfc4f482d1443ceb5d3b2c70a22b85cb880a5de3c80106239a94c

Contents?: true

Size: 1.2 KB

Versions: 4

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

4 entries across 4 versions & 1 rubygems

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