temporaries 0.1.0 → 0.2.0
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/CHANGELOG +4 -0
- data/README.markdown +6 -0
- data/lib/temporaries/values.rb +22 -14
- data/lib/temporaries/version.rb +1 -1
- data/spec/unit/temporaries/values_spec.rb +114 -0
- metadata +4 -4
data/CHANGELOG
CHANGED
data/README.markdown
CHANGED
@@ -8,6 +8,7 @@ Set things temporarily and declaratively. Best way to test.
|
|
8
8
|
* Set values of instance variables.
|
9
9
|
* Set values of class variables.
|
10
10
|
* Set values of globals.
|
11
|
+
* Set definitions of methods.
|
11
12
|
* Set a temporary directory.
|
12
13
|
|
13
14
|
Do it for the length of a test, or within a specific block in your
|
@@ -35,6 +36,7 @@ Here's the full list of methods you can use:
|
|
35
36
|
use_instance_variable_value(object, :name, value)
|
36
37
|
use_class_variable_value(object, :name, value)
|
37
38
|
use_global_value(:name, value)
|
39
|
+
use_method_definition(module, :name, ->(*args){defintion})
|
38
40
|
|
39
41
|
Sigils in the `name` are unnecessary in the last 3 (unlike
|
40
42
|
`instance_variable_get`, for instance).
|
@@ -73,6 +75,10 @@ You may also set the temporary for the duration of a particular block:
|
|
73
75
|
...
|
74
76
|
end
|
75
77
|
|
78
|
+
with_method_definition module, :name, ->(*args){definition} do
|
79
|
+
...
|
80
|
+
end
|
81
|
+
|
76
82
|
with_temporary_directory path do
|
77
83
|
# `tmp' refers to the directory.
|
78
84
|
end
|
data/lib/temporaries/values.rb
CHANGED
@@ -100,35 +100,35 @@ module Temporaries
|
|
100
100
|
end
|
101
101
|
|
102
102
|
mod.class_eval <<-EOS, __FILE__, __LINE__ + 1
|
103
|
-
def push_#{name}
|
103
|
+
def push_#{name}(#{signature}, value)
|
104
104
|
#{save}
|
105
105
|
push_temporary(#{stack_key}, original_value)
|
106
106
|
#{set}
|
107
107
|
end
|
108
108
|
|
109
|
-
def pop_#{name}
|
109
|
+
def pop_#{name}(#{signature})
|
110
110
|
value = pop_temporary(#{stack_key})
|
111
111
|
#{restore}
|
112
112
|
end
|
113
113
|
|
114
|
-
def with_#{name}
|
115
|
-
push_#{name}
|
114
|
+
def with_#{name}(#{signature}, value)
|
115
|
+
push_#{name}(#{signature}, value)
|
116
116
|
begin
|
117
117
|
yield
|
118
118
|
ensure
|
119
|
-
pop_#{name}
|
119
|
+
pop_#{name}(#{signature})
|
120
120
|
end
|
121
121
|
end
|
122
122
|
EOS
|
123
123
|
|
124
124
|
mod::ClassMethods.module_eval <<-EOS, __FILE__, __LINE__ + 1
|
125
|
-
def use_#{name}
|
125
|
+
def use_#{name}(#{signature}, value)
|
126
126
|
temporaries_adapter.before do
|
127
|
-
push_#{name}
|
127
|
+
push_#{name}(#{signature}, value)
|
128
128
|
end
|
129
129
|
|
130
130
|
temporaries_adapter.after do
|
131
|
-
pop_#{name}
|
131
|
+
pop_#{name}(#{signature})
|
132
132
|
end
|
133
133
|
end
|
134
134
|
EOS
|
@@ -148,7 +148,7 @@ module Temporaries
|
|
148
148
|
helpers.define(self)
|
149
149
|
end
|
150
150
|
|
151
|
-
define_helpers_for :
|
151
|
+
define_helpers_for :constant_value do
|
152
152
|
signature 'mod, constant'
|
153
153
|
exists 'mod.const_defined?(constant)'
|
154
154
|
get 'mod.const_get(constant)'
|
@@ -159,13 +159,13 @@ module Temporaries
|
|
159
159
|
remove 'mod.send :remove_const, constant'
|
160
160
|
end
|
161
161
|
|
162
|
-
define_helpers_for :
|
162
|
+
define_helpers_for :attribute_value do
|
163
163
|
signature 'object, attribute'
|
164
164
|
get 'object.send(attribute)'
|
165
165
|
set 'object.send("#{attribute}=", value)'
|
166
166
|
end
|
167
167
|
|
168
|
-
define_helpers_for :
|
168
|
+
define_helpers_for :hash_value do
|
169
169
|
signature 'hash, key'
|
170
170
|
exists 'hash.key?(key)'
|
171
171
|
get 'hash[key]'
|
@@ -173,7 +173,7 @@ module Temporaries
|
|
173
173
|
remove 'hash.delete(key)'
|
174
174
|
end
|
175
175
|
|
176
|
-
define_helpers_for :
|
176
|
+
define_helpers_for :instance_variable_value do
|
177
177
|
signature 'object, name'
|
178
178
|
exists 'object.instance_variable_defined?("@#{name}")'
|
179
179
|
get 'object.instance_variable_get "@#{name}"'
|
@@ -181,7 +181,7 @@ module Temporaries
|
|
181
181
|
remove 'object.send :remove_instance_variable, "@#{name}"'
|
182
182
|
end
|
183
183
|
|
184
|
-
define_helpers_for :
|
184
|
+
define_helpers_for :class_variable_value do
|
185
185
|
signature 'klass, name'
|
186
186
|
exists 'klass.class_variable_defined?("@@#{name}")'
|
187
187
|
get 'klass.send :class_variable_get, "@@#{name}"'
|
@@ -189,10 +189,18 @@ module Temporaries
|
|
189
189
|
remove 'klass.send :remove_class_variable, "@@#{name}"'
|
190
190
|
end
|
191
191
|
|
192
|
-
define_helpers_for :
|
192
|
+
define_helpers_for :global_value do
|
193
193
|
signature 'name'
|
194
194
|
get 'eval("$#{name}")'
|
195
195
|
set 'eval("$#{name} = value")'
|
196
196
|
end
|
197
|
+
|
198
|
+
define_helpers_for :method_definition do
|
199
|
+
signature 'klass, name'
|
200
|
+
exists 'klass.method_defined?(name) && (method = klass.instance_method(name)) && method.owner.equal?(klass)'
|
201
|
+
get 'method'
|
202
|
+
set 'klass.__send__(:define_method, name, value)'
|
203
|
+
remove 'klass.__send__(:remove_method, name)'
|
204
|
+
end
|
197
205
|
end
|
198
206
|
end
|
data/lib/temporaries/version.rb
CHANGED
@@ -531,6 +531,96 @@ describe Temporaries::Values do
|
|
531
531
|
$variable.should == 2
|
532
532
|
end
|
533
533
|
end
|
534
|
+
|
535
|
+
describe "#push_method_definition and #pop_method_definition" do
|
536
|
+
before do
|
537
|
+
@klass = Class.new
|
538
|
+
@instance = @klass.new
|
539
|
+
end
|
540
|
+
|
541
|
+
describe "when the method already exists" do
|
542
|
+
before do
|
543
|
+
@klass.send(:define_method, :meth) { 2 }
|
544
|
+
end
|
545
|
+
|
546
|
+
it "should set the given module's method to the pushed definition until it is popped" do
|
547
|
+
@instance.meth.should == 2
|
548
|
+
@context.push_method_definition @klass, :meth, lambda{3}
|
549
|
+
@instance.meth.should == 3
|
550
|
+
@context.pop_method_definition @klass, :meth
|
551
|
+
@instance.meth.should == 2
|
552
|
+
end
|
553
|
+
|
554
|
+
it "should be nestable" do
|
555
|
+
@instance.meth.should == 2
|
556
|
+
@context.push_method_definition @klass, :meth, lambda{3}
|
557
|
+
@instance.meth.should == 3
|
558
|
+
@context.push_method_definition @klass, :meth, lambda{5}
|
559
|
+
@instance.meth.should == 5
|
560
|
+
@context.pop_method_definition @klass, :meth
|
561
|
+
@instance.meth.should == 3
|
562
|
+
@context.pop_method_definition @klass, :meth
|
563
|
+
@instance.meth.should == 2
|
564
|
+
end
|
565
|
+
end
|
566
|
+
|
567
|
+
describe "when the method does not already exist" do
|
568
|
+
it "should set the given module's method to the pushed definition, and remove it when popped" do
|
569
|
+
@klass.method_defined?(:meth).should be_false
|
570
|
+
@context.push_method_definition @klass, :meth, lambda{3}
|
571
|
+
@instance.meth.should == 3
|
572
|
+
@context.pop_method_definition @klass, :meth
|
573
|
+
@klass.method_defined?(:meth).should be_false
|
574
|
+
end
|
575
|
+
end
|
576
|
+
end
|
577
|
+
|
578
|
+
describe "#with_method_definition" do
|
579
|
+
before do
|
580
|
+
@klass = Class.new{def meth; 2; end}
|
581
|
+
@instance = @klass.new
|
582
|
+
end
|
583
|
+
|
584
|
+
it "should set the given module's method to the given definition only during the given block" do
|
585
|
+
@instance.meth.should == 2
|
586
|
+
block_run = false
|
587
|
+
@context.with_method_definition @klass, :meth, lambda{3} do
|
588
|
+
block_run = true
|
589
|
+
@instance.meth.should == 3
|
590
|
+
end
|
591
|
+
block_run.should be_true
|
592
|
+
@instance.meth.should == 2
|
593
|
+
end
|
594
|
+
|
595
|
+
it "should be nestable" do
|
596
|
+
@instance.meth.should == 2
|
597
|
+
blocks_run = []
|
598
|
+
@context.with_method_definition @klass, :meth, lambda{3} do
|
599
|
+
blocks_run << 1
|
600
|
+
@instance.meth.should == 3
|
601
|
+
@context.with_method_definition @klass, :meth, lambda{5} do
|
602
|
+
blocks_run << 2
|
603
|
+
@instance.meth.should == 5
|
604
|
+
end
|
605
|
+
@instance.meth.should == 3
|
606
|
+
end
|
607
|
+
blocks_run.should == [1, 2]
|
608
|
+
@instance.meth.should == 2
|
609
|
+
end
|
610
|
+
|
611
|
+
it "should handle nonlocal exits" do
|
612
|
+
exception_class = Class.new(Exception)
|
613
|
+
@instance.meth.should == 2
|
614
|
+
begin
|
615
|
+
@context.with_method_definition @klass, :meth, lambda{3} do
|
616
|
+
@instance.meth.should == 3
|
617
|
+
raise exception_class, 'boom'
|
618
|
+
end
|
619
|
+
rescue exception_class => e
|
620
|
+
end
|
621
|
+
@instance.meth.should == 2
|
622
|
+
end
|
623
|
+
end
|
534
624
|
end
|
535
625
|
|
536
626
|
describe Temporaries::Values do
|
@@ -680,4 +770,28 @@ describe Temporaries::Values do
|
|
680
770
|
$variable.should == 2
|
681
771
|
end
|
682
772
|
end
|
773
|
+
|
774
|
+
describe ".use_method_definition" do
|
775
|
+
before do
|
776
|
+
@klass = klass = Class.new { def meth; 2; end }
|
777
|
+
@instance = @klass.new
|
778
|
+
|
779
|
+
@context_class = Class.new(TestContext) do
|
780
|
+
include Temporaries::Values
|
781
|
+
use_method_definition klass, :meth, lambda{3}
|
782
|
+
end
|
783
|
+
@context = @context_class.new
|
784
|
+
end
|
785
|
+
|
786
|
+
it "should set the given module's constant to the given value for the duration of the run" do
|
787
|
+
@instance.meth.should == 2
|
788
|
+
block_run = false
|
789
|
+
@context.run do
|
790
|
+
block_run = true
|
791
|
+
@instance.meth.should == 3
|
792
|
+
end
|
793
|
+
block_run.should be_true
|
794
|
+
@instance.meth.should == 2
|
795
|
+
end
|
796
|
+
end
|
683
797
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: temporaries
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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-06-
|
12
|
+
date: 2012-06-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ritual
|
@@ -107,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
107
|
version: '0'
|
108
108
|
segments:
|
109
109
|
- 0
|
110
|
-
hash:
|
110
|
+
hash: -3674253645093316834
|
111
111
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
112
|
none: false
|
113
113
|
requirements:
|
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
116
|
version: '0'
|
117
117
|
segments:
|
118
118
|
- 0
|
119
|
-
hash:
|
119
|
+
hash: -3674253645093316834
|
120
120
|
requirements: []
|
121
121
|
rubyforge_project:
|
122
122
|
rubygems_version: 1.8.24
|