hash_dealer 1.3.1 → 1.3.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/VERSION +1 -1
- data/hash_dealer.gemspec +1 -1
- data/lib/core_extensions.rb +34 -0
- data/spec/lib/hash_dealer_spec.rb +58 -23
- metadata +2 -2
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
1.3.
|
|
1
|
+
1.3.2
|
data/hash_dealer.gemspec
CHANGED
data/lib/core_extensions.rb
CHANGED
|
@@ -5,6 +5,40 @@ class Object
|
|
|
5
5
|
end
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
+
class TimeDateMatcher
|
|
9
|
+
attr_reader :instance
|
|
10
|
+
def initialize(instance)
|
|
11
|
+
@instance = instance
|
|
12
|
+
end
|
|
13
|
+
def ==(other)
|
|
14
|
+
self.instance.class == other.class
|
|
15
|
+
end
|
|
16
|
+
alias_method :eql?, :==
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# including a module didn't work here - not sure why though
|
|
20
|
+
[Time, DateTime, Date].each do |klass|
|
|
21
|
+
klass.class_eval <<-EOE, __FILE__, __LINE__ + 1
|
|
22
|
+
def matcher(opts = {})
|
|
23
|
+
TimeDateMatcher.new(self)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
alias_method :old_eql?, :eql?
|
|
27
|
+
alias_method :old_equals_equals, :==
|
|
28
|
+
|
|
29
|
+
def ==(other)
|
|
30
|
+
return other == self if other.is_a?(TimeDateMatcher)
|
|
31
|
+
return old_equals_equals(other)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def eql?(other)
|
|
35
|
+
return other.eql?(self) if other.is_a?(TimeDateMatcher)
|
|
36
|
+
return old_eql?(other)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
EOE
|
|
40
|
+
end
|
|
41
|
+
|
|
8
42
|
class Numeric
|
|
9
43
|
def matcher(opts={})
|
|
10
44
|
PathString.new(":#{self}")
|
|
@@ -51,33 +51,68 @@ describe HashDealer do
|
|
|
51
51
|
HashDealer.roll(:variable, {"abc" => "123"})[:abc].should eql("123")
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
HashDealer.define(:variable) do
|
|
56
|
-
abc("test")
|
|
57
|
-
val({
|
|
58
|
-
:k => "v"
|
|
59
|
-
})
|
|
60
|
-
end
|
|
61
|
-
String.new(HashDealer.roll(:variable).matcher[:abc]).should eql ":test"
|
|
62
|
-
String.new(HashDealer.roll(:variable).matcher[:val][:k]).should eql ":v"
|
|
63
|
-
end
|
|
54
|
+
context "Matchers" do
|
|
64
55
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
56
|
+
it "should extend itself to matchers - preventing us from having to re-define them" do
|
|
57
|
+
HashDealer.define(:variable) do
|
|
58
|
+
abc("test")
|
|
59
|
+
val({
|
|
60
|
+
:k => "v"
|
|
61
|
+
})
|
|
62
|
+
end
|
|
63
|
+
String.new(HashDealer.roll(:variable).matcher[:abc]).should eql ":test"
|
|
64
|
+
String.new(HashDealer.roll(:variable).matcher[:val][:k]).should eql ":v"
|
|
69
65
|
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
66
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
67
|
+
it "should not alter fields passed into the matcher method in the except array" do
|
|
68
|
+
HashDealer.define(:variable) do
|
|
69
|
+
a("test_a")
|
|
70
|
+
b("test_b")
|
|
71
|
+
end
|
|
72
|
+
HashDealer.roll(:variable).matcher(:except => [:b])[:a].should eql(":test_a")
|
|
73
|
+
HashDealer.roll(:variable).matcher(:except => [:b])[:b].should eql("test_b")
|
|
78
74
|
end
|
|
79
|
-
|
|
80
|
-
|
|
75
|
+
|
|
76
|
+
it "should only alter fields passed into the matcher method in the the only array" do
|
|
77
|
+
HashDealer.define(:variable) do
|
|
78
|
+
a("test_a")
|
|
79
|
+
b("test_b")
|
|
80
|
+
end
|
|
81
|
+
HashDealer.roll(:variable).matcher(:only => [:b])[:a].should eql("test_a")
|
|
82
|
+
HashDealer.roll(:variable).matcher(:only => [:b])[:b].should eql(":test_b")
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "should create a wrapper for times and dates" do
|
|
86
|
+
HashDealer.define(:variable) do
|
|
87
|
+
time_test(Time.now)
|
|
88
|
+
date_test(Date.today)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
time_test = HashDealer.roll(:variable).matcher[:time_test]
|
|
92
|
+
date_test = HashDealer.roll(:variable).matcher[:date_test]
|
|
93
|
+
|
|
94
|
+
time_test.should be_instance_of TimeDateMatcher
|
|
95
|
+
date_test.should be_instance_of TimeDateMatcher
|
|
96
|
+
|
|
97
|
+
time_test.should eql (Time.now - 1000)
|
|
98
|
+
time_test.should_not eql (Date.today)
|
|
99
|
+
date_test.should eql (Date.today)
|
|
100
|
+
date_test.should_not eql (Time.now)
|
|
101
|
+
|
|
102
|
+
# check the reverse too
|
|
103
|
+
(Time.now - 1000).should eql(time_test)
|
|
104
|
+
(Time.now - 1000).should == time_test
|
|
105
|
+
(Date.today).should_not eql(time_test)
|
|
106
|
+
(Date.today).should_not == time_test
|
|
107
|
+
|
|
108
|
+
# regular behavior should be unaffected
|
|
109
|
+
t = Time.now
|
|
110
|
+
t.should eql t.clone
|
|
111
|
+
t.should == t.clone
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
end
|
|
115
|
+
|
|
81
116
|
end
|
|
82
117
|
|
|
83
118
|
it "should apply except/only to nested values if they are defined by hash dealer and specified" do
|
metadata
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
name: hash_dealer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease:
|
|
5
|
-
version: 1.3.
|
|
5
|
+
version: 1.3.2
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
8
8
|
- Dan Langevin
|
|
@@ -141,7 +141,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
141
141
|
requirements:
|
|
142
142
|
- - ">="
|
|
143
143
|
- !ruby/object:Gem::Version
|
|
144
|
-
hash:
|
|
144
|
+
hash: 538716834169318373
|
|
145
145
|
segments:
|
|
146
146
|
- 0
|
|
147
147
|
version: "0"
|