footing 0.1.8 → 0.1.9
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +0 -10
- data/lib/footing.rb +38 -61
- data/lib/footing/extensions/hash.rb +11 -4
- data/lib/footing/extensions/nil_class.rb +3 -4
- data/lib/footing/extensions/object.rb +9 -0
- data/lib/footing/extensions/string.rb +1 -2
- data/lib/footing/version.rb +1 -1
- metadata +21 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31c11d069e01d431aa95a5648e2b5fc9bc3f071e
|
4
|
+
data.tar.gz: 21c75a976572f1c31ea4ea762f5702ab616ed947
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ad5e918d36cfc0e311af12524deec6f01ad01ed3eff33249312ee75fb7af165b65694d145ce017fe13f73debbf25902b764a2e5a10fc4b2cf194f752f19908f
|
7
|
+
data.tar.gz: 04f48adb18e68f95c258e1c386cb66ff386648cbc68780c7f75957f6d2f41913b9ed52e67dc1b660968a652cb2639666012a8819adef3e31f0324f068083402f
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -9,8 +9,6 @@ Footing provides some sanity for monkey patching practices.
|
|
9
9
|
It's also a utility lib that contains additional functionality for core objects that you might find useful.
|
10
10
|
Think of it as a lightweight version of ActiveSupport that doesn't implicitly change native behavior.
|
11
11
|
|
12
|
-
#### NOTE: this lib is experimental at the moment
|
13
|
-
|
14
12
|
## No implicit monkey patching
|
15
13
|
|
16
14
|
You must explicitly apply monkey patches.
|
@@ -55,14 +53,6 @@ s.respond_to? :escape # => true
|
|
55
53
|
"foo".respond_to? :escape # => false
|
56
54
|
```
|
57
55
|
|
58
|
-
## Shotgun patching
|
59
|
-
|
60
|
-
For the lazy and brave, you can also patch everything at once.
|
61
|
-
|
62
|
-
```ruby
|
63
|
-
Footing.patch_all!
|
64
|
-
```
|
65
|
-
|
66
56
|
## Patch free
|
67
57
|
|
68
58
|
Dont like monkey patches? Run patch free by setting up utility methods instead.
|
data/lib/footing.rb
CHANGED
@@ -1,78 +1,55 @@
|
|
1
1
|
require "delegate"
|
2
|
-
require File.
|
2
|
+
require File.expand_path("../footing/version", __FILE__)
|
3
3
|
|
4
4
|
module Footing
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
"
|
13
|
-
|
14
|
-
]
|
15
|
-
end
|
16
|
-
|
17
|
-
# Applies all Footing patches.
|
18
|
-
def self.patch_all!
|
19
|
-
modules.each do |name|
|
20
|
-
context = Object.const_get(name)
|
21
|
-
footing = Footing.const_get(name)
|
22
|
-
patch! context, footing
|
5
|
+
class << self
|
6
|
+
|
7
|
+
# Patches a Module or instance with the given extension.
|
8
|
+
# @param [Module, Object] obj The Module or instance to patch.
|
9
|
+
# @param [Module] extension The Module that contains the patches.
|
10
|
+
def patch!(obj, extension)
|
11
|
+
context = patch_context(obj)
|
12
|
+
raise "#{obj.class.name} doesn't support patching!" unless context
|
13
|
+
context.send :include, extension
|
23
14
|
end
|
24
|
-
end
|
25
15
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
16
|
+
# Creates class methods for all instance methods in the module.
|
17
|
+
# This allows users to invoke utility methods rather than monkey patching if they so desire.
|
18
|
+
# @param [Module] mod The Module to setup util methods for.
|
19
|
+
def util!(mod)
|
20
|
+
proxy = Class.new(SimpleDelegator)
|
21
|
+
Footing.patch! proxy, mod
|
22
|
+
|
23
|
+
mod.instance_methods(false).each do |method_name|
|
24
|
+
mod.define_singleton_method(method_name) do |target, *args|
|
25
|
+
method = proxy.instance_method(method_name)
|
26
|
+
target = proxy.new(target)
|
27
|
+
if method.parameters.empty?
|
28
|
+
method.bind(target).call
|
29
|
+
else
|
30
|
+
method.bind(target).call(*args)
|
31
|
+
end
|
35
32
|
end
|
36
|
-
rescue Exception
|
37
33
|
end
|
38
34
|
end
|
39
35
|
|
40
|
-
|
41
|
-
context.send :include, extension
|
42
|
-
end
|
36
|
+
private
|
43
37
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
# Creates class methods for all instance methods in the module.
|
50
|
-
# This allows users to invoke utility methods rather than monkey patching if they so desire.
|
51
|
-
# @param [Module] mod The Module to setup util methods for.
|
52
|
-
def self.util!(mod)
|
53
|
-
proxy = Class.new(SimpleDelegator)
|
54
|
-
Footing.patch! proxy, mod
|
55
|
-
|
56
|
-
mod.instance_methods(false).each do |method_name|
|
57
|
-
mod.define_singleton_method(method_name) do |target, *args|
|
58
|
-
method = proxy.instance_method(method_name)
|
59
|
-
target = proxy.new(target)
|
60
|
-
if method.parameters.empty?
|
61
|
-
method.bind(target).call
|
62
|
-
else
|
63
|
-
method.bind(target).call(*args)
|
38
|
+
def patch_context(obj)
|
39
|
+
context = obj if obj.is_a? Module
|
40
|
+
begin
|
41
|
+
context ||= class << obj
|
42
|
+
self
|
64
43
|
end
|
44
|
+
rescue Exception
|
65
45
|
end
|
46
|
+
context
|
66
47
|
end
|
67
|
-
end
|
68
48
|
|
49
|
+
end
|
69
50
|
end
|
70
51
|
|
71
|
-
Dir[File.expand_path(
|
72
|
-
next if file =~
|
73
|
-
|
74
|
-
load file
|
75
|
-
else
|
76
|
-
require file
|
77
|
-
end
|
52
|
+
Dir[File.expand_path("../**/*.rb", __FILE__)].each do |file|
|
53
|
+
next if file =~ /(lib\/footing\.rb|version\.rb)\z/
|
54
|
+
ENV["FOOTING_DEV"] ? load(file) : require(file)
|
78
55
|
end
|
@@ -14,7 +14,7 @@ module Footing
|
|
14
14
|
# @return [Hash] A new Hash that has been re-keyed.
|
15
15
|
def rekey(method_name)
|
16
16
|
inject({}) do |new_hash, (key, value)|
|
17
|
-
new_hash[key.
|
17
|
+
new_hash[key.public_send(method_name)] = value
|
18
18
|
new_hash
|
19
19
|
end
|
20
20
|
end
|
@@ -37,7 +37,7 @@ module Footing
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
#
|
40
|
+
# Recursively adjusts the values of the Hash in place.
|
41
41
|
#
|
42
42
|
# @example
|
43
43
|
# dict = {:a => 1, :b => 2, :c => 3}
|
@@ -46,8 +46,15 @@ module Footing
|
|
46
46
|
#
|
47
47
|
# @yield [value] Yields the current value to the block.
|
48
48
|
# The result of the block is then assigned to the corresponding key.
|
49
|
-
def adjust_values!
|
50
|
-
each
|
49
|
+
def adjust_values!(&block)
|
50
|
+
each do |key, value|
|
51
|
+
if value.respond_to?(:adjust_values!)
|
52
|
+
value.adjust_values!(&block)
|
53
|
+
else
|
54
|
+
self[key] = yield(value)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
self
|
51
58
|
end
|
52
59
|
|
53
60
|
# Recursively casts all string values in this Hash.
|
@@ -1,13 +1,12 @@
|
|
1
1
|
module Footing
|
2
2
|
module NilClass
|
3
3
|
|
4
|
-
#
|
5
|
-
#
|
6
|
-
# It becomes especially helpful when navigating through deeply nested Hashes.
|
4
|
+
# Calling [] on nil returns nil instead of raising an exception.
|
5
|
+
# Helpful when looping over nested Hashes.
|
7
6
|
#
|
8
7
|
# @example
|
9
8
|
# dict = {}
|
10
|
-
# dict[:foo][:bar][:
|
9
|
+
# dict[:foo][:bar][:baz] # => nil
|
11
10
|
#
|
12
11
|
# @param [Object] key The key to lookup.
|
13
12
|
def [](key)
|
@@ -15,5 +15,14 @@ module Footing
|
|
15
15
|
!eigen.nil?
|
16
16
|
end
|
17
17
|
|
18
|
+
# Trys to invoke a method on the object.
|
19
|
+
# Returns nil if the method isn't supported.
|
20
|
+
def try(name, *args, &block)
|
21
|
+
if respond_to?(name)
|
22
|
+
return public_send(name, *args, &block)
|
23
|
+
end
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
18
27
|
end
|
19
28
|
end
|
@@ -22,8 +22,7 @@ module Footing
|
|
22
22
|
(1..length).map{ |i| (chars - opts[:reject]).sample }.join
|
23
23
|
end
|
24
24
|
|
25
|
-
# Escapes a series of chars
|
26
|
-
# NOTE: A new string is returned.
|
25
|
+
# Escapes a series of chars.
|
27
26
|
def escape(*chars)
|
28
27
|
gsub(/(?<!\\)(#{chars.join("|")})/) do |char|
|
29
28
|
"\\" + char
|
data/lib/footing/version.rb
CHANGED
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: footing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Hopkins
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: micro_test
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: micro_mock
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: pry
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: pry-stack_explorer
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
description: A utility belt lib with sane monkey patching.
|
@@ -87,6 +87,12 @@ executables: []
|
|
87
87
|
extensions: []
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/footing.rb
|
90
96
|
- lib/footing/extensions/array.rb
|
91
97
|
- lib/footing/extensions/hash.rb
|
92
98
|
- lib/footing/extensions/kernel.rb
|
@@ -97,12 +103,6 @@ files:
|
|
97
103
|
- lib/footing/extensions/schema_statements.rb
|
98
104
|
- lib/footing/extensions/string.rb
|
99
105
|
- lib/footing/version.rb
|
100
|
-
- lib/footing.rb
|
101
|
-
- Gemfile
|
102
|
-
- Gemfile.lock
|
103
|
-
- LICENSE.txt
|
104
|
-
- Rakefile
|
105
|
-
- README.md
|
106
106
|
homepage: https://github.com/hopsoft/footing
|
107
107
|
licenses:
|
108
108
|
- MIT
|
@@ -113,17 +113,17 @@ require_paths:
|
|
113
113
|
- lib
|
114
114
|
required_ruby_version: !ruby/object:Gem::Requirement
|
115
115
|
requirements:
|
116
|
-
- -
|
116
|
+
- - ">="
|
117
117
|
- !ruby/object:Gem::Version
|
118
118
|
version: '0'
|
119
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
120
|
requirements:
|
121
|
-
- -
|
121
|
+
- - ">="
|
122
122
|
- !ruby/object:Gem::Version
|
123
123
|
version: '0'
|
124
124
|
requirements: []
|
125
125
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.0
|
126
|
+
rubygems_version: 2.2.0
|
127
127
|
signing_key:
|
128
128
|
specification_version: 4
|
129
129
|
summary: A utility belt lib with sane monkey patching.
|
@@ -139,4 +139,3 @@ test_files:
|
|
139
139
|
- lib/footing/extensions/string.rb
|
140
140
|
- lib/footing/version.rb
|
141
141
|
- lib/footing.rb
|
142
|
-
has_rdoc:
|