hash_dealer 1.1.0 → 1.1.2
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/Guardfile +1 -1
- data/VERSION +1 -1
- data/hash_dealer.gemspec +2 -2
- data/lib/core_extensions.rb +34 -15
- data/lib/hash_dealer.rb +13 -35
- data/spec/lib/hash_dealer_spec.rb +43 -0
- metadata +3 -3
data/Guardfile
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# A sample Guardfile
|
|
2
2
|
# More info at https://github.com/guard/guard#readme
|
|
3
3
|
|
|
4
|
-
guard 'rspec', :version => 2 do
|
|
4
|
+
guard 'rspec', :version => 2, :cli => "--color --format nested" do
|
|
5
5
|
watch(%r{^spec/.+_spec\.rb$})
|
|
6
6
|
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
|
7
7
|
watch('spec/spec_helper.rb') { "spec" }
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
1.1.
|
|
1
|
+
1.1.2
|
data/hash_dealer.gemspec
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{hash_dealer}
|
|
8
|
-
s.version = "1.1.
|
|
8
|
+
s.version = "1.1.2"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["Dan Langevin"]
|
|
12
|
-
s.date = %q{2011-08-
|
|
12
|
+
s.date = %q{2011-08-20}
|
|
13
13
|
s.description = %q{Like Factory Girl but for Hashes only}
|
|
14
14
|
s.email = %q{dan.langevin@lifebooker.com}
|
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/core_extensions.rb
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
class Object
|
|
2
|
+
# allows us to call matcher blindly
|
|
3
|
+
def matcher(opts = {})
|
|
4
|
+
return self
|
|
5
|
+
end
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# The only really important matcher
|
|
9
|
+
class String
|
|
10
|
+
def matcher(opts = {})
|
|
11
|
+
":#{self}"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
1
15
|
class Hash
|
|
2
16
|
def pathify_strings!
|
|
3
17
|
self.each_pair do |k,v|
|
|
@@ -8,6 +22,22 @@ class Hash
|
|
|
8
22
|
end
|
|
9
23
|
end
|
|
10
24
|
end
|
|
25
|
+
# recursively get a matcher for each value
|
|
26
|
+
def matcher(opts = {})
|
|
27
|
+
opts[:only] ||= self.keys.collect(&:to_sym)
|
|
28
|
+
opts[:only] = opts[:only].collect(&:to_sym)
|
|
29
|
+
opts[:only] -= (opts[:except] || []).collect(&:to_sym)
|
|
30
|
+
|
|
31
|
+
ret = self.class.new
|
|
32
|
+
self.each_pair do |k,v|
|
|
33
|
+
if opts[:only].include?(k.to_sym)
|
|
34
|
+
ret[k] = v.matcher(opts[k.to_sym] || {})
|
|
35
|
+
else
|
|
36
|
+
ret[k] = v
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
ret
|
|
40
|
+
end
|
|
11
41
|
end
|
|
12
42
|
class Array
|
|
13
43
|
def pathify_strings!
|
|
@@ -19,19 +49,8 @@ class Array
|
|
|
19
49
|
end
|
|
20
50
|
end
|
|
21
51
|
end
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
ret = HashDealer::Kief.new
|
|
26
|
-
self.each_pair do |k,v|
|
|
27
|
-
if v.instance_of?(Hash)
|
|
28
|
-
ret[k] = v.to_kief
|
|
29
|
-
elsif v.instance_of?(Array)
|
|
30
|
-
ret[k] = v.collect{|n| n.instance_of?(Hash) ? n.to_kief : n}
|
|
31
|
-
else
|
|
32
|
-
ret[k] = v
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
ret
|
|
52
|
+
# call matcher on all of the elements
|
|
53
|
+
def matcher(opts = {})
|
|
54
|
+
self.collect(&:matcher)
|
|
36
55
|
end
|
|
37
|
-
end
|
|
56
|
+
end
|
data/lib/hash_dealer.rb
CHANGED
|
@@ -31,22 +31,11 @@ class HashDealer
|
|
|
31
31
|
def root(value)
|
|
32
32
|
@attributes = value
|
|
33
33
|
end
|
|
34
|
-
|
|
35
|
-
# method missing
|
|
36
|
-
def method_missing(meth, *args, &block)
|
|
37
|
-
raise Exception.new("Please provide either a String or a block to #{meth}") unless (args.length == 1 || (args.empty? && block_given?))
|
|
38
|
-
@attributes ||= Kief.new
|
|
39
|
-
if block_given?
|
|
40
|
-
@attributes[meth.to_sym] = block
|
|
41
|
-
else
|
|
42
|
-
@attributes[meth.to_sym] = args.first.is_a?(Hash) ? args.first.to_kief : args.first
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
34
|
+
# get the stored attributes for this HashDealer
|
|
46
35
|
def attributes(*args)
|
|
47
36
|
# allows us to set a root value
|
|
48
37
|
return @attributes unless @attributes.is_a?(Hash)
|
|
49
|
-
att = @parent ? HashDealer.roll(@parent.to_sym) :
|
|
38
|
+
att = @parent ? HashDealer.roll(@parent.to_sym) : Hash.new
|
|
50
39
|
@attributes.each do |k,v|
|
|
51
40
|
att[k] = v.is_a?(Proc) ? v.call(*args) : v
|
|
52
41
|
end
|
|
@@ -58,28 +47,17 @@ class HashDealer
|
|
|
58
47
|
end
|
|
59
48
|
att
|
|
60
49
|
end
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return val
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
# get this Kief as a matcher - works recursively
|
|
76
|
-
def matcher
|
|
77
|
-
ret = self.class.new
|
|
78
|
-
self.each_pair do |k,v|
|
|
79
|
-
ret[k] = make_matcher(v)
|
|
80
|
-
end
|
|
81
|
-
ret
|
|
50
|
+
|
|
51
|
+
protected
|
|
52
|
+
|
|
53
|
+
# method missing
|
|
54
|
+
def method_missing(meth, *args, &block)
|
|
55
|
+
raise Exception.new("Please provide either a String or a block to #{meth}") unless (args.length == 1 || (args.empty? && block_given?))
|
|
56
|
+
@attributes ||= Hash.new
|
|
57
|
+
if block_given?
|
|
58
|
+
@attributes[meth.to_sym] = block
|
|
59
|
+
else
|
|
60
|
+
@attributes[meth.to_sym] = args.first
|
|
82
61
|
end
|
|
83
62
|
end
|
|
84
|
-
|
|
85
63
|
end
|
|
@@ -61,4 +61,47 @@ describe HashDealer do
|
|
|
61
61
|
String.new(HashDealer.roll(:variable).matcher[:abc]).should eql ":test"
|
|
62
62
|
String.new(HashDealer.roll(:variable).matcher[:val][:k]).should eql ":v"
|
|
63
63
|
end
|
|
64
|
+
|
|
65
|
+
it "should not alter fields passed into the matcher method in the except array" do
|
|
66
|
+
HashDealer.define(:variable) do
|
|
67
|
+
a("test_a")
|
|
68
|
+
b("test_b")
|
|
69
|
+
end
|
|
70
|
+
HashDealer.roll(:variable).matcher(:except => [:b])[:a].should eql(":test_a")
|
|
71
|
+
HashDealer.roll(:variable).matcher(:except => [:b])[:b].should eql("test_b")
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "should only alter fields passed into the matcher method in the the only array" do
|
|
75
|
+
HashDealer.define(:variable) do
|
|
76
|
+
a("test_a")
|
|
77
|
+
b("test_b")
|
|
78
|
+
end
|
|
79
|
+
HashDealer.roll(:variable).matcher(:only => [:b])[:a].should eql("test_a")
|
|
80
|
+
HashDealer.roll(:variable).matcher(:only => [:b])[:b].should eql(":test_b")
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "should apply except/only to nested values if they are defined by hash dealer and specified" do
|
|
84
|
+
HashDealer.define(:parent) do
|
|
85
|
+
a("test_a")
|
|
86
|
+
b{HashDealer.roll(:child)}
|
|
87
|
+
end
|
|
88
|
+
HashDealer.define(:child) do
|
|
89
|
+
a("child_a")
|
|
90
|
+
b("child_b")
|
|
91
|
+
end
|
|
92
|
+
HashDealer.roll(:parent).matcher(:only => [:b], :b => {:except => [:a]})[:a].should eql("test_a")
|
|
93
|
+
HashDealer.roll(:parent).matcher(:only => [:b], :b => {:except => [:a]})[:b][:a].should eql("child_a")
|
|
94
|
+
HashDealer.roll(:parent).matcher(:only => [:b], :b => {:except => [:a]})[:b][:b].should eql(":child_b")
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it "should define a matcher for when the response is an Array" do
|
|
98
|
+
HashDealer.define(:variable) do
|
|
99
|
+
root([{
|
|
100
|
+
:abc => "123",
|
|
101
|
+
:deff => "1234"
|
|
102
|
+
}])
|
|
103
|
+
end
|
|
104
|
+
HashDealer.roll(:variable).matcher.should eql([{:abc => ":123", :deff => ":1234"}])
|
|
105
|
+
end
|
|
106
|
+
|
|
64
107
|
end
|
metadata
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
name: hash_dealer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease:
|
|
5
|
-
version: 1.1.
|
|
5
|
+
version: 1.1.2
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
8
8
|
- Dan Langevin
|
|
@@ -10,7 +10,7 @@ autorequire:
|
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
12
|
|
|
13
|
-
date: 2011-08-
|
|
13
|
+
date: 2011-08-20 00:00:00 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: activesupport
|
|
@@ -129,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
129
129
|
requirements:
|
|
130
130
|
- - ">="
|
|
131
131
|
- !ruby/object:Gem::Version
|
|
132
|
-
hash:
|
|
132
|
+
hash: 1622066731891646773
|
|
133
133
|
segments:
|
|
134
134
|
- 0
|
|
135
135
|
version: "0"
|