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

オブジェクトの生成



251
252
253
254
255
256
257
258
# File 'lib/when_exe/parts/enumerator.rb', line 251

def initialize(*args)
  @options = self.class._options(args)
  @exdate  = @options.delete(:exdate)
  @exevent = @options.delete(:exevent)
  @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

巻き戻す



165
166
167
168
169
170
171
# File 'lib/when_exe/parts/enumerator.rb', line 165

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

#eachrewind された self

ブロックを評価する



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/when_exe/parts/enumerator.rb', line 101

def each
  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

#has_next?Boolean

次の要素があるか?



181
182
183
# File 'lib/when_exe/parts/enumerator.rb', line 181

def has_next?
  return (@current != nil)
end

#nextComparable

次の要素を取り出す

Raises:

  • (StopIteration)

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



191
192
193
194
195
# File 'lib/when_exe/parts/enumerator.rb', line 191

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

#succComparable

Note:

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

次の要素を取り出す



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/when_exe/parts/enumerator.rb', line 207

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
  return value
end

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

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



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/when_exe/parts/enumerator.rb', line 126

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 をブロックに渡して評価する



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/when_exe/parts/enumerator.rb', line 147

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