class Shamu::Services::LazyTransform
Lazily transform one enumerable to another with shortcuts for common collection methods such as first, count, etc.
Attributes
Public Class Methods
@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
(see Enumerable#count) @return [Integer]
# File lib/shamu/services/lazy_transform.rb, line 30 def count( *args ) if args.any? || block_given? super else source.count end end
@param [Integer] n number of source entries to skip. @return [LazyTransform] a new {LazyTransform} skipping `n` source
entries.
# 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
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
@return [Boolean] true if there are no source values.
# File lib/shamu/services/lazy_transform.rb, line 73 def empty? source.empty? end
Get the first transformed value without transforming the entire list. @overload first(n) @overload first @return [Object]
# 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
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
For all other methods, force a transform then delegate to the transformed list.
# 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
# File lib/shamu/services/lazy_transform.rb, line 110 def respond_to_missing?( *args ) super || source.respond_to?( *args ) end
@param [Integer] n number of source entries to take. @return [LazyTransform] a new {LazyTransform} taking only `n` source
entries.
# 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
# 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
# File lib/shamu/services/lazy_transform.rb, line 119 def transformed @transformed ||= raise_if_not_transformed( transformer.call( source ) ) end
# File lib/shamu/services/lazy_transform.rb, line 123 def transformed? !!@transformed end