increments 0.0.3 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/increments.rb +20 -17
  3. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4437c3349327b61ddf3322fa91021884c41a74ec
4
- data.tar.gz: 32cc783207051d6e182bb19e87cb5363f19af790
3
+ metadata.gz: 6753a69fd982992e8d84d6213324fd58fe9f91a6
4
+ data.tar.gz: 9132459069f13c8c68aebcfcac8fcecb30ac077e
5
5
  SHA512:
6
- metadata.gz: b133d45ae339118c93eed809d33b2531ef270683bebae6be11741a5ae972a8877e626777e0e6cf4f62f43d4ef4ee8c7f4afbf8cb2dc5ccc6c34bee6d3e1cc463
7
- data.tar.gz: 9a37e1eab9e100a04c79ee1db4f7925a2bfc9e51fafec09a8728ce5b663181a93ce3dabfec37c22a23e333af43177233f7724bab28ceb57a7fcb65942ae2f610
6
+ metadata.gz: 00fe7fc4f2a22ee7159ce8fe0ca9b195130d6a65b7bfacb11358814695a18d636a36e7fb57ebc8a110c9eca6849818f60ef8dc6e8b647162a1bfdb845f247458
7
+ data.tar.gz: 43dd4a84c97ef6e5932aa9dee2b8a65343b244ab974226f1946a578dd72c113b5812b0b8d0003aae9ba32b0feafcf8f4c70f3b0c28fdce07a6fa6c071f34b0cf
@@ -1,39 +1,42 @@
1
1
  module Increments
2
- def self.increment(opts={})
2
+ def self.increment(opts={},&block)
3
3
  options = defaults.merge(opts)
4
- validate(options)
5
- min, max = options[:min], [options[:min] + options[:increment]-1, options[:max]].min
6
- loop do
7
- yield(min,max)
8
- break if max == options[:max]
9
- min = [min + options[:increment], options[:max]].min
10
- max = [max + options[:increment], options[:max]].min
11
- end
4
+ validate(options,&block)
5
+ step_enum(options).each do |min|
6
+ yield(min,range_max(min,options))
7
+ end
12
8
  end
13
9
 
14
- def self.decrement(opts={})
10
+ def self.decrement(opts={},&block)
15
11
  options = defaults.merge(opts)
16
- validate(options)
12
+ validate(options,&block)
17
13
  min, max = [options[:min], options[:max] - options[:increment]+1].max, options[:max]
18
14
  loop do
19
15
  yield(min,max)
20
16
  break if min == options[:min]
21
17
  min = [min - options[:increment], options[:min]].max
22
- max = [max - options[:increment], options[:min]].max
18
+ max = [max - options[:increment], options[:min]].max
23
19
  end
24
20
  end
25
21
 
26
22
  private
27
23
 
24
+ def self.range_max(min,options)
25
+ [min + options[:increment]-1,options[:max]].min
26
+ end
27
+
28
+ def self.step_enum(options)
29
+ (options[:min]..options[:max]).step(options[:increment])
30
+ end
31
+
28
32
  def self.defaults
29
- {
30
- :min => 0,
31
- :max => 4294967295,
32
- :increment => 100000
33
- }
33
+ { :min => 0,
34
+ :max => 4294967295,
35
+ :increment => 100000 }
34
36
  end
35
37
 
36
38
  def self.validate(options)
37
39
  raise ArgumentError, "Invalid Options: #{options}" if options[:min] > options[:max] || options[:increment] <= 0
40
+ raise LocalJumpError, "no block given (yield)" unless block_given?
38
41
  end
39
42
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: increments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garren Staubli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-13 00:00:00.000000000 Z
11
+ date: 2013-07-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Range bound incrementing and decrementing by a specified amount
14
14
  email: gstaubli@demandbase.com
@@ -18,7 +18,8 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - lib/increments.rb
20
20
  homepage: https://github.com/gstaubli/increments
21
- licenses: []
21
+ licenses:
22
+ - MIT
22
23
  metadata: {}
23
24
  post_install_message:
24
25
  rdoc_options: []