footing 1.0.0 → 1.0.1
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 +2 -2
- data/README.md +2 -1
- data/footing.gemspec +5 -3
- data/lib/footing/hash.rb +112 -3
- data/lib/footing/object.rb +9 -24
- data/lib/footing/version.rb +1 -1
- metadata +6 -8
- data/lib/footing/concerns/hash_methods.rb +0 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e66b564326f970428910e2be5bd1d662ef34bfbc
|
4
|
+
data.tar.gz: 015758361e2e8bbaa11af55463600a71f64d6afb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1eaea2d976f77af0181df11cb0aa505ae3cf8bb5fd80dd68664921c8a6c5a303bc153a10b4af0d702451647eabc8b028ffbb6458f039578821f4f286b97b4b3
|
7
|
+
data.tar.gz: bf2537037bf8556cb851d692aae8bd69f0ede809a0831f908d075628163da54456ccea5d8eedee33c7b8d052c1c0795010990f6060694541bab8aa53a8391949
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
[](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
|
2
2
|
[](https://codeclimate.com/github/hopsoft/footing)
|
3
3
|
[](https://gemnasium.com/hopsoft/footing)
|
4
|
+
[](https://gemnasium.com/hopsoft/footing)
|
4
5
|
[](https://travis-ci.org/hopsoft/footing)
|
5
6
|
[](https://coveralls.io/r/hopsoft/footing?branch=master)
|
6
7
|
[](http://rubygems.org/gems/footing)
|
data/footing.gemspec
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "footing/version"
|
2
5
|
|
3
6
|
Gem::Specification.new do |spec|
|
4
7
|
spec.name = "footing"
|
5
8
|
spec.license = "MIT"
|
6
9
|
spec.version = Footing::VERSION
|
7
10
|
spec.homepage = "https://github.com/hopsoft/footing"
|
8
|
-
spec.summary = "
|
9
|
-
spec.description = "A utility belt lib with sane monkey patching."
|
11
|
+
spec.summary = "An ActiveSupport style utility library that employs delegation instead of monkey patching"
|
10
12
|
|
11
13
|
spec.authors = ["Nathan Hopkins"]
|
12
14
|
spec.email = ["natehop@gmail.com"]
|
data/lib/footing/hash.rb
CHANGED
@@ -1,7 +1,116 @@
|
|
1
|
-
require "footing/concerns/hash_methods"
|
2
|
-
|
3
1
|
module Footing
|
4
2
|
class Hash < Footing::Object
|
5
|
-
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def new(o={})
|
6
|
+
return o if o.is_a?(self)
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(o={})
|
12
|
+
super
|
13
|
+
initialize_nested
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns a standard ruby Hash representation of the wrapped Hash.
|
17
|
+
# @return [Hash]
|
18
|
+
def to_h
|
19
|
+
copied_object.each_with_object({}) do |pair, memo|
|
20
|
+
value = pair.last
|
21
|
+
if value.is_a?(Footing::Hash)
|
22
|
+
memo[pair.first] = value.to_h
|
23
|
+
elsif value.is_a?(::Array)
|
24
|
+
memo[pair.first] = value.map do |val|
|
25
|
+
if val.is_a?(Footing::Hash)
|
26
|
+
val.to_h
|
27
|
+
else
|
28
|
+
val
|
29
|
+
end
|
30
|
+
end
|
31
|
+
else
|
32
|
+
memo[pair.first] = value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
alias_method :to_hash, :to_h
|
38
|
+
|
39
|
+
# Recursively updates keys in place by passing them though the given block.
|
40
|
+
# IMPORTANT: This mutates the copied_object.
|
41
|
+
#
|
42
|
+
# @yield [key] Yields the key to the given block
|
43
|
+
# @return [Footing::Hash] Returns self
|
44
|
+
def update_keys!(&block)
|
45
|
+
@copied_object = copied_object.each_with_object({}) do |pair, memo|
|
46
|
+
key = pair.first
|
47
|
+
new_key = block.call(key)
|
48
|
+
value = pair.last
|
49
|
+
if value.is_a?(Footing::Hash)
|
50
|
+
memo[new_key] = value.update_keys!(&block)
|
51
|
+
elsif value.is_a?(::Array)
|
52
|
+
memo[new_key] = value.map do |val|
|
53
|
+
if val.is_a?(Footing::Hash)
|
54
|
+
val.update_keys!(&block)
|
55
|
+
else
|
56
|
+
val
|
57
|
+
end
|
58
|
+
end
|
59
|
+
else
|
60
|
+
memo[new_key] = value
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
self
|
65
|
+
end
|
66
|
+
|
67
|
+
# Recursively filters the values for the specified keys in place.
|
68
|
+
# IMPORTANT: This mutates the copied_object.
|
69
|
+
#
|
70
|
+
# @param keys [Array<Symbol,String,Regexp>] The keys to filter
|
71
|
+
# @param replacement [Object] The replacement value to use
|
72
|
+
# @return [Footing::Hash] Returns self
|
73
|
+
def filter!(keys, replacement: "[FILTERED]")
|
74
|
+
should_replace = lambda do |key|
|
75
|
+
replace = false
|
76
|
+
keys.each do |k|
|
77
|
+
break if replace
|
78
|
+
replace = k.is_a?(Regexp) ? key.to_s =~ k : key.to_s == k.to_s
|
79
|
+
end
|
80
|
+
replace
|
81
|
+
end
|
82
|
+
|
83
|
+
copied_object.each do |key, value|
|
84
|
+
if value.is_a?(Footing::Hash)
|
85
|
+
value.filter!(keys, replacement: replacement)
|
86
|
+
elsif value.is_a?(::Array)
|
87
|
+
value.each do |val|
|
88
|
+
if val.is_a?(Footing::Hash)
|
89
|
+
value.filter!(keys, replacement: replacement)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
else
|
93
|
+
value = replacement if should_replace.call(key)
|
94
|
+
self[key] = value
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
self
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def initialize_nested
|
104
|
+
copied_object.each do |key, value|
|
105
|
+
if value.is_a? ::Hash
|
106
|
+
copied_object[key] = Footing::Hash.new(value)
|
107
|
+
elsif value.is_a? ::Array
|
108
|
+
copied_object[key] = value.each_with_object([]) do |v, memo|
|
109
|
+
v = Footing::Hash.new(v) if v.is_a?(::Hash)
|
110
|
+
memo << v
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
6
115
|
end
|
7
116
|
end
|
data/lib/footing/object.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
module Footing
|
2
2
|
class Object
|
3
|
-
|
4
|
-
|
5
|
-
attr_reader :inner_object
|
3
|
+
attr_reader :original_object, :copied_object
|
6
4
|
|
7
5
|
class << self
|
8
6
|
def target_name
|
@@ -13,41 +11,28 @@ module Footing
|
|
13
11
|
o.class.ancestors.map(&:name).include?(target_name)
|
14
12
|
end
|
15
13
|
|
16
|
-
def new(o
|
14
|
+
def new(o)
|
17
15
|
return o if o.is_a?(self)
|
18
|
-
super
|
19
|
-
end
|
20
|
-
|
21
|
-
def copy(o)
|
22
|
-
Marshal.load Marshal.dump(o)
|
16
|
+
super
|
23
17
|
end
|
24
18
|
end
|
25
19
|
|
26
|
-
def initialize(o
|
20
|
+
def initialize(o)
|
27
21
|
raise ArgumentError.new("Types must match") unless self.class.match?(o)
|
28
|
-
|
29
|
-
@
|
30
|
-
end
|
31
|
-
|
32
|
-
def eigen
|
33
|
-
@eigen ||= class << self
|
34
|
-
self
|
35
|
-
end
|
22
|
+
@original_object = o
|
23
|
+
@copied_object = o.dup
|
36
24
|
end
|
37
25
|
|
38
26
|
def method_missing(name, *args)
|
39
|
-
if
|
40
|
-
|
41
|
-
define_method(name) { |*a| inner_object.send name, *a }
|
42
|
-
end
|
43
|
-
return inner_object.send name, *args
|
27
|
+
if copied_object.respond_to?(name)
|
28
|
+
return copied_object.send name, *args
|
44
29
|
end
|
45
30
|
|
46
31
|
super
|
47
32
|
end
|
48
33
|
|
49
34
|
def respond_to?(name)
|
50
|
-
return true if
|
35
|
+
return true if copied_object.respond_to?(name)
|
51
36
|
super
|
52
37
|
end
|
53
38
|
|
data/lib/footing/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: footing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
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: 2016-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description:
|
55
|
+
description:
|
56
56
|
email:
|
57
57
|
- natehop@gmail.com
|
58
58
|
executables: []
|
@@ -66,7 +66,6 @@ files:
|
|
66
66
|
- Rakefile
|
67
67
|
- footing.gemspec
|
68
68
|
- lib/footing.rb
|
69
|
-
- lib/footing/concerns/hash_methods.rb
|
70
69
|
- lib/footing/hash.rb
|
71
70
|
- lib/footing/object.rb
|
72
71
|
- lib/footing/version.rb
|
@@ -90,14 +89,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
89
|
version: '0'
|
91
90
|
requirements: []
|
92
91
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.4.5
|
92
|
+
rubygems_version: 2.4.5.1
|
94
93
|
signing_key:
|
95
94
|
specification_version: 4
|
96
|
-
summary:
|
95
|
+
summary: An ActiveSupport style utility library that employs delegation instead of
|
96
|
+
monkey patching
|
97
97
|
test_files:
|
98
|
-
- lib/footing/concerns/hash_methods.rb
|
99
98
|
- lib/footing/hash.rb
|
100
99
|
- lib/footing/object.rb
|
101
100
|
- lib/footing/version.rb
|
102
101
|
- lib/footing.rb
|
103
|
-
has_rdoc:
|
@@ -1,39 +0,0 @@
|
|
1
|
-
module Footing
|
2
|
-
module HashMethods
|
3
|
-
|
4
|
-
# Recursively filters the values for the specified keys in place.
|
5
|
-
# IMPORTANT: This mutates the Hash.
|
6
|
-
#
|
7
|
-
# @param keys [Array<Symbol,String,Regexp>] The keys to filter
|
8
|
-
# @param replacement [Object] The replacement value to use
|
9
|
-
# @return [Footing::Hash] Returns self
|
10
|
-
def filter!(keys, replacement: "[FILTERED]")
|
11
|
-
should_replace = lambda do |key|
|
12
|
-
replace = false
|
13
|
-
keys.each do |k|
|
14
|
-
break if replace
|
15
|
-
replace = k.is_a?(Regexp) ? key.to_s =~ k : key.to_s == k.to_s
|
16
|
-
end
|
17
|
-
replace
|
18
|
-
end
|
19
|
-
|
20
|
-
Footing::Hash.new(self).inner_object.each do |key, value|
|
21
|
-
if value.is_a?(::Hash)
|
22
|
-
Footing::Hash.new(value, copy: false).filter!(keys, replacement: replacement)
|
23
|
-
elsif value.is_a?(Enumerable)
|
24
|
-
value.each do |val|
|
25
|
-
if val.is_a?(::Hash)
|
26
|
-
Footing::Hash.new(val, copy: false).filter!(keys, replacement: replacement)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
else
|
30
|
-
value = replacement if should_replace.call(key)
|
31
|
-
self[key] = value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
self
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|