guerrilla_patch 2.3.1 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +26 -0
- data/VERSION +1 -1
- data/guerrilla_patch.gemspec +5 -3
- data/lib/guerrilla_patch/allocate.rb +31 -0
- data/lib/guerrilla_patch/kernel.rb +20 -0
- data/spec/guerrilla_patch/allocate_spec.rb +67 -0
- data/spec/guerrilla_patch/kernel_spec.rb +10 -0
- metadata +14 -12
data/README.md
CHANGED
@@ -54,6 +54,32 @@ end
|
|
54
54
|
|
55
55
|
Somehow for my convoluted brain the later reads better.
|
56
56
|
|
57
|
+
BigDecimal
|
58
|
+
-----------
|
59
|
+
Not using BigDecimal is asking for trouble, but using it is way too verbose:
|
60
|
+
|
61
|
+
```
|
62
|
+
amount = BigDecimal.new("100.10210")/BigDecimal.new(200.12)
|
63
|
+
```
|
64
|
+
|
65
|
+
It is more succint to put it like this:
|
66
|
+
|
67
|
+
```
|
68
|
+
amount = 100.10210.to_d/200.12/to_d
|
69
|
+
|
70
|
+
Allocate
|
71
|
+
---------
|
72
|
+
I belive allocate is missing from standard library.
|
73
|
+
|
74
|
+
```
|
75
|
+
90.allocate_evenly(3) #=> [30, 30, 30]
|
76
|
+
100.allocate_evenly(3) #=> [33.33, 33.33, 33.34]
|
77
|
+
|
78
|
+
100.allocate([1.to_d/2, 1/to_d/2]) #=> [50, 50]
|
79
|
+
100.allocate([30, 30, 30]) #=> [33.33, 33.33, 33.34]
|
80
|
+
```
|
81
|
+
|
82
|
+
|
57
83
|
Contributing to guerrilla_patch
|
58
84
|
-------------------------------
|
59
85
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.4.0
|
data/guerrilla_patch.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "guerrilla_patch"
|
8
|
-
s.version = "2.
|
8
|
+
s.version = "2.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["drKreso"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-04-18"
|
13
13
|
s.description = "I am tierd of hunting down monkey patches at large. Caging them inside this gem"
|
14
14
|
s.email = "kresimir.bojcic@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -27,15 +27,17 @@ Gem::Specification.new do |s|
|
|
27
27
|
"VERSION",
|
28
28
|
"guerrilla_patch.gemspec",
|
29
29
|
"lib/guerrilla_patch.rb",
|
30
|
+
"lib/guerrilla_patch/allocate.rb",
|
30
31
|
"lib/guerrilla_patch/kernel.rb",
|
31
32
|
"lib/guerrilla_patch/string.rb",
|
33
|
+
"spec/guerrilla_patch/allocate_spec.rb",
|
32
34
|
"spec/guerrilla_patch/kernel_spec.rb",
|
33
35
|
"spec/guerrilla_patch/string_spec.rb"
|
34
36
|
]
|
35
37
|
s.homepage = "http://github.com/drkreso/guerrilla_patch"
|
36
38
|
s.licenses = ["MIT"]
|
37
39
|
s.require_paths = ["lib"]
|
38
|
-
s.rubygems_version = "1.8.
|
40
|
+
s.rubygems_version = "1.8.10"
|
39
41
|
s.summary = "Collection of monkey patches"
|
40
42
|
|
41
43
|
if s.respond_to? :specification_version then
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Allocate
|
2
|
+
attr_accessor :amount
|
3
|
+
attr_accessor :ratios
|
4
|
+
|
5
|
+
def initialize(amount = 0 , ratios = [])
|
6
|
+
@amount = amount
|
7
|
+
@ratios = ratios
|
8
|
+
end
|
9
|
+
|
10
|
+
def divided
|
11
|
+
divided_ratios = @ratios.map { |ratio| ratio.to_d/ratios.sum.to_d }
|
12
|
+
compensate_last_slice( divided_ratios.map { |ratio| (@amount * ratio).round(2) } )
|
13
|
+
end
|
14
|
+
|
15
|
+
def compensate_last_slice(rates)
|
16
|
+
rates.tap do |rates|
|
17
|
+
rates[-1] = rates[-1] + (amount - rates.sum).round(2)
|
18
|
+
if ( amount > 0 && ( rates.select {|item| item <= 0 }.count > 0 ) ) ||
|
19
|
+
( amount < 0 && ( rates.select {|item| item >= 0 }.count > 0 ) )
|
20
|
+
raise "Number is too small to be allocated on that number of slices(#@amount on #{@ratios.size} slices)."
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.evenly(amount, number_of_slices)
|
26
|
+
Allocate.new.tap do |a|
|
27
|
+
a.amount = amount
|
28
|
+
a.ratios = (1..number_of_slices).map { 1.to_d/number_of_slices }
|
29
|
+
end.divided
|
30
|
+
end
|
31
|
+
end #Allocate
|
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
require 'guerrilla_patch/allocate'
|
3
|
+
|
1
4
|
module Kernel
|
2
5
|
def let(name, &block)
|
3
6
|
define_method(name, &block)
|
@@ -48,3 +51,20 @@ end
|
|
48
51
|
self.map(&name).reduce(0, :+)
|
49
52
|
end
|
50
53
|
end
|
54
|
+
|
55
|
+
module Kernel
|
56
|
+
def to_d
|
57
|
+
BigDecimal.new(self.to_s)
|
58
|
+
end
|
59
|
+
|
60
|
+
def allocate_evenly(number_of_slices)
|
61
|
+
Allocate.evenly(self.to_d, number_of_slices)
|
62
|
+
end
|
63
|
+
|
64
|
+
def allocate(ratios)
|
65
|
+
Allocate.new(self.to_d, ratios).divided
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'guerrilla_patch/allocate'
|
2
|
+
|
3
|
+
describe Allocate do
|
4
|
+
it 'should divide whole numbers' do
|
5
|
+
subject.amount = 100
|
6
|
+
subject.ratios = [0.1,0.9]
|
7
|
+
subject.divided.should == [10, 90]
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should divide equally on two decimals' do
|
11
|
+
subject.amount = 100
|
12
|
+
subject.ratios = [1.0/3, 1.0/3,1.0/3]
|
13
|
+
subject.divided.should == [33.33, 33.33, 33.34]
|
14
|
+
subject.divided.inject(0) { | sum, item | sum += item }.should == subject.amount
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should calculate ratios if they are over 1' do
|
18
|
+
subject.amount = 100
|
19
|
+
subject.ratios = [50,70,90]
|
20
|
+
subject.divided.should == [23.81, 33.33, 42.86 ]
|
21
|
+
subject.divided.inject(0) { | sum, item | sum += item }.should == subject.amount
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should calculade real example' do
|
25
|
+
subject.amount = 2297.19
|
26
|
+
subject.ratios = [2170.29, 126.90 ]
|
27
|
+
subject.divided.should == [2170.29, 126.90 ]
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should divide equally on number of slices' do
|
31
|
+
Allocate.evenly(100,3).should == [33.33,33.33,33.34]
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should divide equally on number of slices for small number' do
|
35
|
+
Allocate.evenly(0.1,3).should == [0.03,0.03,0.04]
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should divide equally on number of slices for really small number' do
|
39
|
+
Allocate.evenly(0.1,9).should == [0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.02]
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should divide equally on two decimals negative numbers' do
|
43
|
+
subject.amount = -100
|
44
|
+
subject.ratios = [1.0/3, 1.0/3,1.0/3]
|
45
|
+
subject.divided.should == [-33.33, -33.33, -33.34]
|
46
|
+
subject.divided.inject(0) { | sum, item | sum += item }.should == subject.amount
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should raise exception for number that is too small' do
|
50
|
+
lambda do
|
51
|
+
Allocate.evenly(0.1,20)
|
52
|
+
end.should raise_error 'Number is too small to be allocated on that number of slices(0.1 on 20 slices).'
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should raise exception for number that is too small negative' do
|
56
|
+
lambda do
|
57
|
+
Allocate.evenly(-0.1,20)
|
58
|
+
end.should raise_error 'Number is too small to be allocated on that number of slices(-0.1 on 20 slices).'
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should support costructor based params' do
|
62
|
+
subject = Allocate.new( 100, [1.0/3, 1.0/3,1.0/3] )
|
63
|
+
subject.divided.should == [33.33, 33.33, 33.34]
|
64
|
+
subject.divided.inject(0) { | sum, item | sum += item }.should == subject.amount
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
@@ -63,6 +63,16 @@ describe Kernel do
|
|
63
63
|
|
64
64
|
end
|
65
65
|
|
66
|
+
it 'can allocate a number evenly' do
|
67
|
+
100.allocate_evenly(2).should == [50, 50]
|
68
|
+
100.allocate_evenly(3).should == [33.33, 33.33, 33.34]
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'can allocate a number proportionaly' do
|
72
|
+
100.allocate([1.to_d/2, 1.to_d/2]).should == [50, 50]
|
73
|
+
100.allocate([30,30,30]).should == [33.33, 33.33, 33.34]
|
74
|
+
end
|
75
|
+
|
66
76
|
end
|
67
77
|
|
68
78
|
describe Array do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: guerrilla_patch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.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: 2012-
|
12
|
+
date: 2012-04-18 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70158123986620 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.8.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70158123986620
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &70158123985460 !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: *
|
35
|
+
version_requirements: *70158123985460
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: jeweler
|
38
|
-
requirement: &
|
38
|
+
requirement: &70158123984600 !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: *
|
46
|
+
version_requirements: *70158123984600
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rcov
|
49
|
-
requirement: &
|
49
|
+
requirement: &70158123984020 !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: *
|
57
|
+
version_requirements: *70158123984020
|
58
58
|
description: I am tierd of hunting down monkey patches at large. Caging them inside
|
59
59
|
this gem
|
60
60
|
email: kresimir.bojcic@gmail.com
|
@@ -74,8 +74,10 @@ files:
|
|
74
74
|
- VERSION
|
75
75
|
- guerrilla_patch.gemspec
|
76
76
|
- lib/guerrilla_patch.rb
|
77
|
+
- lib/guerrilla_patch/allocate.rb
|
77
78
|
- lib/guerrilla_patch/kernel.rb
|
78
79
|
- lib/guerrilla_patch/string.rb
|
80
|
+
- spec/guerrilla_patch/allocate_spec.rb
|
79
81
|
- spec/guerrilla_patch/kernel_spec.rb
|
80
82
|
- spec/guerrilla_patch/string_spec.rb
|
81
83
|
homepage: http://github.com/drkreso/guerrilla_patch
|
@@ -93,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
95
|
version: '0'
|
94
96
|
segments:
|
95
97
|
- 0
|
96
|
-
hash: -
|
98
|
+
hash: -2033904039094508931
|
97
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
100
|
none: false
|
99
101
|
requirements:
|
@@ -102,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
104
|
version: '0'
|
103
105
|
requirements: []
|
104
106
|
rubyforge_project:
|
105
|
-
rubygems_version: 1.8.
|
107
|
+
rubygems_version: 1.8.10
|
106
108
|
signing_key:
|
107
109
|
specification_version: 3
|
108
110
|
summary: Collection of monkey patches
|