Sha256: 834d16e5be9fbcb43a49e8418ddc2a6eee0914dac8792fb8dc25c9547fa1cf86

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

module ChronoTest::TimeMachine

  # This module contains helpers used throughout the
  # +ChronoModel::TimeMachine+ specs.
  #
  module Helpers
    def self.included(base)
      base.extend(self)
    end

    def adapter
      ChronoTest.connection
    end

    def with_revert
      adapter.transaction do
        adapter.create_savepoint 'revert'

        yield

        adapter.exec_rollback_to_savepoint 'revert'
      end
    end

    # If a context object is given, evaluates the given
    # block in its instance context, then defines a `ts`
    # on it, backed by an Array, and adds the current
    # database timestamp to it.
    #
    # If a context object is not given, the block is
    # evaluated in the current context and the above
    # mangling is done on the blocks' return value.
    #
    def ts_eval(ctx = nil, &block)
      ret = (ctx || self).instance_eval(&block)
      (ctx || ret).tap do |obj|
        obj.singleton_class.instance_eval do
          define_method(:ts) { @_ts ||= [] }
        end unless obj.methods.include?(:ts)

        now = ChronoTest.connection.select_value('select now()::timestamp') + 'Z'
        obj.ts.push(Time.parse(now))
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chrono_model-1.2.2 spec/support/time_machine/helpers.rb
chrono_model-1.2.1 spec/support/time_machine/helpers.rb