class Shamu::Services::LazyTransform

Lazily transform one enumerable to another with shortcuts for common collection methods such as first, count, etc.

Attributes

source[R]
transformer[R]

Public Class Methods

new( source, &transformer ) click to toggle source

@param [Enumerable] source enumerable to transform. @yieldparam [Array<Object>] objects the original values. @yieldreturn the transformed values. @yield (object)

# File lib/shamu/services/lazy_transform.rb, line 13
def initialize( source, &transformer )
  @transformer = transformer
  @source      = source
end

Public Instance Methods

count( *args ) click to toggle source

(see Enumerable#count) @return [Integer]

Calls superclass method
# File lib/shamu/services/lazy_transform.rb, line 30
def count( *args )
  if args.any? || block_given?
    super
  else
    source.count
  end
end
Also aliased as: size, length
drop( n ) click to toggle source

@param [Integer] n number of source entries to skip. @return [LazyTransform] a new {LazyTransform} skipping `n` source

entries.
Calls superclass method
# File lib/shamu/services/lazy_transform.rb, line 91
def drop( n )
  if transformed?
    super
  else
    self.class.new( source.drop( n ), &transformer )
  end
end
each( &block ) click to toggle source

Yields each transformed value from the original source to the block.

@yield (object) @yieldparam [Object] object @return [self]

# File lib/shamu/services/lazy_transform.rb, line 23
def each( &block )
  transformed.each( &block )
  self
end
empty?() click to toggle source

@return [Boolean] true if there are no source values.

# File lib/shamu/services/lazy_transform.rb, line 73
def empty?
  source.empty?
end
first( *args ) click to toggle source

Get the first transformed value without transforming the entire list. @overload first(n) @overload first @return [Object]

Calls superclass method
# File lib/shamu/services/lazy_transform.rb, line 44
def first( *args )
  if args.any?
    super
  else
    return @first if defined? @first
    @first = begin
      value = source.first
      raise_if_not_transformed( transformer.call( [ value ] ) ).first unless value.nil?
    end
  end
end
last( *args ) click to toggle source

Get the last transformed value without transforming the entire list. @overload last(n) @overload last @return [Object]

# File lib/shamu/services/lazy_transform.rb, line 60
def last( *args )
  if args.any?
    transformed.last( *args )
  else
    return @last if defined? @last
    @last = begin
      value = source.last
      raise_if_not_transformed( transformer.call( [ value ] ) ).last unless value.nil?
    end
  end
end
length( *args )
Alias for: count
method_missing( name, *args, &block ) click to toggle source

For all other methods, force a transform then delegate to the transformed list.

Calls superclass method
# File lib/shamu/services/lazy_transform.rb, line 102
def method_missing( name, *args, &block )
  if respond_to_missing?( name, false )
    source.public_send( name, *args, &block )
  else
    super
  end
end
respond_to_missing?( *args ) click to toggle source
Calls superclass method
# File lib/shamu/services/lazy_transform.rb, line 110
def respond_to_missing?( *args )
  super || source.respond_to?( *args )
end
size( *args )
Alias for: count
take( n ) click to toggle source

@param [Integer] n number of source entries to take. @return [LazyTransform] a new {LazyTransform} taking only `n` source

entries.
Calls superclass method
# File lib/shamu/services/lazy_transform.rb, line 80
def take( n )
  if transformed?
    super
  else
    self.class.new( source.take( n ), &transformer )
  end
end

Private Instance Methods

raise_if_not_transformed( transformed ) click to toggle source
# File lib/shamu/services/lazy_transform.rb, line 127
def raise_if_not_transformed( transformed )
  raise "Block to LazyTransform did not return an enumerable value" unless transformed.is_a? Enumerable
  transformed
end
transformed() click to toggle source
# File lib/shamu/services/lazy_transform.rb, line 119
def transformed
  @transformed ||= raise_if_not_transformed( transformer.call( source ) )
end
transformed?() click to toggle source
# File lib/shamu/services/lazy_transform.rb, line 123
def transformed?
  !!@transformed
end