catalyst 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem 'mocha', ">=0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.2"
13
+ gem "rcov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ mocha (0.9.12)
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ shoulda (2.11.3)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.0.0)
19
+ jeweler (~> 1.5.2)
20
+ mocha
21
+ rcov
22
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ari Lerner
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,28 @@
1
+ = catalyst
2
+
3
+ Catalyst carries your application with a stack (with history) and middleware. Basically it let's you build your own rack-like strut for your apps. This is heavily based on the work that https://github.com/mitchellh and https://github.com/chneukirchen/rack have done in building a middleware architecture.
4
+
5
+ Example:
6
+
7
+ run_stack = Catalyst::RunStack.new do
8
+ use MiddlewareClass
9
+ use lambda {|env| }
10
+ end
11
+
12
+ run_stack.call(environment || {})
13
+
14
+ == Contributing to catalyst
15
+
16
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
17
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
18
+ * Fork the project
19
+ * Start a feature/bugfix branch
20
+ * Commit and push until you are happy with your contribution
21
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
22
+ * 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.
23
+
24
+ == Copyright
25
+
26
+ Copyright (c) 2011 Ari Lerner. See LICENSE.txt for
27
+ further details.
28
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "catalyst"
16
+ gem.homepage = "http://github.com/auser/catalyst"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{TODO: one-line summary of your gem}
19
+ gem.description = %Q{TODO: longer description of your gem}
20
+ gem.email = "arilerner@mac.com"
21
+ gem.authors = ["Ari Lerner"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "catalyst #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,15 @@
1
+ module Catalyst
2
+ class Environment
3
+
4
+ attr_reader :hash
5
+
6
+ def initialize(hsh={})
7
+ @hash = hsh
8
+ end
9
+
10
+ def []=(k,v)
11
+ @hash[k] = v
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ module Catalyst
2
+ class Runner
3
+ attr_reader :actions, :history
4
+
5
+ def initialize(actions)
6
+ @history = []
7
+ @actions = actions.map {|act| setup_action(act) }
8
+ end
9
+
10
+ def call(env)
11
+ return if @actions.empty?
12
+
13
+ begin
14
+ action = @history.unshift(@actions.shift).first
15
+ action.call(env)
16
+ rescue Exception => e
17
+ env["catalyst.error"] = e
18
+ raise
19
+ end
20
+ end
21
+
22
+ def setup_action(action)
23
+ klass, args, block = action
24
+
25
+ if klass.is_a?(Class)
26
+ klass.new(self, *args, &block)
27
+ elsif klass.respond_to?(:call)
28
+ lambda do |env|
29
+ klass.call(env)
30
+ self.call(env)
31
+ end
32
+ else
33
+ raise
34
+ end
35
+ end
36
+ end
37
+ end
data/lib/catalyst.rb ADDED
@@ -0,0 +1,43 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ module Catalyst
4
+ autoload :Runner, 'catalyst/runner'
5
+ autoload :Environment, 'catalyst/environment'
6
+
7
+ class RunStack
8
+
9
+ def initialize(&block)
10
+ instance_eval(&block) if block_given?
11
+ end
12
+
13
+ # middleware stack
14
+ def stack
15
+ @stack ||= []
16
+ end
17
+
18
+ # Use 'middleware'
19
+ def use(k, *args, &block)
20
+ if k.is_a?(RunStack)
21
+ stack.concat(k.stack)
22
+ else
23
+ stack << [k, *args, block]
24
+ end
25
+ self
26
+ end
27
+
28
+ # run is just another term for 'use'
29
+ def run
30
+ @run = app
31
+ end
32
+
33
+ # Turn this RunStack to a Runner
34
+ def to_app
35
+ Runner.new(stack)
36
+ end
37
+
38
+ # Start off the middleware
39
+ def call(env)
40
+ to_app.call(env.is_a?(Environment) ? env : Environment.new(env))
41
+ end
42
+ end
43
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+ require 'mocha'
13
+
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
15
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
+ require 'catalyst'
17
+
18
+ class Test::Unit::TestCase
19
+
20
+ def new_environment(env={})
21
+ Catalyst::Environment.new(env)
22
+ end
23
+
24
+ end
@@ -0,0 +1,43 @@
1
+ require 'helper'
2
+
3
+ class TestCatalyst < Test::Unit::TestCase
4
+ class TestCatalystMiddleware
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ @app.call(env)
11
+ end
12
+ end
13
+
14
+ context "Catalyst" do
15
+
16
+ should "be able to build a stack" do
17
+ assert_nothing_raised do
18
+ Catalyst::RunStack.new do |env|
19
+ end
20
+ end
21
+ end
22
+
23
+ should "be able to define a stack" do
24
+ assert_nothing_raised do
25
+ Catalyst::RunStack.new do |env|
26
+ use TestCatalystMiddleware
27
+ end
28
+ end
29
+ end
30
+
31
+ should "be able to call a stack" do
32
+ @@filtered_through_stack = false
33
+ @rs = Catalyst::RunStack.new do
34
+ use TestCatalystMiddleware
35
+ use lambda {|env| @@filtered_through_stack = true }
36
+ end
37
+
38
+ @rs.call({})
39
+ assert @@filtered_through_stack
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,61 @@
1
+ require 'helper'
2
+
3
+ class TestRunner < Test::Unit::TestCase
4
+
5
+ context "Runner" do
6
+
7
+ setup do
8
+ @klass = Catalyst::Runner
9
+ @instance = @klass.new([])
10
+ end
11
+
12
+ context "initializing" do
13
+ should "setup the action" do
14
+ middleware = [1,2,3]
15
+ middleware.each do |m|
16
+ @klass.any_instance.expects(:setup_action).with(m).returns(m)
17
+ end
18
+ @inst = @klass.new(middleware)
19
+ assert_equal @inst.actions, [1,2,3]
20
+ assert_empty @inst.history
21
+ end
22
+ end
23
+
24
+ context "setting up middleware" do
25
+ should "make non-classes lambdas" do
26
+ env = new_environment
27
+ env.expects(:foo).once
28
+
29
+ func = lambda { |x| x.foo }
30
+ @instance.setup_action(func).call(env)
31
+ end
32
+
33
+ should "raise exception if given invalid middleware" do
34
+ assert_raises(RuntimeError) {
35
+ @instance.setup_action(7)
36
+ }
37
+ end
38
+ end
39
+
40
+ context "calling" do
41
+ should "return if there are no actions to run" do
42
+ @instance.history.expects(:shift).never
43
+ assert !@instance.call(new_environment)
44
+ end
45
+
46
+ should "move an action to the history stack" do
47
+ @instance.actions << lambda { |env| }
48
+ assert @instance.history.empty?
49
+ @instance.call(new_environment)
50
+ assert @instance.actions.empty?
51
+ assert !@instance.history.empty?
52
+ end
53
+
54
+ should "call filtered actions" do
55
+
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: catalyst
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Ari Lerner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-14 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: shoulda
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: mocha
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 1.0.0
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: jeweler
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 1.5.2
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rcov
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ type: :development
70
+ version_requirements: *id005
71
+ description: Quickly build a framework for providing a middleware-like structure
72
+ email: arilerner@mac.com
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files:
78
+ - LICENSE.txt
79
+ - README.rdoc
80
+ files:
81
+ - .document
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.rdoc
86
+ - Rakefile
87
+ - VERSION
88
+ - lib/catalyst.rb
89
+ - lib/catalyst/environment.rb
90
+ - lib/catalyst/runner.rb
91
+ - test/helper.rb
92
+ - test/test_catalyst.rb
93
+ - test/test_runner.rb
94
+ has_rdoc: true
95
+ homepage: http://github.com/auser/catalyst
96
+ licenses:
97
+ - MIT
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: "0"
115
+ requirements: []
116
+
117
+ rubyforge_project:
118
+ rubygems_version: 1.6.2
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Quickly build a framework for providing a middleware-like structure
122
+ test_files:
123
+ - test/helper.rb
124
+ - test/test_catalyst.rb
125
+ - test/test_runner.rb