Skip to content

Commit 951c1eb

Browse files
committed
Pre-github
0 parents  commit 951c1eb

40 files changed

+1721
-0
lines changed

README

Whitespace-only changes.

Rakefile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
begin
2+
require 'spec'
3+
rescue LoadError
4+
require 'rubygems'
5+
require 'spec'
6+
end
7+
8+
require 'spec/rake/spectask'
9+
10+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../'
11+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/lib'
12+
13+
require 'scrooge'
14+
15+
desc "Run the specs under spec"
16+
Spec::Rake::SpecTask.new do |t|
17+
t.spec_files = FileList['spec/**/*_spec.rb']
18+
t.spec_opts << "-c"
19+
end

init.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'scrooge'

lib/scrooge.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
$:.unshift(File.dirname(__FILE__))
2+
3+
require 'scrooge/core/string'
4+
require 'scrooge/core/symbol'
5+
6+
module Scrooge
7+
class Base
8+
9+
class << self
10+
11+
def profile
12+
@@profile ||= Scrooge::Profile.new
13+
end
14+
15+
def profile=( profile )
16+
@@profile = profile
17+
end
18+
19+
end
20+
21+
def profile
22+
self.class.profile
23+
end
24+
25+
end
26+
27+
module Middleware
28+
autoload :Tracker, 'scrooge/middleware/tracker'
29+
end
30+
31+
end
32+
33+
require 'scrooge/profile'
34+
require 'scrooge/storage/base'
35+
require 'scrooge/orm/base'
36+
require 'scrooge/framework/base'
37+
require 'scrooge/tracker/base'

lib/scrooge/core/string.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Scrooge
2+
module Core
3+
module String
4+
5+
# Thx ActiveSupport
6+
def to_const
7+
self.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
8+
end
9+
10+
end
11+
end
12+
end
13+
14+
class String
15+
include Scrooge::Core::String
16+
end

lib/scrooge/core/symbol.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Scrooge
2+
module Core
3+
module Symbol
4+
5+
def to_const
6+
to_s.to_const
7+
end
8+
9+
end
10+
end
11+
end
12+
13+
class Symbol
14+
include Scrooge::Core::Symbol
15+
end

