Class: Shamu::Services::LazyTransform

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(source) {|object| ... } ⇒ LazyTransform

Returns a new instance of LazyTransform.

Parameters:

  • source (Enumerable)

    enumerable to transform.

Yields:

  • (object)

Yield Parameters:

  • objects (Array<Object>)

    the original values.

Yield Returns:

  • the transformed values.



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

Returns:

  • (Integer)


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.

Parameters:

  • n (Integer)

    number of source entries to skip.

Returns:



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.

Yields:

  • (object)

Yield Parameters:

  • object (Object)

Returns:

  • (self)


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.

Returns:

  • (Boolean)

    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 #firstObject

Get the first transformed value without transforming the entire list.

Returns:

  • (Object)


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 #lastObject

Get the last transformed value without transforming the entire list.

Returns:

  • (Object)


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

Returns:

  • (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.

Parameters:

  • n (Integer)

    number of source entries to take.

Returns:



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