goat 0.3.32 → 0.3.33

Sign up to get free protection for your applications and to get access to all the features.
data/goat.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  spec = Gem::Specification.new do |s|
2
2
  s.name = 'goat'
3
- s.version = '0.3.32'
3
+ s.version = '0.3.33'
4
4
  s.summary = 'Pre-release beta version of Goat'
5
5
  s.author = 'Patrick Collison'
6
6
  s.email = 'patrick@collison.ie'
data/lib/goat/mongo.rb CHANGED
@@ -26,3 +26,29 @@ module Mongo
26
26
  end
27
27
  end
28
28
  end
29
+
30
+ module MongoComponent
31
+ def self.db_notifications; @db_notifications ||= {}; end
32
+
33
+ def self.db_notification_subscribe(coll, depkey, &blk)
34
+ db_notifications[coll] = depkey
35
+
36
+ Goat::NotificationCenter.subscribe(self, :db_notification_recv,
37
+ 'type' => 'db_action',
38
+ 'collection' => coll
39
+ )
40
+ end
41
+
42
+ def self.db_notification_recv(notif)
43
+ if notif['obj'] && notif['collection']
44
+ depkey = db_notifications[notif['collection']]
45
+ if depkey && (dep = notif['obj'][depkey])
46
+ rerender_and_update([dep])
47
+ end
48
+ else
49
+ raise "Invalid notification: #{notif.inspect}"
50
+ end
51
+ end
52
+ end
53
+
54
+
@@ -28,16 +28,20 @@ module Goat
28
28
  Receiver.start(@host, @recv_port)
29
29
  end
30
30
 
31
- def self.enabled?; @configured; end
32
-
33
31
  def self.configure(opts={})
34
- opts = {:host => '127.0.0.1', :recv_port => 8000, :send_port => 8001}.merge(opts)
35
- @configured = true
36
32
  @host = opts[:host]
37
33
  @recv_port = opts[:recv_port]
38
34
  @send_port = opts[:send_port]
35
+ end
39
36
 
40
- EM.next_tick { self.start_receiver }
37
+ def self.set_defaults
38
+ @host ||= '127.0.0.1'
39
+ @recv_port ||= 8000
40
+ @send_port ||= 8001
41
+ end
42
+
43
+ def self.schedule
44
+ EM.next_tick { set_defaults; self.start_receiver }
41
45
  end
42
46
 
43
47
  def self.subscribers; @subscribers ||= Set.new; end
@@ -92,6 +96,8 @@ module Goat
92
96
  end
93
97
 
94
98
  def self.notify(notif)
99
+ set_defaults # maybe #schedule or #configure never got called
100
+
95
101
  if Dynamic.variable?(:txn)
96
102
  notif['txn'] = Dynamic[:txn]
97
103
  notif['txn_pgid'] = Dynamic[:txn_pgid]
@@ -1,9 +1,17 @@
1
1
  module Goat
2
2
  module StateSrvClient
3
3
  def self.configure(opts={})
4
- # TODO implement fully
5
- EM.next_tick { StateSrvConnection.connect(opts[:host] || '127.0.0.1',
6
- opts[:port] || 8011) }
4
+ @host = opts[:host]
5
+ @port = opts[:port]
6
+ end
7
+
8
+ def self.schedule
9
+ EM.next_tick {
10
+ StateSrvConnection.connect(
11
+ @host || '127.0.0.1',
12
+ @port || 8011
13
+ )
14
+ }
7
15
  end
8
16
 
9
17
  def self.send_message(type, m, sync=false)
data/lib/goat.rb CHANGED
@@ -69,67 +69,77 @@ module Goat
69
69
 
70
70
  def self.goat_path(f); File.join(File.dirname(__FILE__), 'goat', f); end
71
71
 
72
- def self.extend_mongo
73
- require goat_path('mongo')
74
- end
72
+ class Config
73
+ def initialize
74
+ @config = {}
75
+ end
75
76
 
76
- def self.enable_notifications(opts={})
77
- NotificationCenter.configure(opts)
78
- end
77
+ def enable_notifications(opts={})
78
+ NotificationCenter.configure(opts)
79
+ end
79
80
 
80
- def self.enable_statesrv(opts={})
81
- StateSrvClient.configure(opts)
82
- end
81
+ def enable_statesrv(opts={})
82
+ StateSrvClient.configure(opts)
83
+ end
83
84
 
84
- def self.enable_appsrv(opts={})
85
- UpdateDispatcher.enable
86
- end
85
+ def enable_mongo(opts={})
86
+ require Goat.goat_path('mongo')
87
+ end
87
88
 
88
- def self.load_all(dir_fragment)
89
- dir = File.join(Goat.setting!(:root), dir_fragment)
90
- if File.directory?(dir)
91
- Dir.entries(dir).select{|f| f =~ /\.rb$/}.each {|f| require(File.join(dir, f))}
89
+ def get!(opt)
90
+ if @config.include?(opt)
91
+ @config[opt]
92
+ else
93
+ raise "#{opt} not set"
94
+ end
92
95
  end
93
- end
94
96
 
95
- @settings = {}
96
- def self.setting(opt)
97
- @settings[opt]
98
- end
97
+ def add_component_helpers(modul)
98
+ Goat::Component.send(:include, modul)
99
+ end
99
100
 
100
- def self.setting!(opt)
101
- if @settings.include?(opt)
102
- @settings[opt]
103
- else
104
- raise "#{opt} not set"
101
+ def [](k); @config[k]; end
102
+ def []=(k, v)
103
+ @config[k] = v
104
+
105
+ meth = "enable_#{k}".to_sym
106
+ if self.respond_to?(meth)
107
+ self.send(meth, v)
108
+ end
105
109
  end
