footing 0.0.3 → 0.0.4
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.
- data/Gemfile +4 -0
- data/Gemfile.lock +10 -0
- data/README.md +13 -5
- data/lib/extensions/array.rb +5 -0
- data/lib/extensions/hash.rb +16 -23
- data/lib/extensions/kernel.rb +10 -18
- data/lib/extensions/numeric.rb +27 -35
- data/lib/extensions/object.rb +11 -18
- data/lib/extensions/string.rb +18 -20
- data/lib/footing.rb +15 -8
- metadata +3 -2
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -2,11 +2,20 @@ GEM
|
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
4
|
coderay (1.0.7)
|
5
|
+
diff-lcs (1.1.3)
|
5
6
|
method_source (0.8)
|
6
7
|
pry (0.9.10)
|
7
8
|
coderay (~> 1.0.5)
|
8
9
|
method_source (~> 0.8)
|
9
10
|
slop (~> 3.3.1)
|
11
|
+
rspec (2.11.0)
|
12
|
+
rspec-core (~> 2.11.0)
|
13
|
+
rspec-expectations (~> 2.11.0)
|
14
|
+
rspec-mocks (~> 2.11.0)
|
15
|
+
rspec-core (2.11.1)
|
16
|
+
rspec-expectations (2.11.2)
|
17
|
+
diff-lcs (~> 1.1.3)
|
18
|
+
rspec-mocks (2.11.1)
|
10
19
|
slop (3.3.2)
|
11
20
|
yard (0.8.2.1)
|
12
21
|
|
@@ -15,4 +24,5 @@ PLATFORMS
|
|
15
24
|
|
16
25
|
DEPENDENCIES
|
17
26
|
pry
|
27
|
+
rspec
|
18
28
|
yard
|
data/README.md
CHANGED
@@ -70,8 +70,16 @@ Footing::String.escape "foo", "o" # => "f\\o\\o"
|
|
70
70
|
|
71
71
|
## Kick the tires
|
72
72
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
73
|
+
1. `git clone git://github.com/hopsoft/footing.git`
|
74
|
+
1. `cd /path/to/footing`
|
75
|
+
1. `bundle`
|
76
|
+
1. `./console`
|
77
|
+
1. `Footing.patch! String, Footing::String`
|
78
|
+
|
79
|
+
or
|
80
|
+
|
81
|
+
1. `gem install footing`
|
82
|
+
1. `irb`
|
83
|
+
1. `require 'rubygems'`
|
84
|
+
1. `require 'footing'`
|
85
|
+
1. `Footing.patch! String, Footing::String`
|
data/lib/extensions/hash.rb
CHANGED
@@ -1,30 +1,23 @@
|
|
1
1
|
module Footing
|
2
2
|
module Hash
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
# @param [Symbol] name The method name to invoke on the existing keys.
|
20
|
-
# @return [Hash] A new Hash that has been re-keyed.
|
21
|
-
def rekey(method_name)
|
22
|
-
inject({}) do |new_hash, (key, value)|
|
23
|
-
new_hash[key.send(method_name)] = value
|
24
|
-
new_hash
|
25
|
-
end
|
4
|
+
# Rekeys the Hash by invoking a method on the existing keys
|
5
|
+
# and uses the return value as the new key.
|
6
|
+
#
|
7
|
+
# NOTE: Creates and return a new Hash.
|
8
|
+
#
|
9
|
+
# Example:
|
10
|
+
# h = { [1] => "short", [1,2] => "medium", [1,2,3] => "long" }
|
11
|
+
# h.rekey(:length) # => { 1 => "short", 2 => "medium", 3 => "long" }
|
12
|
+
#
|
13
|
+
# @param [Symbol] name The method name to invoke on the existing keys.
|
14
|
+
# @return [Hash] A new Hash that has been re-keyed.
|
15
|
+
def rekey(method_name)
|
16
|
+
inject({}) do |new_hash, (key, value)|
|
17
|
+
new_hash[key.send(method_name)] = value
|
18
|
+
new_hash
|
26
19
|
end
|
27
|
-
|
28
20
|
end
|
21
|
+
|
29
22
|
end
|
30
23
|
end
|
data/lib/extensions/kernel.rb
CHANGED
@@ -1,25 +1,17 @@
|
|
1
1
|
module Footing
|
2
2
|
module Kernel
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
# @param [Integer] level The safe level to apply.
|
14
|
-
# @return [Object]
|
15
|
-
def safe_eval(text, level=4)
|
16
|
-
sandbox = lambda do
|
17
|
-
$SAFE = level
|
18
|
-
eval(text.to_s)
|
19
|
-
end
|
20
|
-
sandbox.call
|
4
|
+
# Safely evals text inside of a sandbox.
|
5
|
+
# @see http://phrogz.net/programmingruby/taint.html Ruby safe level description.
|
6
|
+
# @param [String] text The text to eval.
|
7
|
+
# @param [Integer] level The safe level to apply.
|
8
|
+
# @return [Object]
|
9
|
+
def safe_eval(text, level=4)
|
10
|
+
sandbox = lambda do
|
11
|
+
$SAFE = level
|
12
|
+
eval(text.to_s)
|
21
13
|
end
|
22
|
-
|
14
|
+
sandbox.call
|
23
15
|
end
|
24
16
|
|
25
17
|
end
|
data/lib/extensions/numeric.rb
CHANGED
@@ -1,46 +1,38 @@
|
|
1
1
|
module Footing
|
2
2
|
module Numeric
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
# Returns a positive representation of the number.
|
5
|
+
def positive
|
6
|
+
return self if self >= 0
|
7
|
+
flip_sign
|
6
8
|
end
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
flip_sign
|
14
|
-
end
|
15
|
-
|
16
|
-
# Returns a negative representation of the number.
|
17
|
-
def negative
|
18
|
-
return self if self < 0
|
19
|
-
flip_sign
|
20
|
-
end
|
21
|
-
|
22
|
-
# Flips the sign on the number making it either either positive or negative.
|
23
|
-
def flip_sign
|
24
|
-
self * -1
|
25
|
-
end
|
10
|
+
# Returns a negative representation of the number.
|
11
|
+
def negative
|
12
|
+
return self if self < 0
|
13
|
+
flip_sign
|
14
|
+
end
|
26
15
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
def percent_of(number)
|
32
|
-
percent = (self.to_f / number.to_f) * 100 if number > 0
|
33
|
-
percent ||= 0.0
|
34
|
-
end
|
16
|
+
# Flips the sign on the number making it either either positive or negative.
|
17
|
+
def flip_sign
|
18
|
+
self * -1
|
19
|
+
end
|
35
20
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
21
|
+
# Returns the percentage that this number is of the passed number.
|
22
|
+
# @example
|
23
|
+
# 8.percent_of(10) # => 80.0
|
24
|
+
# @param [Numeric] number The number to calculate the percentage with
|
25
|
+
def percent_of(number)
|
26
|
+
percent = (self.to_f / number.to_f) * 100 if number > 0
|
27
|
+
percent ||= 0.0
|
28
|
+
end
|
43
29
|
|
30
|
+
# Rounds the number to a certain number of decimal places.
|
31
|
+
# @example
|
32
|
+
# 1.784329.round_to(1) # => 1.8
|
33
|
+
# @param [Numeric] decimal_places The number of decimal places to round to
|
34
|
+
def round_to(decimal_places)
|
35
|
+
(self * 10**decimal_places).round.to_f / 10**decimal_places
|
44
36
|
end
|
45
37
|
|
46
38
|
end
|
data/lib/extensions/object.rb
CHANGED
@@ -1,26 +1,19 @@
|
|
1
1
|
module Footing
|
2
2
|
module Object
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
module InstanceMethods
|
9
|
-
|
10
|
-
# Returns the eigen class for the object.
|
11
|
-
def eigen
|
12
|
-
eigen = class << self
|
13
|
-
self
|
14
|
-
end
|
15
|
-
rescue Exception => ex
|
16
|
-
nil
|
17
|
-
end
|
18
|
-
|
19
|
-
# Indicates if the object has an eigen class.
|
20
|
-
def has_eigen?
|
21
|
-
!eigen.nil?
|
4
|
+
# Returns the eigen class for the object.
|
5
|
+
def eigen
|
6
|
+
eigen = class << self
|
7
|
+
self
|
22
8
|
end
|
9
|
+
rescue Exception => ex
|
10
|
+
nil
|
11
|
+
end
|
23
12
|
|
13
|
+
# Indicates if the object has an eigen class.
|
14
|
+
def has_eigen?
|
15
|
+
!eigen.nil?
|
24
16
|
end
|
17
|
+
|
25
18
|
end
|
26
19
|
end
|
data/lib/extensions/string.rb
CHANGED
@@ -1,33 +1,31 @@
|
|
1
1
|
module Footing
|
2
2
|
module String
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
## Generates a random string (upcase alpha-numeric)
|
5
|
+
## Returns a string with the length provided, defaulting to 12 chars
|
6
|
+
def random_key(length=12)
|
7
|
+
@chars ||= [(0..9).to_a, ('A'..'Z').to_a].flatten
|
8
|
+
(1..length).map{ |i| @chars.sample }.join
|
7
9
|
end
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
15
|
-
(0...length).map { chars.split('')[rand(chars.length)] }.join
|
11
|
+
# Escapes a series of chars in the current string.
|
12
|
+
# NOTE: A new string is returned.
|
13
|
+
def escape(*chars)
|
14
|
+
gsub(/(?<!\\)(#{chars.join("|")})/) do |char|
|
15
|
+
"\\" + char
|
16
16
|
end
|
17
|
-
|
18
17
|
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
def escape(*chars)
|
25
|
-
gsub(/(?<!\\)(#{chars.join("|")})/) do |char|
|
26
|
-
"\\" + char
|
27
|
-
end
|
28
|
-
end
|
19
|
+
# Converts a word with underscores into a sentance with a capitalized first word.
|
20
|
+
def humanize
|
21
|
+
self.downcase.gsub(/_/, " ").capitalize
|
22
|
+
end
|
29
23
|
|
24
|
+
# Similar to humanize but it capitalizes each word
|
25
|
+
def titleize
|
26
|
+
self.split('_').map(&:capitalize).join(' ')
|
30
27
|
end
|
28
|
+
alias :titlecase :titleize
|
31
29
|
|
32
30
|
end
|
33
31
|
end
|
data/lib/footing.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
module Footing
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
list = [
|
3
|
+
def self.modules
|
4
|
+
[
|
6
5
|
"Kernel",
|
7
6
|
"Object",
|
8
7
|
"String",
|
9
8
|
"Numeric",
|
10
9
|
"Hash"
|
11
10
|
]
|
11
|
+
end
|
12
12
|
|
13
|
-
|
13
|
+
# Applies all Footing patches.
|
14
|
+
def self.patch_all!
|
15
|
+
modules.each do |name|
|
14
16
|
context = Object.const_get(name)
|
15
17
|
footing = Footing.const_get(name)
|
16
18
|
patch! context, footing
|
@@ -35,7 +37,12 @@ module Footing
|
|
35
37
|
context.send :include, extension
|
36
38
|
end
|
37
39
|
|
38
|
-
# Creates
|
40
|
+
# Creates util methods for all Footing patches.
|
41
|
+
def self.util_all!
|
42
|
+
modules.each { |mod| util! Footing.const_get(mod) }
|
43
|
+
end
|
44
|
+
|
45
|
+
# Creates class methods for all instance methods in the module.
|
39
46
|
# This allows users to invoke utility methods rather than monkey patching if they so desire.
|
40
47
|
# @param [Module] mod The Module to setup util methods for.
|
41
48
|
def self.util!(mod)
|
@@ -50,9 +57,9 @@ module Footing
|
|
50
57
|
self
|
51
58
|
end
|
52
59
|
|
53
|
-
mod.
|
54
|
-
eigen.send :define_method, method do
|
55
|
-
proxy_eigen.instance_method(method).bind(
|
60
|
+
mod.instance_methods(false).each do |method|
|
61
|
+
eigen.send :define_method, method do |*args|
|
62
|
+
proxy_eigen.instance_method(method).bind(args.first || proxy).call(*args)
|
56
63
|
end
|
57
64
|
end
|
58
65
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: footing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-26 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! " Footing provides some sanity for monkey patching practices.\n
|
15
15
|
\ It's also a utility lib that contains additional functionality for core objects
|
@@ -20,6 +20,7 @@ executables: []
|
|
20
20
|
extensions: []
|
21
21
|
extra_rdoc_files: []
|
22
22
|
files:
|
23
|
+
- lib/extensions/array.rb
|
23
24
|
- lib/extensions/hash.rb
|
24
25
|
- lib/extensions/kernel.rb
|
25
26
|
- lib/extensions/numeric.rb
|