cocktail 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -45,6 +45,64 @@ Here is a Mojito for you
45
45
  => nil
46
46
  ```
47
47
 
48
+ ### Composing Mixins
49
+
50
+ ``` rb
51
+ module Scaffold
52
+ extend Mixin
53
+
54
+ mixed do |*_args|
55
+ base, mixin_params, actions_module = _args
56
+
57
+ exclude = mixin_params[:exclude] || []
58
+ only = mixin_params[:only] || []
59
+ resource = mixin_params[:resource]
60
+
61
+ define_method :scaffolded_resource do
62
+ resource
63
+ end
64
+
65
+ actions = actions_module.instance_methods
66
+ actions = only.any? ? actions & only : actions
67
+ actions -= exclude
68
+
69
+ actions.each do |method_name|
70
+ method_proc = actions_module.instance_method method_name
71
+ define_method method_name, method_proc
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+ ```
78
+
79
+
80
+ ``` rb
81
+ module MyScaffold
82
+ extend Mixin
83
+
84
+ mixed do |base, params|
85
+ Scaffold.mixto(base, params, Actions)
86
+ end
87
+
88
+ module Actions
89
+ def index
90
+ @collection = scaffolded_resource.all
91
+ end
92
+
93
+ def show
94
+ @object = scaffolded_resource.find(@id)
95
+ end
96
+ end
97
+ end
98
+ ```
99
+
100
+ ``` rb
101
+ class PostsController < ApplicationController
102
+ mixin(MyScaffold, :resource => Post)
103
+ end
104
+ ```
105
+
48
106
  ## Contributing to `cocktail`
49
107
 
50
108
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
@@ -76,4 +134,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
76
134
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
77
135
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
78
136
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
79
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
137
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/cocktail.gemspec CHANGED
@@ -20,5 +20,4 @@ Gem::Specification.new do |s|
20
20
 
21
21
  # specify any dependencies here; for example:
22
22
  # s.add_development_dependency "rspec"
23
- s.add_runtime_dependency "RubyInline"
24
23
  end
data/lib/cocktail.rb CHANGED
@@ -1,19 +1,16 @@
1
+ if RUBY_VERSION < "1.9"
2
+ raise "Sorry, Cocktail requires ruby >= 1.9"
3
+ end
4
+
1
5
  require "cocktail/version"
2
6
  require "cocktail/mixable"
3
7
  require "cocktail/target"
4
8
 
5
- # eg.
6
- #
7
- # class PostsController < Controller
8
- # mixin(MyScaffold, :resource => Post, :only => :show)
9
- #
10
- # end
11
-
12
9
  module Cocktail
10
+ end
13
11
 
12
+ Class.send :include, Cocktail::Target
13
+ Object.send :include, Cocktail::Mixable
14
14
 
15
15
 
16
- end
17
16
 
18
- Class.send :include, Cocktail::Target
19
- Object.send :include, Cocktail::Mixable
@@ -1,20 +1,25 @@
1
1
  module Cocktail
2
2
  module Mixable
3
3
  module Mixin
4
-
5
- def mixto(base, params)
6
- params.freeze
7
-
8
- if instance_variable_defined?("@_mixed_block")
9
- mixed_block = @_mixed_block
10
- base.class_exec params, &mixed_block
11
- end
12
- end
13
- module_function :mixto
14
4
 
15
- def mixed(&block)
16
- @_mixed_block = block
17
- end
5
+ def mixto(base, params, *args)
6
+
7
+ if instance_variable_defined?("@_mixed_block")
8
+ mixed_block = @_mixed_block
9
+ if mixed_block.arity == -1
10
+ base.class_exec base, params, *args, &mixed_block
11
+ elsif mixed_block.arity == 2
12
+ base.class_exec base, params, &mixed_block
13
+ else
14
+ base.class_exec params, &mixed_block
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ def mixed(&block)
21
+ @_mixed_block = block
22
+ end
18
23
 
19
24
  end
20
25
  end
@@ -1,3 +1,3 @@
1
1
  module Cocktail
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,63 @@
1
+ require 'helper'
2
+
3
+ banner "Extending Mixins"
4
+
5
+ # A very generic scaffolding template
6
+ module Scaffold
7
+ extend Mixin
8
+
9
+ # base, params, actions
10
+ mixed do |*_args|
11
+ _base, _mixin_params, _actions = _args
12
+
13
+ _base.send :include, _actions
14
+
15
+ define_method :resource do
16
+ _mixin_params[:resource]
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ # A specific scaffolding template
24
+ module MyScaffold
25
+ extend Mixin
26
+
27
+ mixed do |base, params|
28
+ Scaffold.mixto(base, params, Actions)
29
+ end
30
+
31
+ module Actions
32
+ def index
33
+ @collection = resource.all
34
+ end
35
+
36
+ def show
37
+ @object = resource.find(@id)
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+
44
+ class Post
45
+ class << self
46
+ def all
47
+ "returning a collection of #{self.name}s"
48
+ end
49
+
50
+ def find(id)
51
+ "finding a #{self.name}"
52
+ end
53
+
54
+ end
55
+ end
56
+
57
+ class Controller
58
+ mixin MyScaffold, :resource => Post
59
+
60
+ end
61
+
62
+
63
+ assert Controller.new.show == "finding a Post"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocktail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,23 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-05-18 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: RubyInline
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
13
+ dependencies: []
30
14
  description: Cocktail is a ruby gem that allows you to use parametric mixins in Ruby
31
15
  email:
32
16
  - maurizio.cas@gmail.com
@@ -50,6 +34,7 @@ files:
50
34
  - test/test_cocktail_defined.rb
51
35
  - test/test_context_clashes.rb
52
36
  - test/test_mixed_called_from_mixin.rb
37
+ - test/test_module_extension.rb
53
38
  - test/test_parametric_method_names.rb
54
39
  - test/test_target_scope_available.rb
55
40
  homepage: ''
@@ -83,5 +68,6 @@ test_files:
83
68
  - test/test_cocktail_defined.rb
84
69
  - test/test_context_clashes.rb
85
70
  - test/test_mixed_called_from_mixin.rb
71
+ - test/test_module_extension.rb
86
72
  - test/test_parametric_method_names.rb
87
73
  - test/test_target_scope_available.rb