lib/shamu/services/lazy_transform.rb in shamu-0.0.13 vs lib/shamu/services/lazy_transform.rb in shamu-0.0.14

- old
+ new

@@ -51,10 +51,26 @@ 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] + 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 + # @return [Boolean] true if there are no source values. def empty? source.empty? end @@ -76,9 +92,24 @@ if transformed? super else self.class.new( source.drop( n ), &transformer ) end + end + + # For all other methods, force a transform then delegate to the + # transformed list. + + def method_missing( name, *args, &block ) + if respond_to_missing?( name, false ) + source.public_send( name, *args, &block ) + else + super + end + end + + def respond_to_missing?( *args ) + super || source.respond_to?( *args ) end private attr_reader :source