Class: When::Events::Range

Inherits:
Range
  • Object
show all
Defined in:
lib/when_exe/events.rb

Overview

イベント管理用範囲オブジェクト

Constant Summary collapse

Delta =

実数のための exclude_end? 判定用マージン

When::TM::Duration::SECOND / 4096.0 / When::TM::Duration::DAY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first, last, exclude_end = false) ⇒ Range

イベント管理用範囲オブジェクトの生成

Parameters:

  • first (Object)

    小さい方の境界

  • last (Object)

    大きい方の境界

  • exclude_end (Boolean) (defaults to: false)

    大きい方の境界を含まないか?



181
182
183
184
185
186
187
# File 'lib/when_exe/events.rb', line 181

def initialize(first, last, exclude_end=false)
  @start = first
  @until = last
  range  = [first, last].map {|edge| edge.respond_to?(:first) ? edge.first : edge}
  rase ArgumentError, "#{range.last} is less than #{range.first}" if range.last < range.first
  super(range.first, range.last, exclude_end)
end

Instance Attribute Details

#originalString

オリジナル文字列

Returns:



44
45
46
# File 'lib/when_exe/events.rb', line 44

def original
  @original
end

#startObject (readonly)

小さい方の境界

Returns:

  • (Object)


32
33
34
# File 'lib/when_exe/events.rb', line 32

def start
  @start
end

#untilObject (readonly)

大きい方の境界

Returns:

  • (Object)


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

def until
  @until
end

Class Method Details

.convert_from(source) ⇒ When::Events::Range

オブジェクトを When::Events::Range 型に変換する

Parameters:

  • source (Object)

    変換元のオブジェクト

Returns:



53
54
55
56
57
58
59
60
# File 'lib/when_exe/events.rb', line 53

def self.convert_from(source)
  case
  when source.kind_of?(self)             ; source
  when source.respond_to?(:exclude_end?) ; new(source, source.last, source.exclude_end?)
  when source.respond_to?(:succ)         ; new(source, source.succ, true)
  else raise ArgumentError, "Can't convert #{source} to #{self}"
  end
end

Instance Method Details

#include(target) ⇒ Array<Object>

含む対象の抽出

Parameters:

  • target (Object)

    判定するオブジェクト

Returns:

  • (Array<Object>)

    実際に包含した範囲オブジェクトの Array



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/when_exe/events.rb', line 89

def include(target)
  return [] if (exclude_end? && @until <= target) || (!exclude_end? && @until < target)

  case @start
  when ::Range
    return @start.include?(target) ? [@start] : []

  when Array
    list = []
    @start.each do |range|
      break if _range_exceeded?(range, target)
      list << range if _target_included?(range, target)
    end
    return list

  when Enumerator
    begin
      list = []
      while (range = @start.succ)
        break if _range_exceeded?(range, target)
        list << range if _target_included?(range, target)
      end
      return list
    ensure
      @start._rewind
    end
 
  else
    return @start <= target ? [@start] : []
  end
end

#include?(target) ⇒ Boolean

指定オブジェクトが範囲内か?

Parameters:

  • target (Object)

    判定するオブジェクト

Returns:

  • (Boolean)

    true - 範囲内のオブジェクトあり, false - 範囲内のオブジェクトなし



79
80
81
# File 'lib/when_exe/events.rb', line 79

def include?(target)
  !include(target).empty?
end

#is_complex?Boolean

範囲の重なりの判断が複雑になるか?

Returns:

  • (Boolean)

    true - 複雑, false - 単純



66
67
68
69
70
71
# File 'lib/when_exe/events.rb', line 66

def is_complex?
  case @start
  when Array, Enumerator ; true
  else                   ; false
  end
end

#is_overlaped?(range) ⇒ Boolean

範囲の重なりの判断

Parameters:

  • range (Range)

    確認対象の単純範囲オブジェクト

Returns:

  • (Boolean)

    true - 重なる, false - 重ならない



128
129
130
# File 'lib/when_exe/events.rb', line 128

def is_overlaped?(range)
  !overlaped(range).empty?
end

#overlaped(target) ⇒ Array<Object>

範囲が重なる対象の抽出

Parameters:

  • target (Range)

    確認対象の単純範囲オブジェクト

Returns:

  • (Array<Object>)

    実際に重なった範囲オブジェクトの Array



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/when_exe/events.rb', line 139

def overlaped(target)
  first = target.first
  last  = target.last
  last  = last.respond_to?(:prev) ? last.prev : last - Delta if target.exclude_end?
  return [] if (exclude_end? && @until <= first) || (!exclude_end? && @until < first)

  case @start
  when ::Range
    return _target_exceeded?(@start, first) || _range_exceeded?(@start, last) ? [] : [@start]

  when Array
    list = []
    @start.each do |range|
      break if _range_exceeded?(range, last)
      list << range unless _target_exceeded?(range, first)
    end
    return list

  when Enumerator
    begin
      list = []
      while (range = @start.succ)
        break if _range_exceeded?(range, last)
        list << range unless _target_exceeded?(range, first)
      end
      return list
    ensure
      @start._rewind
    end
 
  else
    return @start <= last ? [@start] : []
  end
end