slab 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +4 -0
- data/lib/slab.rb +3 -0
- data/lib/slab/all.rb +3 -0
- data/lib/slab/big_decimal.rb +7 -0
- data/lib/slab/date.rb +31 -0
- data/lib/slab/enumerable.rb +34 -0
- data/lib/slab/hash.rb +19 -0
- data/lib/slab/metaclass.rb +10 -0
- data/lib/slab/methodphitamine.rb +25 -0
- data/lib/slab/module.rb +7 -0
- data/lib/slab/numeric.rb +43 -0
- data/lib/slab/object.rb +5 -0
- data/lib/slab/string.rb +11 -0
- data/lib/slab/version.rb +3 -0
- metadata +76 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 onrooby GmbH
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/lib/slab.rb
ADDED
data/lib/slab/all.rb
ADDED
data/lib/slab/date.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Slab
|
2
|
+
module DateExt
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def epoch(name = nil)
|
10
|
+
case name
|
11
|
+
when :db then '1000-01-01'.to_date
|
12
|
+
else
|
13
|
+
Time.at(0).to_date
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def end_of_month?
|
19
|
+
self.month != self.advance(:days => 1).month
|
20
|
+
end
|
21
|
+
|
22
|
+
def on_end_of_month(&block)
|
23
|
+
if end_of_month?
|
24
|
+
yield(self)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
Date.send :include, Slab::DateExt
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Enumerable
|
2
|
+
|
3
|
+
def any_not?(&block)
|
4
|
+
not all? { |e| yield(e) }
|
5
|
+
end
|
6
|
+
|
7
|
+
def none?(&block)
|
8
|
+
not any? { |e| yield(e) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def hmap(&block)
|
12
|
+
inject({}) { |hash, obj| hash[obj] = yield(obj); hash }
|
13
|
+
end
|
14
|
+
|
15
|
+
def hmap_pair(&block)
|
16
|
+
inject({}) { |hash, obj| k,v = yield(obj); hash[k] = v; hash }
|
17
|
+
end
|
18
|
+
alias :build_hash :hmap_pair
|
19
|
+
|
20
|
+
def map_with_index
|
21
|
+
result = []
|
22
|
+
each_with_index do |elt, idx|
|
23
|
+
result << yield(elt, idx)
|
24
|
+
end
|
25
|
+
result
|
26
|
+
end
|
27
|
+
alias :collect_with_index :map_with_index
|
28
|
+
|
29
|
+
def inject_with_index(initial, &block)
|
30
|
+
index = 0
|
31
|
+
inject(initial) { |memo, obj| result = yield(memo, obj, index); index += 1; result }
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/lib/slab/hash.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Slab
|
2
|
+
module HashExt
|
3
|
+
def hmap(&block)
|
4
|
+
self.inject({}) { |h, i|
|
5
|
+
h[i[0]] = yield(i[0], i[1])
|
6
|
+
h
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
def extract(*keys)
|
11
|
+
keys.inject({}) { |h, key|
|
12
|
+
h[key] = self[key] if self.key?(key)
|
13
|
+
h
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Hash.send :include, Slab::HashExt
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Kernel
|
2
|
+
protected
|
3
|
+
def it() It.new end
|
4
|
+
alias its it
|
5
|
+
end
|
6
|
+
|
7
|
+
class It < BasicObject
|
8
|
+
|
9
|
+
undef_method(*(instance_methods - [:__id__, :__send__]))
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@methods = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(*args, &block)
|
16
|
+
@methods << [args, block] unless args == [:respond_to?, :to_proc]
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_proc
|
21
|
+
::Kernel.lambda do |obj|
|
22
|
+
@methods.inject(obj) { |current, (args,block)| current.send(*args, &block) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/slab/module.rb
ADDED
data/lib/slab/numeric.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
module Slab
|
2
|
+
module NumericExt
|
3
|
+
|
4
|
+
class DiscountTooBig < StandardError; end
|
5
|
+
|
6
|
+
def percent
|
7
|
+
self / 100.0
|
8
|
+
end
|
9
|
+
|
10
|
+
def discount(factor)
|
11
|
+
raise DiscountTooBig if factor > 1
|
12
|
+
self * (1.0 - factor)
|
13
|
+
end
|
14
|
+
|
15
|
+
def net(tax_factor)
|
16
|
+
self / (1.0 + tax_factor)
|
17
|
+
end
|
18
|
+
|
19
|
+
def gross(tax_factor)
|
20
|
+
self * (1.0 + tax_factor)
|
21
|
+
end
|
22
|
+
|
23
|
+
def tax(factor)
|
24
|
+
self * factor.to_f
|
25
|
+
end
|
26
|
+
|
27
|
+
def commercial_round
|
28
|
+
(self * 100.0).round / 100.0
|
29
|
+
end
|
30
|
+
|
31
|
+
def sgn
|
32
|
+
if self > 0
|
33
|
+
1
|
34
|
+
elsif self == 0
|
35
|
+
0
|
36
|
+
else
|
37
|
+
-1
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
Numeric.send :include, Slab::NumericExt
|
data/lib/slab/object.rb
ADDED
data/lib/slab/string.rb
ADDED
data/lib/slab/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slab
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthias Grosser
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.3
|
30
|
+
description: Extensions for Ruby base classes
|
31
|
+
email:
|
32
|
+
- mtgrosser@gmx.net
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- lib/slab/big_decimal.rb
|
38
|
+
- lib/slab/numeric.rb
|
39
|
+
- lib/slab/methodphitamine.rb
|
40
|
+
- lib/slab/hash.rb
|
41
|
+
- lib/slab/version.rb
|
42
|
+
- lib/slab/string.rb
|
43
|
+
- lib/slab/object.rb
|
44
|
+
- lib/slab/metaclass.rb
|
45
|
+
- lib/slab/enumerable.rb
|
46
|
+
- lib/slab/date.rb
|
47
|
+
- lib/slab/module.rb
|
48
|
+
- lib/slab/all.rb
|
49
|
+
- lib/slab.rb
|
50
|
+
- MIT-LICENSE
|
51
|
+
- README.md
|
52
|
+
homepage: http://rubygems.org/gems/slab
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.23
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Core extensions to build Rails upon
|
76
|
+
test_files: []
|