106
110
  end
107
111
 
108
- def self.settings; @settings; end
109
-
110
- def self.add_component_helpers(modul)
111
- Goat::Component.send(:include, modul)
112
+ def self.load_all(dir_fragment)
113
+ dir = File.join(Goat.config.get!(:root), dir_fragment)
114
+ if File.directory?(dir)
115
+ Dir.entries(dir).select{|f| f =~ /\.rb$/}.each {|f| require(File.join(dir, f))}
116
+ end
112
117
  end
113
118
 
119
+ def self.config; @config; end
120
+
114
121
  def self.configure(&blk)
115
- blk.call
122
+ @config = Config.new
123
+
124
+ blk.call(@config)
116
125
 
117
126
  load_all('components')
118
127
  load_all('pages')
119
128
 
120
- Goat.extend_mongo if Goat.setting(:mongo)
121
-
122
- if p = Goat.setting(:press)
123
- Goat::Static.press = p
124
- end
129
+ # disabled for now
130
+ #if p = Goat.setting(:press)
131
+ # Goat::Static.press = p
132
+ #end
125
133
 
126
- if Goat.setting(:debug)
134
+ if @config[:debug]
127
135
  if defined?(Thin)
128
136
  Thin::Logging.debug = true
129
137
  end
130
138
  end
131
139
 
132
- NotificationCenter.configure(Goat.setting(:notifications)) if Goat.setting(:notifications)
140
+ Goat::StateSrvClient.schedule
141
+ Goat::NotificationCenter.schedule
142
+ UpdateDispatcher.enable
133
143
  end
134
144
 
135
145
  def self.rack_builder(app, opts={})
@@ -137,13 +147,13 @@ module Goat
137
147
  opts = defaults.merge(opts)
138
148
 
139
149
  Rack::Builder.new do
140
- if cookies = Goat.setting(:cookies)
150
+ if cookies = Goat.config[:cookies]
141
151
  use Rack::Session::Cookie, cookies
142
152
  end
143
153
 
144
154
  use opts[:logger]
145
155
 
146
- if static = Goat.setting(:static)
156
+ if static = Goat.config[:static]
147
157
  use Rack::Static, :urls => static.fetch(:urls), :root => static.fetch(:root)
148
158
  end
149
159
 
@@ -153,6 +163,11 @@ module Goat
153
163
  end
154
164
  end
155
165
 
166
+ def self.run_app(app, opts={})
167
+ builder = rack_builder(app, opts)
168
+ Rack::Handler::Thin.run(builder.to_app)
169
+ end
170
+
156
171
  class NotFoundError < RuntimeError
157
172
  attr_reader :path
158
173
 
@@ -437,10 +452,10 @@ module Goat
437
452
  if name =~ /\.erb$/ # allow an absolute path to be passed
438
453
  erbf = name
439
454
  else
440
- erbf = File.join(Goat.setting!(:root), 'views', "#{name}.erb")
455
+ erbf = File.join(Goat.config.get!(:root), 'views', "#{name}.erb")
441
456
  end
442
457
 
443
- layf = File.join(Goat.setting!(:root), 'views', 'layout.erb')
458
+ layf = File.join(Goat.config.get!(:root), 'views', 'layout.erb')
444
459
  template = Tilt[:erb].new(erbf) { File.read(erbf) }
445
460
 
446
461
  layout = File.read(layf) if File.exists?(layf) && !partial && use_layout
@@ -533,10 +548,6 @@ module Goat
533
548
  around_handlers << [:after, blk]
534
549
  end
535
550
 
536
- def enable_notifications(opts={})
537
- NotificationCenter.configure(opts)
538
- end
539
-
540
551
  def respond_success; [200, {}, ['ok']]; end
541
552
  def respond_failed; [500, {}, ['failed']]; end
542
553
 
@@ -721,7 +732,7 @@ module Goat
721
732
 
722
733
  def unpressed_component_classes
723
734
  ordered_component_classes.to_a.reject do |cls|
724
- Goat.setting(:press) && Goat::Static.pressed?(cls)
735
+ Goat.config[:press] && Goat::Static.pressed?(cls)
725
736
  end
726
737
  end
727
738
 
@@ -759,6 +770,9 @@ module Goat
759
770
 
760
771
  def self.handle_request(app)
761
772
  pg = self.new(app)
773
+
774
+ raise "Page #{pg} has no id: did you forget to call super()?" unless pg.id
775
+
762
776
  pg.response
763
777
  end
764
778
 
@@ -1283,8 +1297,9 @@ module Goat
1283
1297
  def clientside_args; []; end
1284
1298
 
1285
1299
  def clientside_instance
1286
- args = [id, @parent ? @parent.id : nil, clientside_args].to_json[1..-2]
1287
- "(new #{self.class.name}('#{self.class.name}', #{args}))"
1300
+ args = [id, @parent ? @parent.id : nil, clientside_args]
1301
+ argjson = args.to_json[1..-2] # strip the braces
1302
+ "(new #{self.class.name}('#{self.class.name}', #{argjson}))"
1288
1303
  end
1289
1304
 
1290
1305
  def self.rpc(name, opts={}, &blk)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goat
3
3
  version: !ruby/object:Gem::Version
4
- hash: 83
4
+ hash: 81
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 32
10
- version: 0.3.32
9
+ - 33
10
+ version: 0.3.33
11
11
  platform: ruby
12
12
  authors:
13
13
  - Patrick Collison
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-07 00:00:00 +00:00
18
+ date: 2011-01-18 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies: []
21
21