basic_assumption 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,10 @@
1
1
  require 'basic_assumption/configuration'
2
+ require 'basic_assumption/default_assumption'
2
3
 
3
4
  module BasicAssumption
4
- def self.extended(base)
5
- base.default_assumption {}
6
- end
7
-
8
- def default_assumption(&block)
9
- define_method(:default_assumption) do
10
- block
11
- end
5
+ def default_assumption(symbol=nil, &block)
6
+ default = block_given? ? block : symbol
7
+ BasicAssumption::DefaultAssumption.register(self, default)
12
8
  end
13
9
 
14
10
  def assume(name, &block)
@@ -17,7 +13,8 @@ module BasicAssumption
17
13
  @basic_assumptions[name] ||= if block_given?
18
14
  instance_eval(&block)
19
15
  else
20
- instance_exec(name, &default_assumption)
16
+ block = BasicAssumption::DefaultAssumption.resolve(self.class)
17
+ instance_exec(name, &block)
21
18
  end
22
19
  end
23
20
  after_assumption(name)
@@ -10,5 +10,9 @@ module BasicAssumption
10
10
  alias default_exposure default_assumption
11
11
  end
12
12
  end
13
+
14
+ def default_assumption=(value)
15
+ BasicAssumption::DefaultAssumption.default = value
16
+ end
13
17
  end
14
18
  end
@@ -0,0 +1,38 @@
1
+ require 'basic_assumption/default_assumption/base'
2
+ require 'basic_assumption/default_assumption/class_resolver'
3
+
4
+ module BasicAssumption
5
+ module DefaultAssumption
6
+ def self.register(klass, default)
7
+ registry[klass.object_id] = strategy(default)
8
+ end
9
+
10
+ def self.resolve(klass)
11
+ while !registry.has_key?(klass.object_id)
12
+ klass = klass.superclass
13
+ break if klass.nil?
14
+ end
15
+ registry[klass.object_id]
16
+ end
17
+
18
+ class << self
19
+ attr_accessor :default
20
+
21
+ protected
22
+ def registry
23
+ @registry ||= Hash.new { |h, k| strategy(default) }
24
+ end
25
+
26
+ def strategy(given=nil)
27
+ case given
28
+ when Proc
29
+ given
30
+ when Symbol
31
+ ClassResolver.instance(given).block
32
+ else
33
+ Base.new.block
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ module BasicAssumption
2
+ module DefaultAssumption
3
+ class Base
4
+ def block
5
+ Proc.new { |name| }
6
+ end
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,23 @@
1
+ module BasicAssumption
2
+ module DefaultAssumption
3
+ class ClassResolver
4
+ def self.instance(name, *args)
5
+ new(name).instance(*args)
6
+ end
7
+ def initialize(name)
8
+ @name = name
9
+ end
10
+ def instance(*args)
11
+ constantize(camelize(@name)).new(*args)
12
+ end
13
+ def camelize(name)
14
+ name.to_s.gsub(/(?:^|_)(.)/) { "#{$1.upcase}" }
15
+ end
16
+ def constantize(name)
17
+ namespace = BasicAssumption::DefaultAssumption
18
+ namespace.const_missing(name) unless namespace.const_defined? name
19
+ namespace.const_get(name)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,2 @@
1
+ require 'basic_assumption/default_assumption/simple_rails'
2
+
@@ -0,0 +1,13 @@
1
+ module BasicAssumption
2
+ module DefaultAssumption
3
+ class SimpleRails < BasicAssumption::DefaultAssumption::Base
4
+ def block
5
+ Proc.new do |name|
6
+ model_class = name.to_s.classify.constantize
7
+ model_class.find(params["#{name}_id"] || params['id'])
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,23 @@
1
+ if defined?(Rails) && Rails::VERSION::MAJOR == 3
2
+ require 'basic_assumption/default_assumption/rails'
3
+
4
+ module BasicAssumption
5
+ class Railtie < Rails::Railtie
6
+
7
+ initializer "basic_assumption.set_up_action_controller_base" do |app|
8
+ ActionController::Base.class_eval do
9
+ extend BasicAssumption
10
+
11
+ def self.after_assumption(name)
12
+ hide_action name
13
+ helper_method name
14
+ end
15
+ end
16
+
17
+ BasicAssumption::Configuration.configure do |config|
18
+ config.default_assumption = :simple_rails
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module BasicAssumption
2
+ VERSION = '0.3.0'
3
+ end
data/rails/init.rb CHANGED
@@ -1,8 +1,5 @@
1
- begin
2
- require File.join(File.dirname(__FILE__), 'lib', 'basic_assumption')
3
- rescue LoadError
4
- require 'basic_assumption'
5
- end
1
+ require 'basic_assumption'
2
+ require 'basic_assumption/default_assumption/rails'
6
3
 
