slab 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/slab.rb +2 -0
- data/lib/slab/{object.rb → returning.rb} +2 -2
- data/lib/slab/string.rb +4 -2
- data/lib/slab/version.rb +1 -1
- data/test/cases/slab_test.rb +181 -0
- data/test/test_helper.rb +61 -0
- metadata +18 -19
data/lib/slab.rb
CHANGED
data/lib/slab/string.rb
CHANGED
@@ -5,7 +5,9 @@ class String
|
|
5
5
|
encode('iso-8859-1', :invalid => :replace, :undef => :replace)
|
6
6
|
end
|
7
7
|
|
8
|
-
def self.random(length = 12)
|
9
|
-
|
8
|
+
def self.random(length = 12, set = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
|
9
|
+
set_size, string = set.size, ''
|
10
|
+
length.times { string << set[SecureRandom.random_number(set_size)] }
|
11
|
+
string
|
10
12
|
end
|
11
13
|
end
|
data/lib/slab/version.rb
CHANGED
@@ -0,0 +1,181 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
4
|
+
|
5
|
+
require 'slab/all'
|
6
|
+
|
7
|
+
class SlabTest < ActiveSupport::TestCase
|
8
|
+
|
9
|
+
test 'enumerable extension: hmap' do
|
10
|
+
assert_equal Hash['a' => 'b', 'b' => 'c'], ['a', 'b'].hmap(&:succ)
|
11
|
+
end
|
12
|
+
|
13
|
+
test 'hash extension: hmap' do
|
14
|
+
block_args = []
|
15
|
+
block = ->(k, v) { block_args << [k, v]; v**2 }
|
16
|
+
assert_equal({ a: 1, b: 4, c: 9 }, { a: 1, b: 2, c: 3 }.hmap(&block))
|
17
|
+
assert_equal [[:a, 1], [:b, 2], [:c, 3]], block_args
|
18
|
+
end
|
19
|
+
|
20
|
+
test 'hash extension: hmap_pair' do
|
21
|
+
assert_equal Hash['aaa' => 3, 'bbb' => 6], { :a => 1, :b => 2 }.hmap_pair { |k, v| [k.to_s * 3, v * 3] }
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'enumerable extension: hmap_pair' do
|
25
|
+
assert_equal Hash['a' => 'c', 'b' => 'd'], %w(a b).hmap_pair { |k, v| [k, k.succ.succ] }
|
26
|
+
end
|
27
|
+
|
28
|
+
test 'hash extension: extract' do
|
29
|
+
assert_equal({ a: 1, b: 2 }, { :a => 1, :aa => 1.5, :b => 2, 'c' => 3, :c => '4' }.extract(:a, :b))
|
30
|
+
end
|
31
|
+
|
32
|
+
test 'numeric extension: percent' do
|
33
|
+
assert_float_equal 0.123, 12.3.percent
|
34
|
+
assert_float_equal 2, 200.percent
|
35
|
+
assert_float_equal 999.9, BigDecimal.new('99990.0000').percent
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'numeric extension: discount' do
|
39
|
+
assert_float_equal 0.8, 1.0.discount(20.percent)
|
40
|
+
assert_float_equal 200, BigDecimal.new('400.0').discount(50.percent)
|
41
|
+
assert_raise(Numeric::DiscountTooBig) { 10.discount(101.percent) }
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'numeric extension: net' do
|
45
|
+
assert_float_equal 1.0, 1.19.net(19.percent)
|
46
|
+
assert_float_equal 100, BigDecimal.new('107.0').net(7.percent)
|
47
|
+
end
|
48
|
+
|
49
|
+
test 'numeric extension: gross' do
|
50
|
+
assert_float_equal 1.19, 1.0.gross(19.percent)
|
51
|
+
assert_float_equal 10.7, BigDecimal.new('10').gross(7.percent)
|
52
|
+
end
|
53
|
+
|
54
|
+
test 'numeric extension: tax' do
|
55
|
+
assert_float_equal 0.19, 1.0.tax(19.percent)
|
56
|
+
assert_float_equal 70, BigDecimal.new('1000').tax(7.percent)
|
57
|
+
end
|
58
|
+
|
59
|
+
test 'numeric extension: sgn' do
|
60
|
+
assert_equal 1, 1.sgn
|
61
|
+
assert_equal 0, 0.0000000.sgn
|
62
|
+
assert_equal -1, BigDecimal.new('-345345345.45345345').sgn
|
63
|
+
end
|
64
|
+
|
65
|
+
test 'numeric extension: commercial round' do
|
66
|
+
assert_equal 1.23, 1.234999.commercial_round
|
67
|
+
assert_equal -34.44, -34.44223.commercial_round
|
68
|
+
end
|
69
|
+
|
70
|
+
test 'methodphitamine' do
|
71
|
+
assert_equal [1, 4, 9], (1..3).map(&it ** 2)
|
72
|
+
assert_equal [true, true, false], [-4, 0, 4].map(&its <= 0)
|
73
|
+
assert_equal [false, false, true, false], %w(a aaa aaaa aaaaa).map(&its.size.even?)
|
74
|
+
assert_equal ['F', 'O', 'U'], %w(af bo cu) .map(&its[1].upcase)
|
75
|
+
end
|
76
|
+
|
77
|
+
test 'methodphitamine captures the equality operator' do
|
78
|
+
assert_equal [true, false, true], ['1', :f, 'F'].map(&its.class.name == 'String')
|
79
|
+
end
|
80
|
+
|
81
|
+
test 'date extension: epoch' do
|
82
|
+
assert_equal '1000-01-01'.to_date, Date.epoch(:db)
|
83
|
+
assert_equal '1970-01-01'.to_date, Date.epoch
|
84
|
+
end
|
85
|
+
|
86
|
+
test 'date extension: end of month' do
|
87
|
+
assert_equal true, '2012-02-29'.to_date.end_of_month?
|
88
|
+
assert_equal false, '2012-02-28'.to_date.end_of_month?
|
89
|
+
assert_equal true, '1999-12-31'.to_date.end_of_month?
|
90
|
+
assert_nothing_raised do
|
91
|
+
'2012-02-28'.to_date.on_end_of_month do
|
92
|
+
raise
|
93
|
+
end
|
94
|
+
end
|
95
|
+
dummy_error = Class.new(Exception)
|
96
|
+
assert_raise(dummy_error) do
|
97
|
+
'2012-03-31'.to_date.on_end_of_month do
|
98
|
+
raise dummy_error
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
test 'enumerable extension: any not?' do
|
104
|
+
klass = callable_class
|
105
|
+
array = (1..5).map { |n| klass.new(n) }
|
106
|
+
assert_equal true, array.any_not? { |obj| obj.value < 3 }
|
107
|
+
assert_equal [true, true, true, false, false], array.map(&:touched?)
|
108
|
+
end
|
109
|
+
|
110
|
+
test 'enumerable extension: none?' do
|
111
|
+
klass = callable_class
|
112
|
+
array = (1..5).map { |n| klass.new(n) }
|
113
|
+
assert_equal false, array.none? { |obj| obj.value > 3 }
|
114
|
+
assert_equal [true, true, true, true, false], array.map(&:touched?)
|
115
|
+
end
|
116
|
+
|
117
|
+
test 'enumerable extension: map with index' do
|
118
|
+
assert_equal [0, 1, 2], (3..5).map_with_index { |obj, idx| idx }
|
119
|
+
end
|
120
|
+
|
121
|
+
test 'enumerable extension: inject with index' do
|
122
|
+
assert_equal 'a0b1c2', ['a', 'b', 'c'].inject_with_index('') { |memo, object, index| memo << "#{object}#{index}" }
|
123
|
+
end
|
124
|
+
|
125
|
+
test 'big decimal extension: readable inspect' do
|
126
|
+
number = '1030345345345.34534535'
|
127
|
+
assert BigDecimal.new(number).inspect.match(/#{Regexp.escape(number)}>\z/)
|
128
|
+
end
|
129
|
+
|
130
|
+
test 'module extension: basename' do
|
131
|
+
assert_equal 'Testing', ActiveSupport::Testing.basename
|
132
|
+
assert_equal 'Assertions', ActiveSupport::Testing::Assertions.basename
|
133
|
+
end
|
134
|
+
|
135
|
+
test 'string extension: to_iso' do
|
136
|
+
assert_equal "f\xF6\xF6b\xE4r".force_encoding('iso-8859-1'), 'fööbär'.to_iso
|
137
|
+
assert_equal '50 ?'.force_encoding('iso-8859-1'), "50 €".to_iso
|
138
|
+
end
|
139
|
+
|
140
|
+
test 'string extension: random' do
|
141
|
+
assert_match /\A\w+\z/, String.random(200)
|
142
|
+
assert_equal 12, String.random.size
|
143
|
+
assert_match /\A[f0b4r]+\z/, String.random(1234, 'f0b4r')
|
144
|
+
end
|
145
|
+
|
146
|
+
test 'kernel extension: returning' do
|
147
|
+
s = ''
|
148
|
+
object_id = s.object_id
|
149
|
+
result = returning(s) { |arg| assert_equal object_id, arg.object_id; arg << 'foo'; false }
|
150
|
+
assert_equal 'foo', result
|
151
|
+
assert_equal 'foo', s
|
152
|
+
end
|
153
|
+
|
154
|
+
test 'object extension: metaclass' do
|
155
|
+
mc = class << self; self; end
|
156
|
+
assert_equal mc, self.metaclass
|
157
|
+
obj = Object.new
|
158
|
+
obj.meta_eval do
|
159
|
+
define_method(:foo) { 'foo!' }
|
160
|
+
end
|
161
|
+
assert_equal 'foo!', obj.foo
|
162
|
+
end
|
163
|
+
|
164
|
+
private
|
165
|
+
|
166
|
+
def callable_class
|
167
|
+
Class.new(Object) do
|
168
|
+
define_method 'initialize' do |value|
|
169
|
+
@value = value
|
170
|
+
end
|
171
|
+
define_method 'value' do
|
172
|
+
@touched = true
|
173
|
+
@value
|
174
|
+
end
|
175
|
+
define_method 'touched?' do
|
176
|
+
!!@touched
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
ENV["RAILS_ENV"] = "test"
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
if RUBY_VERSION >= '1.9'
|
6
|
+
require 'simplecov'
|
7
|
+
SimpleCov.start do
|
8
|
+
if artifacts_dir = ENV['CC_BUILD_ARTIFACTS']
|
9
|
+
coverage_dir Pathname.new(artifacts_dir).relative_path_from(Pathname.new(SimpleCov.root)).to_s
|
10
|
+
end
|
11
|
+
add_filter '/test/'
|
12
|
+
add_filter 'vendor'
|
13
|
+
end
|
14
|
+
|
15
|
+
SimpleCov.at_exit do
|
16
|
+
SimpleCov.result.format!
|
17
|
+
if result = SimpleCov.result
|
18
|
+
File.open(File.join(SimpleCov.coverage_path, 'coverage_percent.txt'), 'w') { |f| f << result.covered_percent.to_s }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'rubygems'
|
24
|
+
require 'bundler/setup'
|
25
|
+
Bundler.require(:default)
|
26
|
+
require 'test/unit'
|
27
|
+
|
28
|
+
require 'slab'
|
29
|
+
|
30
|
+
require 'irb'
|
31
|
+
|
32
|
+
module IRB # :nodoc:
|
33
|
+
def self.start_session(binding)
|
34
|
+
unless @__initialized
|
35
|
+
args = ARGV
|
36
|
+
ARGV.replace(ARGV.dup)
|
37
|
+
IRB.setup(nil)
|
38
|
+
ARGV.replace(args)
|
39
|
+
@__initialized = true
|
40
|
+
end
|
41
|
+
|
42
|
+
workspace = WorkSpace.new(binding)
|
43
|
+
|
44
|
+
irb = Irb.new(workspace)
|
45
|
+
|
46
|
+
@CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
|
47
|
+
@CONF[:MAIN_CONTEXT] = irb.context
|
48
|
+
|
49
|
+
catch(:IRB_EXIT) do
|
50
|
+
irb.eval_input
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class ActiveSupport::TestCase
|
56
|
+
|
57
|
+
def assert_float_equal(expected, actual, order = 1)
|
58
|
+
assert_in_delta expected, actual, Float::EPSILON * 10**order
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slab
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
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-07-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirement: &2151918800 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,12 +21,7 @@ dependencies:
|
|
21
21
|
version: 3.2.3
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ~>
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: 3.2.3
|
24
|
+
version_requirements: *2151918800
|
30
25
|
description: Extensions for Ruby base classes
|
31
26
|
email:
|
32
27
|
- mtgrosser@gmx.net
|
@@ -34,21 +29,23 @@ executables: []
|
|
34
29
|
extensions: []
|
35
30
|
extra_rdoc_files: []
|
36
31
|
files:
|
32
|
+
- lib/slab/all.rb
|
37
33
|
- lib/slab/big_decimal.rb
|
38
|
-
- lib/slab/
|
39
|
-
- lib/slab/
|
34
|
+
- lib/slab/date.rb
|
35
|
+
- lib/slab/enumerable.rb
|
40
36
|
- lib/slab/hash.rb
|
41
|
-
- lib/slab/version.rb
|
42
|
-
- lib/slab/string.rb
|
43
|
-
- lib/slab/object.rb
|
44
37
|
- lib/slab/metaclass.rb
|
45
|
-
- lib/slab/
|
46
|
-
- lib/slab/date.rb
|
38
|
+
- lib/slab/methodphitamine.rb
|
47
39
|
- lib/slab/module.rb
|
48
|
-
- lib/slab/
|
40
|
+
- lib/slab/numeric.rb
|
41
|
+
- lib/slab/returning.rb
|
42
|
+
- lib/slab/string.rb
|
43
|
+
- lib/slab/version.rb
|
49
44
|
- lib/slab.rb
|
50
45
|
- MIT-LICENSE
|
51
46
|
- README.md
|
47
|
+
- test/cases/slab_test.rb
|
48
|
+
- test/test_helper.rb
|
52
49
|
homepage: http://rubygems.org/gems/slab
|
53
50
|
licenses: []
|
54
51
|
post_install_message:
|
@@ -69,8 +66,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
66
|
version: '0'
|
70
67
|
requirements: []
|
71
68
|
rubyforge_project:
|
72
|
-
rubygems_version: 1.8.
|
69
|
+
rubygems_version: 1.8.11
|
73
70
|
signing_key:
|
74
71
|
specification_version: 3
|
75
72
|
summary: Core extensions to build Rails upon
|
76
|
-
test_files:
|
73
|
+
test_files:
|
74
|
+
- test/cases/slab_test.rb
|
75
|
+
- test/test_helper.rb
|