basic_assumption 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ BasicAssumption
2
+ ===============
3
+
4
+ A clone of DecentExposure, with less shared-state in its specs.
5
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1
@@ -0,0 +1,25 @@
1
+ module BasicAssumption
2
+ def self.extended(base)
3
+ base.default_assumption {}
4
+ end
5
+
6
+ def default_assumption(&block)
7
+ define_method(:default_assumption) do
8
+ block
9
+ end
10
+ end
11
+
12
+ def assume(name, &block)
13
+ define_method(name) do
14
+ @basic_assumptions ||= {}
15
+ @basic_assumptions[name] ||= if block_given?
16
+ instance_eval(&block)
17
+ else
18
+ instance_exec(name, &default_assumption)
19
+ end
20
+ end
21
+ after_assumption(name)
22
+ end
23
+
24
+ def after_assumption(name); end
25
+ end
@@ -0,0 +1,14 @@
1
+ begin
2
+ require File.join(File.dirname(__FILE__), 'lib', 'basic_assumption')
3
+ rescue LoadError
4
+ require 'basic_assumption'
5
+ end
6
+
7
+ ActionController::Base.class_eval do
8
+ extend BasicAssumption
9
+
10
+ def self.after_assumption(name)
11
+ hide_action name
12
+ helper_method name
13
+ end
14
+ end
@@ -0,0 +1,140 @@
1
+ require 'spec_helper'
2
+ require 'action_controller'
3
+
4
+ class Extender
5
+ def quack
6
+ {:foo => :bar}
7
+ end
8
+ end
9
+
10
+ describe BasicAssumption do
11
+
12
+ context "when a class extends BasicAssumption" do
13
+
14
+ let(:extender_class) { Class.new(Extender) }
15
+ let(:extender_instance) { extender_class.new }
16
+ before(:each) do
17
+ extender_class.extend(BasicAssumption)
18
+ end
19
+
20
+ it "declares named resources via #assume" do
21
+ expect {
22
+ extender_class.class_eval do
23
+ assume :resource_name
24
+ end
25
+ }.to_not raise_error(NoMethodError)
26
+ end
27
+
28
+ it "declares an instance method of the given name" do
29
+ extender_class.class_eval do
30
+ assume :my_method_name
31
+ end
32
+ extender_instance.should respond_to(:my_method_name)
33
+ end
34
+
35
+ context "the instance method" do
36
+ context "when no block was passed to assume" do
37
+ it "returns nil by default" do
38
+ extender_class.class_eval do
39
+ assume :by_default
40
+ end
41
+ extender_instance.by_default.should be_nil
42
+ end
43
+
44
+ context "when the default is overridden" do
45
+ it "returns the result of the overriding block" do
46
+ extender_class.class_eval do
47
+ default_assumption { 'overridden' }
48
+ assume :overriden
49
+ end
50
+ extender_instance.overriden.should eql('overridden')
51
+ end
52
+
53
+ it "executes the default in the context of the extending instance" do
54
+ extender_class.class_eval do
55
+ default_assumption { quack }
56
+ assume(:access_instance)
57
+ end
58
+ extender_instance.access_instance.should eql({:foo => :bar})
59
+ end
60
+
61
+ it "passes the name into the default block" do
62
+ extender_class.class_eval do
63
+ default_assumption { |name| name }
64
+ assume(:given_name)
65
+ end
66
+ extender_instance.given_name.should eql(:given_name)
67
+ end
68
+ end
69
+ end
70
+
71
+ context "when a block was passed" do
72
+ it "invokes the block as the method implementation" do
73
+ extender_class.class_eval do
74
+ assume(:resource) { 'this is my resource' }
75
+ end
76
+ extender_instance.resource.should eql('this is my resource')
77
+ end
78
+
79
+ it "executes in the context of the extending instance" do
80
+ extender_class.class_eval do
81
+ assume(:access_instance) { quack }
82
+ end
83
+ extender_instance.access_instance.should eql({:foo => :bar})
84
+ end
85
+ end
86
+
87
+ it "memoizes the result for further calls" do
88
+ extender_class.class_eval do
89
+ assume(:random_once) { "#{rand(1_000_000)} #{rand(1_000_000)}" }
90
+ end
91
+ extender_instance.random_once.should eql(extender_instance.random_once)
92
+ end
93
+ end
94
+ end
95
+
96
+ context "within Rails" do
97
+ before(:all) do
98
+ require 'rails/init.rb'
99
+ end
100
+ let(:controller_class) { Class.new(::ActionController::Base) }
101
+ let(:controller_instance) { controller_class.new }
102
+
103
+ it "is extended by ActionController::Base" do
104
+ ::ActionController::Base.should respond_to(:assume)
105
+ end
106
+
107
+ context "the instance method created by #assume" do
108
+ it "is hidden from being an action" do
109
+ controller_class.should_receive(:hide_action).with(:resource_name)
110
+ controller_class.class_eval do
111
+ assume(:resource_name)
112
+ end
113
+ end
114
+
115
+ it "is visible in views" do
116
+ controller_class.should_receive(:helper_method).with(:resource_name)
117
+ controller_class.class_eval do
118
+ assume(:resource_name)
119
+ end
120
+ end
121
+ end
122
+
123
+ context "classes derived from ActionController::Base" do
124
+ before do
125
+ ::ActionController::Base.class_eval do
126
+ default_assumption { |name| "#{name}#{name}" }
127
+ end
128
+ end
129
+ let(:derived_class) { Class.new(controller_class) }
130
+ let(:derived_instance) { derived_class.new }
131
+
132
+ it "inherit the default assumption" do
133
+ derived_class.class_eval do
134
+ assume(:twice)
135
+ end
136
+ derived_instance.twice.should eql('twicetwice')
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1 @@
1
+ require 'basic_assumption'
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: basic_assumption
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 1
9
+ version: 0.2.1
10
+ platform: ruby
11
+ authors:
12
+ - Matt Yoho
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-17 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 3
30
+ - 0
31
+ version: 1.3.0
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: "\n Allows a simple declarative idiom for accessing resources in controllers and views\n "
35
+ email: mby@mattyoho.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.md
42
+ files:
43
+ - README.md
44
+ - VERSION
45
+ - lib/basic_assumption.rb
46
+ - rails/init.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/mattyoho/basic_assumption
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.6
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Allows a simple declarative idiom for resources in controllers and views
77
+ test_files:
78
+ - spec/spec_helper.rb
79
+ - spec/lib/basic_assumption_spec.rb