Sha256: 4468d9218290d9b559026d46050db3fb401d453452d237adeb4825e337293b60

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 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]&.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

1 entries across 1 versions & 1 rubygems

Version Path
pragma-decorator-2.0.0 lib/pragma/decorator/timestamp.rb