Class: Seq

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/seq.rb,
lib/seq/lazy.rb,
lib/seq/paged.rb,
lib/seq/pager.rb,
lib/seq/random.rb,
lib/seq/version.rb

Overview

A Seq will cycle over a list returning a certain number of items.

Examples:

With number of items to return


s = Seq.new([1, 2], 4)
s.next #=> 1
s.next #=> 2
s.next #=> 1
s.next #=> 2
s.next #=> nil

With an offset


s = Seq.new([1, 2, 3, 4, 5], 5, 3)
s.next #=> 4
s.next #=> 5
s.next #=> 1
# etc

With default value


s = Seq.new([1, 2], 2, 0, 2)
s.next #=> 1
s.next #=> 2
s.next #=> 2
s.next #=> 2

Direct Known Subclasses

Lazy, Paged, Pager, Random

Defined Under Namespace

Classes: Lazy, Paged, Pager, Random

Constant Summary collapse

VERSION =
"0.3.0"

Instance Method Summary collapse

Constructor Details

#initialize(list = [], items = Float::INFINITY, offset = 0, default = nil) ⇒ Seq

Creates a new instance of Seq.

Parameters:

  • list (Array) (defaults to: [])

    List of values to cycle over

  • items (Integer) (defaults to: Float::INFINITY)

    Number of values to return

  • offset (Integer) (defaults to: 0)

    Index of item in list to start at

  • default (Object) (defaults to: nil)

    Value to return when finished cycling



43
44
45
46
47
48
49
50
# File 'lib/seq.rb', line 43

def initialize(list=[], items=Float::INFINITY, offset=0, default=nil)
  @list    = list
  @items   = items
  @offset  = offset
  @default = default

  self.reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Any method called on a Seq with ending with !, will be caught by method missing, it will then attempt to call the non ! version but will not alter the index or number of cycles completed.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/seq.rb', line 120

def method_missing(sym, *args, &block)
  if sym.to_s[-1] == "!" &&
    self.respond_to?(sym.to_s[0..-2].to_sym) &&
    ! [:infinite?, :ended?, :to_a, :entries, :inc, :reset].include?(sym.to_s[0..-2].to_sym)

    begin
      i, c = @index, @cycles
      self.send(sym.to_s[0..-2].to_sym, *args, &block)
    ensure
      @index, @cycles = i, c
    end
  else
    super
  end
end

Instance Method Details

#eachObject

Iterates over each item as returned by #next until #ended?.



82
83
84
85
86
# File 'lib/seq.rb', line 82

def each
  until ended?
    yield self.next
  end
end

#ended?Boolean

Returns Whether the Seq has returned enough items.

Returns:

  • (Boolean)

    Whether the Seq has returned enough items.



113
114
115
# File 'lib/seq.rb', line 113

def ended?
  @cycles >= @items
end

#entriesArray

Returns The items that would be returned by repeated calls to #next until #ended?.

Returns:

  • (Array)

    The items that would be returned by repeated calls to #next until #ended?.

Raises:

  • (RangeError)

    If #infinite?, otherwise it creates an infinite loop!



93
94
95
96
97
98
99
100
# File 'lib/seq.rb', line 93

def entries
  raise RangeError if infinite?

  i, c = @index, @cycles
  r = super
  @index, @cycles = i, c
  r
end

#incInteger

Increment the list index, the number of cycles completed and if at the end of the list returns to the first item.

Returns:

  • (Integer)

    Number of items that have been returned.



72
73
74
75
76
77
78
79
# File 'lib/seq.rb', line 72

def inc
  if @index+1 == @list.size
    @index = 0
  else
    @index += 1
  end
  @cycles += 1
end

#infinite?Boolean

Returns Whether the Seq returns infinite items.

Returns:

  • (Boolean)

    Whether the Seq returns infinite items.



108
109
110
# File 'lib/seq.rb', line 108

def infinite?
  @items == Float::INFINITY
end

#nextObject

Returns Until ended it returns the next item from the list, when ended it returns the default item.

Returns:

  • Until ended it returns the next item from the list, when ended it returns the default item.



60
61
62
63
64
65
66
# File 'lib/seq.rb', line 60

def next
  if ended?
    @default
  else
    @list[@index].tap { inc }
  end
end

#resetObject

Resets the Seqs position to the same as when initialized.



53
54
55
56
# File 'lib/seq.rb', line 53

def reset
  @cycles = 0
  @index  = @offset
end

#to_aArray

Returns the original list

Returns:

  • (Array)

    Returns the original list



103
104
105
# File 'lib/seq.rb', line 103

def to_a
  @list
end