lib/scrooge/framework/base.rb

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
module Scrooge
2+
module Framework
3+
4+
# Scrooge is framework agnostic and attempts to abstract the following :
5+
#
6+
# * current environment
7+
# * app root dir
8+
# * app tmp dir
9+
# * app config dir
10+
# * logging
11+
# * resource endpoints
12+
# * caching
13+
# * injecting Rack MiddleWare
14+
#
15+
# Framework Signatures
16+
#
17+
# Scrooge will attempt to determine the current active framework it's deployed with
18+
# through various framework specific hooks.
19+
#
20+
# module Scrooge
21+
# module Framework
22+
# module YetAnother < Base
23+
# ...
24+
# signature do
25+
# Object.const_defined?( "UnlikeAnyOther" )
26+
# end
27+
# ...
28+
# end
29+
# end
30+
# end
31+
32+
autoload :Rails, 'scrooge/framework/rails'
33+
34+
class Base < Scrooge::Base
35+
class NotImplemented < StandardError
36+
end
37+
38+
class NoSupportedFrameworks < StandardError
39+
end
40+
41+
class << self
42+
43+
@@signatures = {}
44+
@@signatures[self.name] = Hash.new( [] )
45+
@@frameworks = []
46+
47+
def signature( &block )
48+
@@signatures[self.name] = signatures << block
49+
end
50+
51+
def signatures
52+
@@signatures[self.name] || []
53+
end
54+
55+
def frameworks
56+
@@frameworks
57+
end
58+
59+
def which_framework?
60+
iterate_frameworks() || raise( NoSupportedFrameworks )
61+
end
62+
63+
def instantiate
64+
Object.module_eval("::#{which_framework?()}", __FILE__, __LINE__).new
65+
end
66+
67+
private
68+
69+
def inherited( subclass ) #:nodoc:
70+
@@frameworks << subclass
71+
end
72+
73+
def iterate_frameworks #:nodoc:
74+
frameworks.detect do |framework|
75+
framework.signatures.all?{|sig| sig.call }
76+
end
77+
end
78+
79+
end
80+
81+
def environment
82+
raise NotImplemented
83+
end
84+
85+
def root
86+
raise NotImplemented
87+
end
88+
89+
def tmp
90+
raise NotImplemented
91+
end
92+
93+
def config
94+
raise NotImplemented
95+
end
96+
97+
def logger
98+
raise NotImplemented
99+
end
100+
101+
def resource( app )
102+
raise NotImplemented
103+
end
104+
105+
def write_cache( key, value )
106+
raise NotImplemented
107+
end
108+
109+
def read_cache( key )
110+
raise NotImplemented
111+
end
112+
113+
def middleware( &block )
114+
raise NotImplemented
115+
end
116+
117+
def install_scope_middleware( tracker )
118+
raise NotImplemented
119+
end
120+
121+
def install_tracking_middleware
122+
raise NotImplemented
123+
end
124+
125+
def scopes
126+
ensure_scopes_path do
127+
Dir.entries( scopes_path ).grep(/\d{10}/)
128+
end
129+
end
130+
131+
def scopes_path
132+
@profiles_path ||= File.join( config, 'scrooge', 'scopes' )
133+
end
134+
135+
def scope_path( scope )
136+
File.join( scopes_path, scope.to_s )
137+
end
138+
139+
private
140+
141+
def ensure_scopes_path #:nodoc:
142+
FileUtils.makedirs( scopes_path ) unless File.exist?( scopes_path )
143+
yield if block_given?
144+
end
145+
146+
end
147+
end
148+
end

lib/scrooge/framework/rails.rb

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
module Scrooge
2+
module Framework
3+
class Rails < Base
4+
5+
signature do
6+
defined?(RAILS_ROOT)
7+
end
8+
9+
signature do
10+
Object.const_defined?( "ActiveSupport" )
11+
end
12+
13+
signature do
14+
Object.const_defined?( "ActionController" )
15+
end
16+
17+
def environment
18+
::Rails.env
19+
end
20+
21+
def root
22+
::Rails.root
23+
end
24+
25+
def tmp
26+
File.join( ::Rails.root, 'tmp' )
27+
end
28+
29+
def config
30+
File.join( ::Rails.root, 'config' )
31+
end
32+
33+
def logger
34+
::Rails.logger
35+
end
36+
37+
def resource( app )
38+
Scrooge::Tracker::Resource.new do |resource|
39+
resource.controller = app.request.path_parameters['controller']
40+
resource.action = app.request.path_parameters['action']
41+
resource.method = app.request.method
42+
resource.format = app.request.format
43+
end
44+
end
45+
46+
def read_cache( key )
47+
::Rails.cache.read( key )
48+
end
49+
50+
def write_cache( key, value )
51+
::Rails.cache.write( key, value )
52+
end
53+
54+
def middleware( &block )
55+
::ActionController::Dispatcher.middleware.instance_eval do
56+
block.call
57+
end
58+
end
59+
60+
def install_scope_middleware( tracker )
61+
tracker.resources.each do |resource|
62+
tracker.middleware.each do |resource_middleware|
63+
middleware do
64+
use resource_middleware
65+
end
66+
end
67+
end
68+
end
69+
70+
end
71+
end
72+
end

lib/scrooge/middleware/tracker.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Scrooge
2+
module Middleware
3+
class Tracker < Scrooge::Base
4+
5+
def initialize(app, options = {})
6+
@app = app
7+
end
8+
9+
def call(env)
10+
Scrooge::Profile.tracker.track( resource ) do
11+
@app.call(env)
12+
end
13+
end
14+
15+
private
16+
17+
def resource
18+
Scrooge::Profile.framework.resource( @app )
19+
end
20+
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)