Class: When::Parts::Enumerator

Inherits:
Enumerator
  • Object
show all
Defined in:
lib/when_exe/parts/enumerator.rb,
lib/when_exe/parts/enumerator.rb

Overview

本ライブラリ用の Enumerator の雛形

Defined Under Namespace

Classes: Array, Integrated

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, range, count_limit = nil) ⇒ Enumerator #initialize(parent, first, direction, count_limit = nil) ⇒ Enumerator

オブジェクトの生成



271
272
273
274
275
276
277
278
279
# File 'lib/when_exe/parts/enumerator.rb', line 271

def initialize(*args)
  @options = self.class._options(args)
  @exdate  = @options.delete(:exdate)
  @exevent = @options.delete(:exevent)
  @delayed = @options.delete(:delayed)
  @parent, *rest = args
  _range(rest)
  _rewind
end

Instance Attribute Details

#countInteger (readonly)

現在の繰り返し回数



78
79
80
# File 'lib/when_exe/parts/enumerator.rb', line 78

def count
  @count
end

#count_limitInteger (readonly)

最大繰り返し回数



74
75
76
# File 'lib/when_exe/parts/enumerator.rb', line 74

def count_limit
  @count_limit
end

#currentComparable

現在の要素



82
83
84
# File 'lib/when_exe/parts/enumerator.rb', line 82

def current
  @current
end

#directionSymbol (readonly)

繰り返し方向



70
71
72
# File 'lib/when_exe/parts/enumerator.rb', line 70

def direction
  @direction
end

#exdateWhen::Parts::GeometricComplex

除外要素



48
49
50
# File 'lib/when_exe/parts/enumerator.rb', line 48

def exdate
  @exdate
end

#firstComparable

最初の要素



58
59
60
# File 'lib/when_exe/parts/enumerator.rb', line 58

def first
  @first
end

#indexInteger (readonly)

現在のインデックス



88
89
90
# File 'lib/when_exe/parts/enumerator.rb', line 88

def index
  @index
end

#lastComparable

最後の要素



63
64
65
# File 'lib/when_exe/parts/enumerator.rb', line 63

def last
  @last
end

#objectComparable

with_object メソッドで渡すインスタンス



93
94
95
# File 'lib/when_exe/parts/enumerator.rb', line 93

def object
  @object
end

#optionsHash

オプション



43
44
45
# File 'lib/when_exe/parts/enumerator.rb', line 43

def options
  @options
end

#parentComparable (readonly)

生成元オブジェクト



38
39
40
# File 'lib/when_exe/parts/enumerator.rb', line 38

def parent
  @parent
end

#processedWhen::Parts::GeometricComplex

処理済み要素



53
54
55
# File 'lib/when_exe/parts/enumerator.rb', line 53

def processed
  @processed
end

Class Method Details

._options(args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/when_exe/parts/enumerator.rb', line 25

def self._options(args)
  options  = args[-1].kind_of?(Hash) ? args.pop.dup : {}
  options[:exdate] =
    case options[:exdate]
    when GeometricComplex ; options[:exdate].dup
    when nil ; GeometricComplex.new()
    else ; GeometricComplex.new(options[:exdate])
    end
  return options
end

Instance Method Details

#_rewindrewind された self Also known as: rewind

巻き戻す



174
175
176
177
178
179
180
# File 'lib/when_exe/parts/enumerator.rb', line 174

def _rewind
  @processed = @exdate.dup
  @count     = 0
  @current   = :first
  succ
  self
end

#each(options = {}) ⇒ rewind された self

ブロックを評価する

@param [Hash] options 以下の通り
@option options [Symbol]     :direction   方向(:forward/:reverse)
@option options [Comparable] :until       終了閾値
@option options [Integer]    :count_limit 繰り返し回数


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/when_exe/parts/enumerator.rb', line 106

def each(options={})
  @direction   = options[:direction] if options.key?(:direction)
  @last        = @direction == :reverse ? [options[:until], @last].compact.max :
                                          [options[:until], @last].compact.min
  @count_limit = [options[:count_limit], @count_limit].compact.min
  return self unless block_given?
  while (has_next?) do
    if @index
      yield(succ, @index)
      @index += 1
    elsif @object
      yield(succ, @object)
    else
      yield(succ)
    end
  end
  @index = @object = nil
  rewind
end

#exclude_endBoolean

終端要素は含む



250
251
252
# File 'lib/when_exe/parts/enumerator.rb', line 250

def exclude_end
  false
end

#has_next?Boolean

次の要素があるか?



190
191
192
# File 'lib/when_exe/parts/enumerator.rb', line 190

def has_next?
  return (@count_limit != 0) && !@current.nil?
end

#nextComparable

次の要素を取り出す

Raises:

  • (StopIteration)

    次の要素がない場合 rewind して例外を発生



200
201
202
203
204
# File 'lib/when_exe/parts/enumerator.rb', line 200

def next
  return succ if has_next?
  rewind
  raise StopIteration, "Iteration Stopped"
end

#succComparable

Note:

次の要素がない場合 rewind や、StopIteration例外発生は行わない

次の要素を取り出す



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/when_exe/parts/enumerator.rb', line 216

def succ
  value = @current
  if @count_limit.kind_of?(Numeric) && @count >= @count_limit
    @current = nil
  else
    loop do
      @current = _succ
      break unless @current
      next if (@current == :next)
      @current = GeometricComplex.new(@current, @duration) if @duration
      next if _exclude(@current)
      case @direction
      when :reverse
        next if @current > @first
        @current = nil if @last && @current < @last
        break
      else
        next if @current < @first
        @current = nil if @last && @current > @last
        break
      end
    end
    @count += 1
    _exclude(@current) if @current
  end
  @delayed && value.respond_to?(:apply_delayed_options) ?
    value.apply_delayed_options(@delayed)               :
    value
end

#with_index(offset = 0, &block) ⇒ When::Parts:Enumerator

index をブロックに渡して評価する



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/when_exe/parts/enumerator.rb', line 135

def with_index(offset=0, &block)
  if block_given?
    @index = offset||@count
    return each(block)
  else
    copy = _copy
    copy.object = nil
    copy.index  = offset||@count
    return copy
  end
end

#with_object(object, &block) ⇒ When::Parts:Enumerator

index をブロックに渡して評価する



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/when_exe/parts/enumerator.rb', line 156

def with_object(object, &block)
  if block_given?
    @object = object
    each(block)
    return object
  else
    copy = _copy
    copy.object = object
    copy.index  = nil
    return copy
  end
end