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
0 commit comments