bradgessler-app-slice 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 2
4
+ :patch: 0
@@ -0,0 +1,61 @@
1
+ module AppSlice
2
+ module Middleware
3
+ class Static
4
+ FILE_METHODS = %w(GET HEAD).freeze unless defined?(FILE_METHODS)
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ @file_servers = [
9
+ ::Rack::File.new(app_slice_public_path),
10
+ ::Rack::File.new(rails_public_path)
11
+ ]
12
+ end
13
+
14
+ def call(env)
15
+ path = env['PATH_INFO'].chomp('/')
16
+ method = env['REQUEST_METHOD']
17
+
18
+ if FILE_METHODS.include?(method)
19
+ if rack_file = file_exist?(path)
20
+ return rack_file.call(env)
21
+ else
22
+ cached_path = directory_exist?(path) ? "#{path}/index" : path
23
+ cached_path += ::ActionController::Base.page_cache_extension
24
+
25
+ if rack_file = file_exist?(cached_path)
26
+ env['PATH_INFO'] = cached_path
27
+ return rack_file.call(env)
28
+ end
29
+ end
30
+ end
31
+
32
+ @app.call(env)
33
+ end
34
+
35
+ private
36
+ def rails_public_path
37
+ File.join(RAILS_ROOT, 'public')
38
+ end
39
+
40
+ def app_slice_public_path
41
+ File.join(RAILS_ROOT, 'apps', AppSlice.app, 'public')
42
+ end
43
+
44
+ # TODO [BG Mar 09] This might have to be optimized instead of a stupid check
45
+ # one, check the other arrangement
46
+ def file_exist?(path)
47
+ @file_servers.find do |file_server|
48
+ full_path = File.join(file_server.root, ::Rack::Utils.unescape(path))
49
+ File.file?(full_path) && File.readable?(full_path)
50
+ end
51
+ end
52
+
53
+ def directory_exist?(path)
54
+ @file_servers.find do |file_server|
55
+ full_path = File.join(file_server.root, ::Rack::Utils.unescape(path))
56
+ File.directory?(full_path) && File.readable?(full_path)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require File.join(File.dirname(__FILE__), '../app_slice')
3
+ require File.join(File.dirname(__FILE__), 'middleware/static')
4
+
5
+ module AppSlice
6
+ module Middleware
7
+ end
8
+ end
@@ -0,0 +1,47 @@
1
+ module AppSlice
2
+ module Rails
3
+ module Initializer
4
+ def self.included(base)
5
+ # Just skip this whole thing if a app package is not specified and
6
+ # load rails as you normally would without app packages
7
+ base.send(:extend, ClassMethods)
8
+ base.send(:include, InstanceMethods)
9
+
10
+ base.instance_eval do # Class methods
11
+ alias :run_without_app_package :run
12
+ alias :run :run_with_app_package
13
+ end
14
+
15
+ base.class_eval do # Instance methods
16
+ alias :load_environment_without_app_package :load_environment
17
+ alias :load_environment :load_environment_with_app_package
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ def load_environment_with_app_package(*args)
23
+ load_environment_without_app_package(*args)
24
+ # This is copy, pasted, and modified to load app package configs from
25
+ # http://github.com/rails/rails/blob/39ff550fa88da9a22d8c21ca872f5e4d0d83f8d4/railties/lib/initializer.rb#L365-369
26
+ config = self.send(:configuration) # Gotta tee up config for the bindings below in the evals
27
+ silence_warnings do
28
+ # If the env files don't exist, who cares? Just use the base configs
29
+ eval(IO.read(AppSlice::Rails.root_environment_path), binding, AppSlice::Rails.root_environment_path) if File.exists? AppSlice::Rails.root_environment_path
30
+ eval(IO.read(AppSlice::Rails.environment_path), binding, AppSlice::Rails.environment_path) if File.exists? AppSlice::Rails.environment_path
31
+ end
32
+ end
33
+ end
34
+
35
+ module ClassMethods
36
+ # TODO [BG Mar 09] Watch Rails::Initializer on github to monitor changes and pontential breakage
37
+ # of function arity in future versions of rails.
38
+ def run_with_app_package(command = :process, configuration=::Rails::Configuration.new, &block)
39
+ run_without_app_package(command, AppSlice::Rails.configure(configuration), &block)
40
+ AppSlice::Rails.init!
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ Rails::Initializer.send(:include, AppSlice::Rails::Initializer) if AppSlice.exists?
@@ -0,0 +1,57 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. app_slice])
2
+ require File.join(File.dirname(__FILE__), %w[rails initializer])
3
+
4
+ module AppSlice
5
+ module Rails
6
+ class << self
7
+ def init!
8
+ ::Rails.public_path = File.join(root, 'public')
9
+ ::ActionController::Routing::Routes.add_configuration_file File.join(config_path, 'routes.rb')
10
+ # Update frameworks with view_paths
11
+ [::ActionController, ::ActionMailer].each do |framework|
12
+ framework::Base.send(:view_paths).unshift(view_path)
13
+ end
14
+ @initialized = true
15
+ end
16
+
17
+ def root
18
+ File.join(RAILS_ROOT, 'apps', AppSlice.app)
19
+ end
20
+
21
+ def environment_path
22
+ File.join(config_path, 'environments', "#{RAILS_ENV}.rb")
23
+ end
24
+
25
+ def root_environment_path
26
+ File.join(config_path, 'environment.rb')
27
+ end
28
+
29
+ def config_path
30
+ File.join(root, 'config')
31
+ end
32
+
33
+ def view_path
34
+ File.join(root, %w[app views])
35
+ end
36
+
37
+ def configure(configuration = ::Rails::Configuration.new)
38
+ # Create our new configuration block here
39
+ configuration.database_configuration_file = database_configuration_file
40
+ return configuration
41
+ end
42
+
43
+ def initialized?
44
+ @initialized
45
+ end
46
+
47
+ protected
48
+ def routes_configuration_file
49
+ File.join(config_path, 'routes.rb')
50
+ end
51
+
52
+ def database_configuration_file
53
+ File.join(config_path, 'database.yml')
54
+ end
55
+ end
56
+ end
57
+ end
data/lib/app_slice.rb ADDED
@@ -0,0 +1,18 @@
1
+ APP_SLICE = ENV['APP_SLICE'] unless defined?(APP_SLICE)
2
+
3
+ module AppSlice
4
+ class << self
5
+ attr_accessor :app
6
+
7
+ def exists?
8
+ !@app.nil? and @app != ''
9
+ end
10
+
11
+ # Binds the AppSlice to the environmental variable APP_SLICE
12
+ def init!
13
+ @app = APP_SLICE || ''
14
+ end
15
+ end
16
+ end
17
+
18
+ AppSlice.init!
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ # Loads various ruby files and application stacks for testing purposes
5
+ module AppSlice
6
+ module TestHelpers
7
+ class << self
8
+ def require_app_slice!
9
+ require File.join(File.dirname(__FILE__), %w[.. lib app_slice])
10
+ end
11
+
12
+ def require_middleware!
13
+ require 'rack'
14
+ require File.join(File.dirname(__FILE__), %w[.. lib app_slice middleware])
15
+ end
16
+
17
+ def require_rails!
18
+ # Load whatever is needed to test app slice integration with rails
19
+ # require 'rails'
20
+ require File.join(File.dirname(__FILE__), %w[.. lib app_slice rails])
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ AppSlice::TestHelpers.require_app_slice!
4
+
5
+ describe AppSlice do
6
+ it "should have init!" do
7
+ AppSlice.init!
8
+ end
9
+
10
+ it "should initialize with APP_SLICE value" do
11
+ APP_SLICE = 'foo.com'
12
+ AppSlice.init!
13
+ AppSlice.app.should eql(APP_SLICE)
14
+ end
15
+
16
+ it "should have name" do
17
+ AppSlice.should respond_to(:name)
18
+ end
19
+
20
+ it "should have app" do
21
+ AppSlice.should respond_to(:app)
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ AppSlice::TestHelpers::require_middleware!
4
+
5
+ describe AppSlice::Middleware::Static do
6
+
7
+ end
@@ -0,0 +1,15 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ AppSlice::TestHelpers::require_rails!
4
+
5
+ # describe Initiailizer do
6
+ # it "should initialize without APP_SLICE"
7
+ #
8
+ # it "should initialize with APP_SLICE"
9
+ #
10
+ # it %{should override RAILS_ROOT/config/environments.rb
11
+ # with RAILS_ROOT/apps/APP_SLICE/config/environments.rb}
12
+ #
13
+ # it %{should override RAILS_ROOT/config/environments/RAILS_ENV.rb
14
+ # with RAILS_ROOT/apps/APP_SLICE/config/environments/RAILS_ENV.rb}
15
+ # end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bradgessler-app-slice
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Brad Gessler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-01 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Partition rails apps by apps
17
+ email: brad@conden.se
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/app_slice
27
+ - lib/app_slice/middleware
28
+ - lib/app_slice/middleware/static.rb
29
+ - lib/app_slice/middleware.rb
30
+ - lib/app_slice/rails
31
+ - lib/app_slice/rails/initializer.rb
32
+ - lib/app_slice/rails.rb
33
+ - lib/app_slice.rb
34
+ - spec/fixtures
35
+ - spec/spec_helper.rb
36
+ - spec/specs
37
+ - spec/specs/app_slice_spec.rb
38
+ - spec/specs/middleware_spec.rb
39
+ - spec/specs/rails_spec.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/bradgessler/app-slice
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --inline-source
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: App slice is a way to partition a rails application into various apps. This was built for a project where we needed to build private label websites for one of our rails applications.
67
+ test_files: []
68
+