surrogate 0.4.2 → 0.4.3

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/Readme.md CHANGED
@@ -358,6 +358,8 @@ TODO
358
358
 
359
359
  * Add a better explanation for motivations
360
360
  * Figure out whether I'm supposed to be using clone or dup for the object -.^ (looks like there may also be an `initialize_copy` method I can take advantage of instead of crazy stupid shit I'm doing now)
361
+ * don't blow up when delegating to the Object#initialize with args
362
+ * queues should not reset when done, they should raise
361
363
 
362
364
 
363
365
  Future Features
@@ -76,7 +76,7 @@ class Surrogate
76
76
 
77
77
  def enable_defining_methods(klass)
78
78
  def klass.define(method_name, options={}, &block)
79
- @hatchery.define method_name, options, block
79
+ @hatchery.define method_name, options, &block
80
80
  end
81
81
 
82
82
  def klass.api_method_names
@@ -11,7 +11,7 @@ class Surrogate
11
11
  klass_can_define_api_methods
12
12
  end
13
13
 
14
- def define(method_name, options={}, block)
14
+ def define(method_name, options={}, &block)
15
15
  add_api_method_for method_name
16
16
  add_verb_helpers_for method_name
17
17
  add_noun_helpers_for method_name
@@ -17,7 +17,7 @@ class Surrogate
17
17
 
18
18
  def invoke_method(method_name, args, &block)
19
19
  invoked_methods[method_name] << args
20
- return get_default method_name, args unless has_ivar? method_name
20
+ return get_default method_name, args, &block unless has_ivar? method_name
21
21
  Value.factory(get_ivar method_name).value(self, method_name)
22
22
  end
23
23
 
@@ -64,8 +64,8 @@ class Surrogate
64
64
  end
65
65
  end
66
66
 
67
- def get_default(method_name, args)
68
- api_methods[method_name].default instance, args do
67
+ def get_default(method_name, args, &block)
68
+ api_methods[method_name].default instance, args, block do
69
69
  raise UnpreparedMethodError, "#{method_name} has been invoked without being told how to behave"
70
70
  end
71
71
  end
@@ -1,3 +1,5 @@
1
+ require 'bindable_block'
2
+
1
3
  class Surrogate
2
4
  class Options
3
5
  attr_accessor :options, :default_proc
@@ -18,10 +20,21 @@ class Surrogate
18
20
  options
19
21
  end
20
22
 
21
- def default(instance, args, &no_default)
22
- return options[:default] if options.has_key? :default
23
- return instance.instance_exec(*args, &default_proc) if default_proc
24
- no_default.call
23
+ def default(instance, args, block, &no_default)
24
+ if options.has_key? :default
25
+ options[:default]
26
+ elsif default_proc
27
+ # This works for now, but it's a kind of crappy solution because
28
+ # BindableBlock adds and removes methods for each time it is invoked.
29
+ #
30
+ # A better solution would be to instantiate it before passing it to
31
+ # the options, then we only have to bind it to an instance and invoke
32
+ BindableBlock.new(instance.class, &default_proc)
33
+ .bind(instance)
34
+ .call(*args, &block)
35
+ else
36
+ no_default.call
37
+ end
25
38
  end
26
39
  end
27
40
  end
@@ -1,3 +1,3 @@
1
1
  class Surrogate
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
@@ -6,31 +6,48 @@ describe 'define' do
6
6
 
7
7
  describe 'in the block' do
8
8
  it 'is an api method for the class' do
9
- pristine_klass = Class.new do
10
- Surrogate.endow self do
11
- define(:find) { 123 }
12
- end
13
- end
14
-
15
- klass1 = pristine_klass.clone
16
- klass1.should_not have_been_told_to :find
17
- klass1.find(1).should == 123
18
- klass1.should have_been_told_to(:find).with(1)
9
+ pristine_klass = Class.new { Surrogate.endow(self) { define :find } }
10
+ pristine_klass.singleton_class.api_method_names.should == [:find]
19
11
  end
20
12
  end
21
13
 
22
14
 
23
15
  describe 'out of the block' do
24
16
  it 'is an api method for the instance' do
25
- mocked_class.define(:book) { 'book' }
26
- instance.book.should == 'book'
17
+ mocked_class.define :book
18
+ mocked_class.api_method_names.should == [:book]
19
+ end
20
+ end
21
+
22
+ describe 'definition default block invocation' do
23
+ xit "something about raising an error if arity is wrong" do
24
+ mocked_class.define(:a) { |arg| 1 }
25
+ mocked_class.new.a.should == 1
26
+ mocked_class.new.a(2).should == 1
27
+ mocked_class.new.a(3).should == 1
28
+ end
29
+
30
+ it "is passed the arguments" do
31
+ arg = nil
32
+ mocked_class.define(:meth) { |inner_arg| arg = inner_arg }.new.meth(1212)
33
+ arg.should == 1212
34
+ end
35
+
36
+ it "is passed the block" do
37
+ block = nil
38
+ mocked_class.define(:meth) { |&inner_block| block = inner_block }.new.meth { 12 }
39
+ block.call.should == 12
40
+ end
41
+
42
+ it "returns the value that the method returns" do
43
+ mocked_class.define(:meth) { 1234 }.new.meth.should == 1234
27
44
  end
28
45
  end
29
46
 
30
47
  describe 'declaring the behaviour' do
31
48
 
32
49
  describe 'for verbs' do
33
- before { mocked_class.define :wink }
50
+ before { mocked_class.define :wink }
34
51
 
35
52
  describe 'will_<api_method>' do
36
53
  it 'overrides the default value for the api method' do
data/surrogate.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- # specify any dependencies here; for example:
21
+ s.add_runtime_dependency 'bindable_block', '= 0.0.4'
22
+
22
23
  s.add_development_dependency "rspec", '~> 2.8.0' # TODO: Figure out how far back we can work with
23
- # s.add_runtime_dependency "rest-client"
24
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surrogate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-27 00:00:00.000000000 Z
12
+ date: 2012-06-05 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bindable_block
16
+ requirement: &70325486799660 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - =
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70325486799660
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: rspec
16
- requirement: &70344532950860 !ruby/object:Gem::Requirement
27
+ requirement: &70325486799160 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ~>
@@ -21,7 +32,7 @@ dependencies:
21
32
  version: 2.8.0
22
33
  type: :development
23
34
  prerelease: false
24
- version_requirements: *70344532950860
35
+ version_requirements: *70325486799160
25
36
  description: Framework to aid in handrolling mock/spy objects.
26
37
  email:
27
38
  - josh.cheek@gmail.com