Sha256: 926f914942314b5fca7707906a1b14633efc196435a3e64c4dc27d9642cdb9ea

Contents?: true

Size: 846 Bytes

Versions: 5

Compression:

Stored size: 846 Bytes

Contents

# frozen_string_literal: true
module Timeful
  # Makes +Array+ behave much like +ActiveRecord::Relation+.
  #
  # This proxy class wraps an +Array+ or +ActiveRecord::Relation+ instance and makes the former
  # behave like the latter by simulating methods like +find_each+.
  #
  # @author Alessandro Desantis
  class RelationProxy < SimpleDelegator
    # If +find_each+ is defined on the object we're delegating to (i.e. the object is an instance
    # of +ActiveRecord::Relation+), calls it on the object.
    #
    # Otherwise, calls +each+ on the object and yields the values.
    #
    # @yieldparam item [Object] the items in the object
    def find_each(*args, &block)
      method = if __getobj__.respond_to?(:find_each)
        :find_each
      else
        :each
      end

      __getobj__.send(method, *args, &block)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
timeful-2.1.0 lib/timeful/relation_proxy.rb
timeful-2.0.0 lib/timeful/relation_proxy.rb
timeful-1.1.0 lib/timeful/relation_proxy.rb
timeful-1.0.0 lib/timeful/relation_proxy.rb
timeful-0.0.1 lib/timeful/relation_proxy.rb