7
4
  ActionController::Base.class_eval do
8
5
  extend BasicAssumption
@@ -11,9 +8,8 @@ ActionController::Base.class_eval do
11
8
  hide_action name
12
9
  helper_method name
13
10
  end
11
+ end
14
12
 
15
- default_assumption do |name|
16
- model_class = name.to_s.classify.constantize
17
- model_class.find(params["#{name}_id"] || params['id'])
18
- end
13
+ BasicAssumption::Configuration.configure do |config|
14
+ config.default_assumption = :simple_rails
19
15
  end
@@ -8,14 +8,26 @@ describe BasicAssumption::Configuration do
8
8
  end
9
9
 
10
10
  context "an instance" do
11
- class Exposer
11
+ class Assumer
12
12
  extend BasicAssumption
13
13
  end
14
- let(:instance) { BasicAssumption::Configuration.new }
15
- let(:exposing_class) { Exposer }
14
+ let(:config) { BasicAssumption::Configuration.new }
15
+ let(:assuming_class) { Class.new Assumer }
16
16
  it "allows decent_exposure emulation mode" do
17
- instance.emulate_exposure!
18
- exposing_class.should respond_to(:expose)
17
+ config.emulate_exposure!
18
+ assuming_class.should respond_to(:expose)
19
+ end
20
+
21
+ it "can set the default assumption" do
22
+ config.default_assumption = Proc.new { :qux }
23
+ assuming_class.class_eval do
24
+ assume :baz
25
+ end
26
+ assuming_class.new.baz.should eql(:qux)
27
+ end
28
+
29
+ after(:all) do
30
+ config.default_assumption = nil
19
31
  end
20
32
  end
21
33
  end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+ require 'active_support'
3
+
4
+ describe BasicAssumption::DefaultAssumption::Base do
5
+ context "#block.call" do
6
+ subject { BasicAssumption::DefaultAssumption::Base.new.block.call(:name) }
7
+ it { should be_nil }
8
+ end
9
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'basic_assumption/default_assumption/class_resolver'
3
+
4
+ class ::ExampleClass
5
+ attr_reader :foo, :bar
6
+ def initialize(foo=nil, bar=nil)
7
+ @foo = foo; @bar = bar
8
+ end
9
+ end
10
+
11
+ describe BasicAssumption::DefaultAssumption::ClassResolver do
12
+ let(:resolver) { BasicAssumption::DefaultAssumption::ClassResolver.new(nil) }
13
+ context '#camelize, given a symbol or string' do
14
+ it 'returns an upper camel-cased string' do
15
+ resolver.camelize('camel').should eql('Camel')
16
+ end
17
+ it 'treats underscores as word boundaries and removes them' do
18
+ resolver.camelize('camel_case_word').should eql('CamelCaseWord')
19
+ end
20
+ end
21
+ context '#constantize' do
22
+ it 'retrieves a constant from the BasicAssumption::DefaultAssumption namespace' do
23
+ resolver.constantize('ClassResolver').should eql(BasicAssumption::DefaultAssumption::ClassResolver)
24
+ end
25
+ it 'raises an error if no such constant is found' do
26
+ expect { resolver.constantize('CamelCase') }.to raise_error(NameError)
27
+ end
28
+ end
29
+ context '#instance' do
30
+ before(:each) do
31
+ @resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:example_class)
32
+ @resolver.stub!(:constantize).and_return(ExampleClass)
33
+ end
34
+ it 'returns an instance of the resolved class' do
35
+ @resolver.instance.should be_a_kind_of(ExampleClass)
36
+ end
37
+ it 'accepts and passes on any arguments to the instantiation of the object' do
38
+ example_object = @resolver.instance('foo', 'bar')
39
+ example_object.foo.should eql('foo')
40
+ example_object.bar.should eql('bar')
41
+ end
42
+ end
43
+ context '::instance' do
44
+ it 'returns an instance of the resolved class' do
45
+ mod = BasicAssumption::DefaultAssumption
46
+ mod::ClassResolver.instance(:base).should be_an_instance_of(mod::Base)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ require 'active_support'
3
+ require 'basic_assumption/default_assumption/simple_rails'
4
+
5
+ describe BasicAssumption::DefaultAssumption::SimpleRails do
6
+ class Model; end
7
+
8
+ context "#block" do
9
+ let(:default) { BasicAssumption::DefaultAssumption::SimpleRails.new }
10
+ let(:params) { stub(:[] => 42) }
11
+
12
+ before(:each) do
13
+ Model.stub!(:find)
14
+ default.stub!(:params).and_return(params)
15
+ end
16
+
17
+ it "looks for a params[model_id] and params[id] in its calling context" do
18
+ params.should_receive(:[]).with('model_id').and_return(nil)
19
+ params.should_receive(:[]).with('id')
20
+ default.block.call(:model)
21
+ end
22
+
23
+ it "attempts to find a model instance based off the given name" do
24
+ Model.should_receive(:find).with(42).and_return(:model)
25
+ default.block.call(:model).should eql(:model)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+ require 'lib/basic_assumption/default_assumption/simple_rails'
3
+
4
+ describe BasicAssumption::DefaultAssumption do
5
+ let(:mod) { BasicAssumption::DefaultAssumption }
6
+ describe "::register and ::resolve" do
7
+ it "maps an object to a proc according to an internal strategy" do
8
+ mod.should_receive(:strategy).with(:behavior).and_return(:block)
9
+ mod.register(Class, :behavior)
10
+ mod.resolve(Class).should eql(:block)
11
+ end
12
+ end
13
+ describe "::strategy" do
14
+ it "returns the proc when given one" do
15
+ a_proc = Proc.new { :block }
16
+ mod.send(:strategy, a_proc).call.should eql(:block)
17
+ end
18
+ it "returns the block of a class in the BasicAssumption::DefaultAssumption namespace when given a symbol" do
19
+ mod::SimpleRails.should_receive(:new).and_return(stub(:block => :block))
20
+ mod.send(:strategy, :simple_rails).should eql(:block)
21
+ end
22
+ it "returns the block of a Base instance otherwise" do
23
+ mod.send(:strategy, nil).call.should be_nil
24
+ end
25
+ end
26
+ describe "::registry" do
27
+ before(:each) do
28
+ mod.send(:registry).clear
29
+ end
30
+ it "returns a default when given a key it doesn't have" do
31
+ mod.default = :custom_default
32
+ mod.stub!(:strategy).with(:custom_default).and_return(:some_block)
33
+ mod.send(:registry).should_not have_key(:key)
34
+ mod.send(:registry)[:key].should eql(:some_block)
35
+ end
36
+ after(:each) do
37
+ mod.default = nil
38
+ end
39
+ end
40
+ end
@@ -120,7 +120,7 @@ describe BasicAssumption do
120
120
  end
