Class: Enumerator

Inherits:
Object show all
Includes:
Enumerable
Defined in:
opal/opal/corelib/enumerator/lazy.rb,
opal/opal/corelib/enumerator.rb,
opal/opal/corelib/enumerator/chain.rb,
opal/opal/corelib/enumerator/yielder.rb,
opal/opal/corelib/enumerator/generator.rb,
opal/opal/corelib/enumerator/arithmetic_sequence.rb

Overview

helpers: breaker

Direct Known Subclasses

self::ArithmeticSequence, self::Chain, self::Lazy

Defined Under Namespace

Classes: Generator, Yielder

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #chunk, #chunk_while, #collect, #collect_concat, #compact, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_object, #entries, #enumerator_size, #filter_map, #find_all, #find_index, #first, #grep, #grep_v, #group_by, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_h, #uniq, #zip

Constructor Details

#initialize(&block) ⇒ Enumerator

Returns a new instance of Enumerator.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'opal/opal/corelib/enumerator.rb', line 24

def initialize(*, &block)
  @cursor = 0
  if block
    @object = Generator.new(&block)
    @method = :each
    @args   = []
    @size   = `arguments[0] || nil`

    if @size && !@size.respond_to?(:call)
      @size = `$coerce_to(#{@size}, #{::Integer}, 'to_int')`
    end
  else
    @object = `arguments[0]`
    @method = `arguments[1] || "each"`
    @args   = `$slice.call(arguments, 2)`
    @size   = nil
  end
end

Class Method Details

.for(object, method = :each, *args, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'opal/opal/corelib/enumerator.rb', line 10

def self.for(object, method = :each, *args, &block)
  %x{
    var obj = #{allocate};

    obj.object = object;
    obj.size   = block;
    obj.method = method;
    obj.args   = args;
    obj.cursor = 0;

    return obj;
  }
end

Instance Method Details

#+(other) ⇒ Object



121
122
123
# File 'opal/opal/corelib/enumerator.rb', line 121

def +(other)
  ::Enumerator::Chain.new(self, other)
end

#each(*args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'opal/opal/corelib/enumerator.rb', line 43

def each(*args, &block)
  return self if block.nil? && args.empty?

  args = @args + args

  return self.class.new(@object, @method, *args) if block.nil?

  @object.__send__(@method, *args, &block)
end

#each_with_index(&block) ⇒ Object



82
83
84
85
86
87
# File 'opal/opal/corelib/enumerator.rb', line 82

def each_with_index(&block)
  return enum_for(:each_with_index) { size } unless block_given?

  super
  @object
end

#feed(arg) ⇒ Object



117
118
119
# File 'opal/opal/corelib/enumerator.rb', line 117

def feed(arg)
  raise NotImplementedError, "Opal doesn't support Enumerator#feed"
end

#inspectObject



125
126
127
128
129
130
131
132
133
# File 'opal/opal/corelib/enumerator.rb', line 125

def inspect
  result = "#<#{self.class}: #{@object.inspect}:#{@method}"

  if @args.any?
    result += "(#{@args.inspect[::Range.new(1, -2)]})"
  end

  result + '>'
end

#nextObject



112
113
114
115
# File 'opal/opal/corelib/enumerator.rb', line 112

def next
  values = next_values
  values.length <= 1 ? values[0] : values
end

#next_valuesObject



106
107
108
109
110
# File 'opal/opal/corelib/enumerator.rb', line 106

def next_values
  out = peek_values
  @cursor += 1
  out
end

#peekObject



101
102
103
104
# File 'opal/opal/corelib/enumerator.rb', line 101

def peek
  values = peek_values
  values.length <= 1 ? values[0] : values
end

#peek_valuesObject



95
96
97
98
99
# File 'opal/opal/corelib/enumerator.rb', line 95

def peek_values
  @values ||= map { |*i| i }
  ::Kernel.raise ::StopIteration, 'iteration reached an end' if @cursor >= @values.length
  @values[@cursor]
end

#rewindObject



89
90
91
92
93
# File 'opal/opal/corelib/enumerator.rb', line 89

def rewind
  @cursor = 0

  self
end

#sizeObject



53
54
55
# File 'opal/opal/corelib/enumerator.rb', line 53

def size
  @size.respond_to?(:call) ? @size.call(*@args) : @size
end

#with_index(offset = 0, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'opal/opal/corelib/enumerator.rb', line 57

def with_index(offset = 0, &block)
  offset = if offset
             `$coerce_to(offset, #{::Integer}, 'to_int')`
           else
             0
           end

  return enum_for(:with_index, offset) { size } unless block

  %x{
    var result, index = offset;

    self.$each.$$p = function() {
      var param = #{::Opal.destructure(`arguments`)},
          value = block(param, index);

      index++;

      return value;
    }

    return self.$each();
  }
end