ranges_merger 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,8 @@
2
2
 
3
3
  Very simple gem for merging range-like objects.
4
4
 
5
+
6
+
5
7
  == How to use, simple merge
6
8
 
7
9
  If you want to merge please use code just like below.
@@ -16,6 +18,8 @@ If you want to merge please use code just like below.
16
18
 
17
19
  # result => [[1, 6]]
18
20
 
21
+
22
+
19
23
  == Range objects
20
24
 
21
25
  If you want to merge Range objects.
@@ -34,6 +38,8 @@ If you want to merge Range objects.
34
38
 
35
39
  # result => [(1..3),(5..8)]
36
40
 
41
+
42
+
37
43
  == Exclusion
38
44
 
39
45
  If you want to exclude please use code just like below.
@@ -58,6 +64,8 @@ And run some processing
58
64
 
59
65
  # result => [[1, 2], [10, 11]]
60
66
 
67
+
68
+
61
69
  == I want an instance
62
70
 
63
71
  If you want to have instance, add and remove ranges as you wish just do something like that:
@@ -77,6 +85,54 @@ If you want to have instance, add and remove ranges as you wish just do somethin
77
85
  # r.to_array => [[1, 2], [4,11], [12,15]]
78
86
 
79
87
 
88
+
89
+ == Dividing
90
+
91
+ Then you have range you may divide it to small pieced with proper interval. You can divide it with 'partials' - smaller
92
+ parts than interval, or without them.
93
+
94
+ r = RangesMerger.new
95
+
96
+ r += [[1, 9]]
97
+
98
+ result = r % 3
99
+
100
+ # result => [[1, 4], [4, 7]]
101
+
102
+ result = r / 3
103
+
104
+ # result => [[1, 4], [4, 7], [7, 9]]
105
+
106
+
107
+
108
+ == Ranges are not only numbers
109
+
110
+ They can be made from all objects which can be compared, and has simple math. operation like sum. For example - Time
111
+ object.
112
+
113
+ t_now = Time.now
114
+
115
+ r = RangesMerger.new
116
+
117
+ r += [[t_now - 2*3600, t_now - 0*3600]]
118
+ r += [[t_now - 3*3600, t_now - 1.3600]]
119
+
120
+ result = r.to_array
121
+
122
+ # try by yourself or read spec :]
123
+
124
+ r -= [[t_now - 2*3600, t_now - 1*3600]]
125
+
126
+ result = r.to_array
127
+
128
+ # try by yourself or read spec :]
129
+
130
+ result = r / (10*60)
131
+
132
+ # try by yourself or read spec :]
133
+
134
+
135
+
80
136
  == Contributing to ranges_merger
81
137
 
82
138
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -1,11 +1,13 @@
1
1
  require 'ranges_merger/ranges_merger_merging'
2
2
  require 'ranges_merger/ranges_merger_utils'
3
3
  require 'ranges_merger/ranges_merger_exclusion'
4
+ require 'ranges_merger/ranges_merger_divider'
4
5
 
5
6
  class RangesMerger
6
7
  extend RangesMergerMerging
7
8
  extend RangesMergerUtils
8
9
  extend RangesMergerExclusion
10
+ extend RangesMergerDivider
9
11
 
10
12
  attr_reader :ranges
11
13
 
@@ -33,6 +35,18 @@ class RangesMerger
33
35
  remove(_array)
34
36
  end
35
37
 
38
+ def divide(interval, allow_partials = true)
39
+ return self.class.divide(@ranges, interval, allow_partials)
40
+ end
41
+
42
+ def /(interval)
43
+ divide(interval, true)
44
+ end
45
+
46
+ def %(interval)
47
+ divide(interval, false)
48
+ end
49
+
36
50
  def to_ranges
37
51
  self.class.array_to_ranges(@ranges)
38
52
  end
@@ -0,0 +1,31 @@
1
+ module RangesMergerDivider
2
+ # Divide not merged array
3
+ def divide(_array, interval, allow_partials = true)
4
+ divided_array = Array.new
5
+ joined_array = merge(_array)
6
+
7
+ joined_array.each do |r|
8
+ r_from = r[0]
9
+ r_to = r[1]
10
+ r_now = r_from
11
+
12
+ while r_now <= r_to
13
+ new_r_from = r_now
14
+ new_r_to = new_r_from + interval
15
+
16
+ if allow_partials and new_r_to > r_to and new_r_from < r_to
17
+ new_r_to = r_to
18
+ end
19
+
20
+ divided_array << [new_r_from, new_r_to] if new_r_to <= r_to
21
+ r_now += interval
22
+ end
23
+
24
+
25
+
26
+ end
27
+
28
+ return divided_array
29
+
30
+ end
31
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ranges_merger"
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Aleksander Kwiatkowski"]
12
- s.date = "2011-12-06"
12
+ s.date = "2011-12-08"
13
13
  s.description = "Simple gem used for merging range-like objects. If you have problems with operations on ranges this gem can save you some time."