121
121
  end
122
122
 
123
- context "classes derived from ActionController::Base" do
123
+ context "classes in the inheritance chain of ActionController::Base" do
124
124
  let(:application_controller) { Class.new(controller_class) }
125
125
  let(:derived_class) { Class.new(application_controller) }
126
126
  let(:derived_instance) { derived_class.new }
@@ -131,7 +131,7 @@ describe BasicAssumption do
131
131
  end
132
132
  end
133
133
 
134
- it "inherit the default assumption" do
134
+ it "inherits the default assumption" do
135
135
  derived_class.class_eval do
136
136
  assume(:twice)
137
137
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
7
  - 3
9
- version: 0.2.3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matt Yoho
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-17 00:00:00 -05:00
17
+ date: 2010-05-12 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -69,9 +69,15 @@ extra_rdoc_files:
69
69
  - README.md
70
70
  files:
71
71
  - README.md
72
- - VERSION
73
72
  - lib/basic_assumption.rb
74
73
  - lib/basic_assumption/configuration.rb
74
+ - lib/basic_assumption/default_assumption.rb
75
+ - lib/basic_assumption/default_assumption/base.rb
76
+ - lib/basic_assumption/default_assumption/class_resolver.rb
77
+ - lib/basic_assumption/default_assumption/rails.rb
78
+ - lib/basic_assumption/default_assumption/simple_rails.rb
79
+ - lib/basic_assumption/rails.rb
80
+ - lib/basic_assumption/version.rb
75
81
  - rails/init.rb
76
82
  has_rdoc: true
77
83
  homepage: http://github.com/mattyoho/basic_assumption
@@ -102,8 +108,12 @@ rubyforge_project:
102
108
  rubygems_version: 1.3.6
103
109
  signing_key:
104
110
  specification_version: 3
105
- summary: Allows a simple declarative idiom for resources in controllers and views
111
+ summary: Allows a simple declarative idiom for resources in controllers and views. Custom default behavior can be defined in a pluggable manner.
106
112
  test_files:
107
113
  - spec/spec_helper.rb
108
114
  - spec/lib/basic_assumption_spec.rb
109
115
  - spec/lib/basic_assumption/configuration_spec.rb
116
+ - spec/lib/basic_assumption/default_assumption_spec.rb
117
+ - spec/lib/basic_assumption/default_assumption/base_spec.rb
118
+ - spec/lib/basic_assumption/default_assumption/class_resolver_spec.rb
119
+ - spec/lib/basic_assumption/default_assumption/simple_rails_spec.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.2.3