Class: Shamu::Services::LazyTransform
- Inherits:
-
Object
- Object
- Shamu::Services::LazyTransform
- Includes:
- Enumerable
- Defined in:
- lib/shamu/services/lazy_transform.rb
Overview
Lazily transform one enumerable to another with shortcuts for common collection methods such as first, count, etc.
Instance Method Summary collapse
- #count(*args) ⇒ Integer (also: #size, #length)
-
#drop(n) ⇒ LazyTransform
A new LazyTransform skipping
n
source entries. -
#each {|object| ... } ⇒ self
Yields each transformed value from the original source to the block.
-
#empty? ⇒ Boolean
True if there are no source values.
-
#first(*args) ⇒ Object
Get the first transformed value without transforming the entire list.
-
#initialize(source) {|object| ... } ⇒ LazyTransform
constructor
A new instance of LazyTransform.
-
#last(*args) ⇒ Object
Get the last transformed value without transforming the entire list.
-
#method_missing(name, *args, &block) ⇒ Object
For all other methods, force a transform then delegate to the transformed list.
- #respond_to_missing?(*args) ⇒ Boolean
-
#take(n) ⇒ LazyTransform
A new LazyTransform taking only
n
source entries.
Constructor Details
#initialize(source) {|object| ... } ⇒ LazyTransform
Returns a new instance of LazyTransform.
13 14 15 16 |
# File 'lib/shamu/services/lazy_transform.rb', line 13 def initialize( source, &transformer ) @transformer = transformer @source = source end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
For all other methods, force a transform then delegate to the transformed list.
102 103 104 105 106 107 108 |
# 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 |
Instance Method Details
#count(*args) ⇒ Integer Also known as: size, length
30 31 32 33 34 35 36 |
# File 'lib/shamu/services/lazy_transform.rb', line 30 def count( *args ) if args.any? || block_given? super else source.count end end |
#drop(n) ⇒ LazyTransform
Returns a new Shamu::Services::LazyTransform skipping n
source
entries.
91 92 93 94 95 96 97 |
# 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 {|object| ... } ⇒ self
Yields each transformed value from the original source to the block.
23 24 25 26 |
# File 'lib/shamu/services/lazy_transform.rb', line 23 def each( &block ) transformed.each( &block ) self end |
#empty? ⇒ Boolean
Returns true if there are no source values.
73 74 75 |
# File 'lib/shamu/services/lazy_transform.rb', line 73 def empty? source.empty? end |
#first(n) ⇒ Object #first ⇒ Object
Get the first transformed value without transforming the entire list.
44 45 46 47 48 49 50 51 52 53 54 |
# 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(n) ⇒ Object #last ⇒ Object
Get the last transformed value without transforming the entire list.
60 61 62 63 64 65 66 67 68 69 70 |
# 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 |
#respond_to_missing?(*args) ⇒ Boolean
110 111 112 |
# File 'lib/shamu/services/lazy_transform.rb', line 110 def respond_to_missing?( *args ) super || source.respond_to?( *args ) end |
#take(n) ⇒ LazyTransform
Returns a new Shamu::Services::LazyTransform taking only n
source
entries.
80 81 82 83 84 85 86 |
# 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 |