14
14
  s.email = "bobikx@poczta.fm"
15
15
  s.extra_rdoc_files = [
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "Rakefile",
28
28
  "VERSION",
29
29
  "lib/ranges_merger.rb",
30
+ "lib/ranges_merger/ranges_merger_divider.rb",
30
31
  "lib/ranges_merger/ranges_merger_exclusion.rb",
31
32
  "lib/ranges_merger/ranges_merger_merging.rb",
32
33
  "lib/ranges_merger/ranges_merger_utils.rb",
@@ -278,4 +278,73 @@ describe "RangesMerger" do
278
278
  r.to_array.should == [[-2, 3], [5, 20], [30, 50]]
279
279
  end
280
280
 
281
+ # Dividing
282
+
283
+ it "should divide simple range" do
284
+ r = RangesMerger.new
285
+ r += [[10, 50]]
286
+ #result = r.divide(5)
287
+ result = r / 5
288
+ result.should == [[10, 15], [15, 20], [20, 25], [25, 30], [30, 35], [35, 40], [40, 45], [45, 50]]
289
+
290
+ result = r % 5
291
+ result.should == [[10, 15], [15, 20], [20, 25], [25, 30], [30, 35], [35, 40], [40, 45], [45, 50]]
292
+ end
293
+
294
+ it "should divide simple range with partials and without" do
295
+ r = RangesMerger.new
296
+ r += [[1, 9]]
297
+ result = r % 3
298
+ result.should == [[1, 4], [4, 7]]
299
+ result = r / 3
300
+ result.should == [[1, 4], [4, 7], [7, 9]]
301
+ end
302
+
303
+ it "should divide simple range with partials and without" do
304
+ r = RangesMerger.new
305
+ r += [[1, 2], [3, 5], [6, 9]]
306
+ result = r % 2
307
+ result.should == [[3, 5], [6, 8]]
308
+ result = r / 2
309
+ result.should == [[1, 2], [3, 5], [6, 8], [8, 9]]
310
+ end
311
+
312
+ it "should merge Time ranges" do
313
+ t_now = Time.now
314
+
315
+ r = RangesMerger.new
316
+ r += [[t_now - 2*3600, t_now - 0*3600]]
317
+ r += [[t_now - 3*3600, t_now - 1.3600]]
318
+
319
+ result = r.to_array
320
+ (result[0][1] - result[0][0]).should == 3*3600
321
+
322
+ r -= [[t_now - 2*3600, t_now - 1*3600]]
323
+
324
+ result = r.to_array
325
+ result.should == [
326
+ [t_now - 3*3600, t_now - 2*3600],
327
+ [t_now - 1*3600, t_now - 0*3600]
328
+ ]
329
+
330
+ result = r / (10*60)
331
+ result.size.should == 12
332
+ (result.first[1] - result.first[0]).should == 10*60
333
+ end
334
+
335
+ it "should divide simple Time ranges" do
336
+ t_now = Time.now
337
+ r = RangesMerger.new
338
+ r += [[t_now - 1*3600, t_now - 1*3600 + 45*60]]
339
+
340
+ result = r / (10*60)
341
+ result.size.should == 5
342
+ (result.last[1] - result.last[0]).should == 5*60
343
+
344
+ result = r % (10*60)
345
+ result.size.should == 4
346
+ (result.last[1] - result.last[0]).should == 10*60
347
+ end
348
+
349
+
281
350
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ranges_merger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-06 00:00:00.000000000Z
12
+ date: 2011-12-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &83398430 !ruby/object:Gem::Requirement
16
+ requirement: &90032930 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.3.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *83398430
24
+ version_requirements: *90032930
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &83398180 !ruby/object:Gem::Requirement
27
+ requirement: &90032640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *83398180
35
+ version_requirements: *90032640
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &83397940 !ruby/object:Gem::Requirement
38
+ requirement: &90032340 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.6.4
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *83397940
46
+ version_requirements: *90032340
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rcov
49
- requirement: &83333790 !ruby/object:Gem::Requirement
49
+ requirement: &90032040 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *83333790
57
+ version_requirements: *90032040
58
58
  description: Simple gem used for merging range-like objects. If you have problems
59
59
  with operations on ranges this gem can save you some time.
60
60
  email: bobikx@poczta.fm
@@ -74,6 +74,7 @@ files:
74
74
  - Rakefile
75
75
  - VERSION
76
76
  - lib/ranges_merger.rb
77
+ - lib/ranges_merger/ranges_merger_divider.rb
77
78
  - lib/ranges_merger/ranges_merger_exclusion.rb
78
79
  - lib/ranges_merger/ranges_merger_merging.rb
79
80
  - lib/ranges_merger/ranges_merger_utils.rb
@@ -95,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
96
  version: '0'
96
97
  segments:
97
98
  - 0
98
- hash: -671424623
99
+ hash: 29790049
99
100
  required_rubygems_version: !ruby/object:Gem::Requirement
100
101
  none: false
101
102
  requirements: