small 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Bryan Goines
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,5 @@
1
+ Small
2
+ ======
3
+
4
+ Small collection of useful Ruby utilities
5
+
@@ -0,0 +1,10 @@
1
+
2
+ module Small
3
+ VERSION = "0.1.0"
4
+ end
5
+
6
+ require 'small/object'
7
+ require 'small/string'
8
+ require 'small/math'
9
+ require 'small/array'
10
+ require 'small/hash'
@@ -0,0 +1,10 @@
1
+ class Array
2
+ def extract_options!
3
+ last.is_a?(Hash) ? pop : {}
4
+ end
5
+
6
+ def sample
7
+ self[Kernel.rand(size)]
8
+ end
9
+
10
+ end
@@ -0,0 +1,17 @@
1
+ class Hash
2
+
3
+ def stringify_keys!
4
+ keys.each {|key| self[key.to_s] = delete(key) }
5
+ self
6
+ end
7
+
8
+ def symbolize_keys!
9
+ keys.each {|key| self[key.to_sym] = delete(key) }
10
+ self
11
+ end
12
+
13
+ def to_param
14
+ collect {|key, value| "#{key}=#{value}" }.sort * '&'
15
+ end
16
+
17
+ end
@@ -0,0 +1,15 @@
1
+ module Math
2
+ class << self
3
+
4
+ def fact(n)
5
+ (1..n).inject(1) {|r,i| r*i}
6
+ end
7
+ alias :factorial :fact
8
+
9
+ def fib(n)
10
+ n < 2 ? n : fib(n-1)+fib(n-2)
11
+ end
12
+ alias :fibanocci :fib
13
+
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ class Object
2
+
3
+ def blank?
4
+ respond_to?(:empty?) ? empty? : !self
5
+ end
6
+
7
+ def try(*a, &b)
8
+ if a.empty? && block_given?
9
+ yield self
10
+ else
11
+ __send__(*a, &b)
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,62 @@
1
+ class String
2
+
3
+ def camelize
4
+ gsub(/\s+/, '_').gsub(/(?:^|_)(.)/) { $1.upcase }
5
+ end
6
+
7
+ def dashize
8
+ gsub(/_/, '-')
9
+ end
10
+
11
+ def humanize
12
+ gsub(/_/, ' ').gsub(/^(\w)/) { $1.upcase }
13
+ end
14
+
15
+ def underscore
16
+ gsub(/(-|\s+)/,'_').downcase
17
+ end
18
+
19
+ def constantize
20
+ constant = Object
21
+ constant.const_defined?(self.to_s) ? constant.const_get(self.to_s) : constant.const_missing(self.to_s)
22
+ end
23
+
24
+ def encode64
25
+ [self.to_s].pack("m").gsub(/\n/,'')
26
+ end
27
+
28
+ def decode64
29
+ unpack("m").first
30
+ end
31
+
32
+ def to_sha1
33
+ require "digest/sha1" unless defined?(Digest::SHA1)
34
+ Digest::SHA1.hexdigest(self.to_s)
35
+ end
36
+
37
+ def to_sha2(provider = 256)
38
+ require 'digest/sha2' unless defined?(Digest::SHA256) || defined?(Digest::SHA384) || defined?(Digest::SHA512)
39
+ object = case provider
40
+ when 256 then Digest::SHA256.new
41
+ when 384 then Digest::SHA384.new
42
+ when 512 then Digest::SHA512.new
43
+ else raise Exception, "Unknown digest provider: #{provider} -- try 256, 384, and 512"
44
+ end
45
+ object.hexdigest(self.to_s)
46
+ end
47
+
48
+ def to_md5
49
+ require 'digest/md5' unless defined?(Digest::MD5)
50
+ Digest::MD5.hexdigest(self.to_s)
51
+ end
52
+
53
+ def escape
54
+ require 'cgi' unless defined?(CGI)
55
+ CGI.escape(self.to_s)
56
+ end
57
+
58
+ def unescape
59
+ require 'cgi' unless defined?(CGI)
60
+ CGI.unescape(self.to_s)
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: small
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bryan Goines
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: bryann83@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - LICENSE
22
+ - lib/small/array.rb
23
+ - lib/small/hash.rb
24
+ - lib/small/math.rb
25
+ - lib/small/object.rb
26
+ - lib/small/string.rb
27
+ - lib/small.rb
28
+ homepage: https://github.com/bry4n/small
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.15
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: A small collection of useful Ruby utilities
52
+ test_files: []