cocktail 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Cocktail – Ruby parametric mixins
2
+ **Cocktail** is a ruby gem that allows you to use parametric mixins in Ruby
3
+
4
+ ## Installation
5
+
6
+ ```
7
+ gem install cocktail
8
+ ```
9
+
10
+ or in `Gemfile`
11
+
12
+ ``` rb
13
+ gem "cocktail"
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ### Defining parametric modules
19
+
20
+ ``` rb
21
+ module CocktailMaker
22
+ extend Mixin
23
+
24
+ mixed do |args|
25
+ args[:known_recipes].each do |cocktail|
26
+ define_method :"serve_#{cocktail.downcase}" do
27
+ "Here is a #{cocktail} for you"
28
+ end
29
+ end
30
+ end
31
+ end
32
+ ```
33
+
34
+ ### Mixing parametric modules
35
+
36
+ ``` rb
37
+ class Bartender
38
+ mixin CocktailMaker, :known_recipes => %W(Mojito Manhattan Daiquiri Negroni)
39
+ end
40
+ ```
41
+
42
+ ```
43
+ > puts Bartender.new.serve_a_mojito
44
+ Here is a Mojito for you
45
+ => nil
46
+ ```
47
+
48
+ ## Contributing to `cocktail`
49
+
50
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
51
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
52
+ * Fork the project.
53
+ * Start a feature/bugfix branch.
54
+ * Commit and push until you are happy with your contribution.
55
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
56
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
57
+
58
+ ---
59
+
60
+ Copyright (c) 2012 mcasimir
61
+
62
+ Permission is hereby granted, free of charge, to any person obtaining
63
+ a copy of this software and associated documentation files (the
64
+ "Software"), to deal in the Software without restriction, including
65
+ without limitation the rights to use, copy, modify, merge, publish,
66
+ distribute, sublicense, and/or sell copies of the Software, and to
67
+ permit persons to whom the Software is furnished to do so, subject to
68
+ the following conditions:
69
+
70
+ The above copyright notice and this permission notice shall be
71
+ included in all copies or substantial portions of the Software.
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
74
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
75
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
76
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
77
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
78
+ 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.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/cocktail.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cocktail/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cocktail"
7
+ s.version = Cocktail::VERSION
8
+ s.authors = ["Maurizio Casimirri"]
9
+ s.email = ["maurizio.cas@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Ruby parametric mixins}
12
+ s.description = %q{Cocktail is a ruby gem that allows you to use parametric mixins in Ruby}
13
+
14
+ s.rubyforge_project = "cocktail"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "RubyInline"
24
+ end
@@ -0,0 +1,21 @@
1
+ module Cocktail
2
+ module Mixable
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
+
15
+ def mixed(&block)
16
+ @_mixed_block = block
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ require 'cocktail/mixable/mixin'
2
+
3
+ module Cocktail
4
+ module Mixable
5
+
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module Cocktail
2
+ module Target
3
+
4
+ def mixin(mod, params = {})
5
+ mod.send :mixto, self, params
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Cocktail
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cocktail.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "cocktail/version"
2
+ require "cocktail/mixable"
3
+ require "cocktail/target"
4
+
5
+ # eg.
6
+ #
7
+ # class PostsController < Controller
8
+ # mixin(MyScaffold, :resource => Post, :only => :show)
9
+ #
10
+ # end
11
+
12
+ module Cocktail
13
+
14
+
15
+
16
+ end
17
+
18
+ Class.send :include, Cocktail::Target
19
+ Object.send :include, Cocktail::Mixable
@@ -0,0 +1,2 @@
1
+ class Breakpoint < Exception
2
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'test/unit'
5
+
6
+ class CocktailTest < Test::Unit::TestCase
7
+
8
+ Dir.glob(File.join(File.dirname(__FILE__), "test*.rb")).map{|p| File.basename(p, ".rb")}.each do |testname|
9
+ define_method testname do
10
+ require testname
11
+ end
12
+ end
13
+
14
+ end
15
+
data/test/helper.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'cocktail'
3
+ require 'breakpoint'
4
+
5
+ def colorize(text, color_code)
6
+ "#{color_code}#{text}\e[0m"
7
+ end
8
+
9
+ def red(text); colorize(text, "\e[31m"); end
10
+ def green(text); colorize(text, "\e[32m"); end
11
+ def blue(text); colorize(text, "\e[34m"); end
12
+ def bold(text); colorize(text, "\e[1m"); end
13
+ def fail(text); red("FAILED: " + text); end
14
+ def banner(text); print bold("\nTesting '#{text}' ... "); STDOUT.flush; end
15
+
16
+ include Test::Unit::Assertions
@@ -0,0 +1,5 @@
1
+ require 'helper'
2
+
3
+ banner "Test Cocktail defined"
4
+
5
+ assert defined?(Cocktail), fail("Cocktail not defined")
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ banner "Module/Target context clashes"
4
+
5
+ module M2
6
+ extend Mixin
7
+
8
+ mixed do |params|
9
+ define_method :"#{params[:name]}" do
10
+ "#{params[:name]}"
11
+ end
12
+ end
13
+ end
14
+
15
+ class A
16
+ mixin M2, :name => "john"
17
+
18
+ def params
19
+ {:name => "mary"}
20
+ end
21
+ end
22
+
23
+
24
+ assert A.new.respond_to?(:john), fail("Unexpected: target context shadows the module context")
25
+
@@ -0,0 +1,17 @@
1
+ require 'helper'
2
+
3
+ banner "Mixed called on mixin"
4
+
5
+ assert_raise(Breakpoint, fail("Breakpoint not reached")) do
6
+ module M
7
+ extend Mixin
8
+ mixed do |mix_params|
9
+ raise Breakpoint
10
+ end
11
+ end
12
+
13
+ class A
14
+ mixin(M)
15
+ end
16
+ end
17
+
@@ -0,0 +1,21 @@
1
+ require 'helper'
2
+
3
+ banner "Parametric method name"
4
+
5
+ module M2
6
+ extend Mixin
7
+
8
+ mixed do |mix_params|
9
+ define_method :"#{mix_params[:name]}" do
10
+ "My name is #{mix_params[:name]}"
11
+ end
12
+ end
13
+ end
14
+
15
+ class A
16
+ mixin M2, :name => "john"
17
+ end
18
+
19
+ assert A.new.respond_to?(:john), fail("Method not mixed in")
20
+ assert A.new.john == "My name is john", fail("Method block not parameterized")
21
+
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ banner "Target scope available"
4
+
5
+ module M2
6
+ extend Mixin
7
+
8
+ mixed do |mix_params|
9
+ define_method :"#{mix_params[:name]}" do
10
+ "Here is a #{drink} for #{mix_params[:name]}"
11
+ end
12
+ end
13
+ end
14
+
15
+ class A
16
+ mixin M2, :name => "john"
17
+
18
+ def drink
19
+ "Mojito"
20
+ end
21
+ end
22
+
23
+ assert A.new.respond_to?(:john), fail("Method not mixed in")
24
+ assert A.new.john == "Here is a Mojito for john", fail("Method block not parameterized or local context not available")
25
+
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cocktail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Maurizio Casimirri
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
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'
30
+ description: Cocktail is a ruby gem that allows you to use parametric mixins in Ruby
31
+ email:
32
+ - maurizio.cas@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - README.md
40
+ - Rakefile
41
+ - cocktail.gemspec
42
+ - lib/cocktail.rb
43
+ - lib/cocktail/mixable.rb
44
+ - lib/cocktail/mixable/mixin.rb
45
+ - lib/cocktail/target.rb
46
+ - lib/cocktail/version.rb
47
+ - test/breakpoint.rb
48
+ - test/cocktail_tests.rb
49
+ - test/helper.rb
50
+ - test/test_cocktail_defined.rb
51
+ - test/test_context_clashes.rb
52
+ - test/test_mixed_called_from_mixin.rb
53
+ - test/test_parametric_method_names.rb
54
+ - test/test_target_scope_available.rb
55
+ homepage: ''
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project: cocktail
75
+ rubygems_version: 1.8.24
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Ruby parametric mixins
79
+ test_files:
80
+ - test/breakpoint.rb
81
+ - test/cocktail_tests.rb
82
+ - test/helper.rb
83
+ - test/test_cocktail_defined.rb
84
+ - test/test_context_clashes.rb
85
+ - test/test_mixed_called_from_mixin.rb
86
+ - test/test_parametric_method_names.rb
87
+ - test/test_target_scope_available.rb