slab 1.0.0

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.
@@ -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.
@@ -0,0 +1,4 @@
1
+ slab
2
+ ====
3
+
4
+ Core extensions to build Rails apps upon
@@ -0,0 +1,3 @@
1
+ module Slab
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ module Slab
2
+ Dir[File.join(File.dirname(__FILE__), '*.rb')].each { |file| require file }
3
+ end
@@ -0,0 +1,7 @@
1
+ class BigDecimal
2
+
3
+ def inspect
4
+ "#<BigDecimal:#{object_id.to_s(0x10)}@#{precs.first},#{precs.last} #{to_s}>"
5
+ end
6
+
7
+ end
@@ -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
@@ -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,10 @@
1
+ class Object
2
+ # The hidden singleton lurks behind everyone
3
+ def metaclass
4
+ class << self; self; end
5
+ end
6
+
7
+ def meta_eval(&block)
8
+ metaclass.instance_eval &block
9
+ end
10
+ end
@@ -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
@@ -0,0 +1,7 @@
1
+ # http://extensions.rubyforge.org/rdoc/classes/Module.html
2
+
3
+ class Module
4
+ def basename
5
+ self.name.sub(/^.*::/, '')
6
+ end
7
+ end
@@ -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
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def returning(value, &block)
3
+ value.tap(&block)
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ require 'securerandom'
2
+
3
+ class String
4
+ def to_iso
5
+ encode('iso-8859-1', :invalid => :replace, :undef => :replace)
6
+ end
7
+
8
+ def self.random(length = 12)
9
+ SecureRandom.base64(length)[0..(length - 1)]
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Slab
2
+ VERSION = '1.0.0'
3
+ end
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: []