primer 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +17 -0
- data/README.rdoc +74 -35
- data/example/README.rdoc +33 -23
- data/example/application.rb +2 -2
- data/example/console.rb +133 -0
- data/example/db/blog.sqlite3 +0 -0
- data/example/{models → db}/connection.rb +0 -0
- data/example/environment.rb +17 -4
- data/example/models/comment.rb +5 -0
- data/example/models/post.rb +5 -0
- data/example/script/setup_database.rb +8 -2
- data/example/views/comments.erb +6 -0
- data/example/views/layout.erb +1 -1
- data/example/views/show.erb +5 -1
- data/lib/primer/bus/amqp.rb +7 -5
- data/lib/primer/bus/amqp.rbc +1045 -0
- data/lib/primer/bus/memory.rbc +344 -0
- data/lib/primer/bus.rb +3 -2
- data/lib/primer/bus.rbc +872 -0
- data/lib/primer/cache/memory.rbc +1443 -0
- data/lib/primer/cache/redis.rb +2 -2
- data/lib/primer/cache/redis.rbc +1643 -0
- data/lib/primer/cache.rb +2 -16
- data/lib/primer/cache.rbc +1502 -0
- data/lib/primer/enabler.rbc +414 -0
- data/lib/primer/helpers.rbc +1438 -0
- data/lib/primer/lazyness.rb +63 -0
- data/lib/primer/lazyness.rbc +1442 -0
- data/lib/primer/real_time.rbc +1999 -0
- data/lib/primer/route_set.rb +1 -0
- data/lib/primer/route_set.rbc +1475 -0
- data/lib/primer/watcher/active_record_macros.rb +13 -3
- data/lib/primer/watcher/active_record_macros.rbc +1796 -0
- data/lib/primer/watcher/macros.rb +11 -14
- data/lib/primer/watcher/macros.rbc +1628 -0
- data/lib/primer/watcher.rb +2 -7
- data/lib/primer/watcher.rbc +1416 -0
- data/lib/primer/worker/active_record_agent.rb +30 -33
- data/lib/primer/worker/active_record_agent.rbc +2840 -0
- data/lib/primer/worker/changes_agent.rb +20 -0
- data/lib/primer/worker/changes_agent.rbc +578 -0
- data/lib/primer/worker.rb +20 -3
- data/lib/primer/worker.rbc +1254 -0
- data/lib/primer.rb +10 -2
- data/lib/primer.rbc +944 -0
- data/spec/db/test.sqlite3 +0 -0
- data/spec/models/artist.rb +3 -2
- data/spec/models/artist.rbc +288 -0
- data/spec/models/blog_post.rb +3 -1
- data/spec/models/blog_post.rbc +181 -0
- data/spec/models/calendar.rbc +209 -0
- data/spec/models/concert.rbc +211 -0
- data/spec/models/performance.rbc +177 -0
- data/spec/models/person.rb +1 -0
- data/spec/models/person.rbc +306 -0
- data/spec/models/watchable.rbc +363 -0
- data/spec/primer/bus_spec.rbc +940 -0
- data/spec/primer/cache_spec.rb +3 -3
- data/spec/primer/cache_spec.rbc +8535 -0
- data/spec/primer/helpers/erb_spec.rb +14 -14
- data/spec/primer/helpers/erb_spec.rbc +2485 -0
- data/spec/primer/lazyness_spec.rb +61 -0
- data/spec/primer/lazyness_spec.rbc +1408 -0
- data/spec/primer/watcher/active_record_spec.rb +15 -15
- data/spec/primer/watcher/active_record_spec.rbc +5202 -0
- data/spec/primer/watcher_spec.rbc +2645 -0
- data/spec/schema.rbc +775 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/spec_helper.rbc +1193 -0
- data/spec/templates/page.erb +0 -1
- metadata +77 -70
- data/example/models/blog_post.rb +0 -4
@@ -0,0 +1,63 @@
|
|
1
|
+
module Primer
|
2
|
+
module Lazyness
|
3
|
+
|
4
|
+
def self.included(klass)
|
5
|
+
klass.extend(Macros)
|
6
|
+
if defined?(ActiveRecord) and klass < ActiveRecord::Base
|
7
|
+
klass.lazy_patch(:find, :all, :first, :last, :method_missing)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Macros
|
12
|
+
def lazy_patch(*method_names)
|
13
|
+
method_names.each do |method_name|
|
14
|
+
instance_eval <<-RUBY
|
15
|
+
alias :eager_#{method_name} :#{method_name}
|
16
|
+
def #{method_name}(*args, &block)
|
17
|
+
Primer::Lazyness::Proxy.new(self, primary_key, :eager_#{method_name}, args, block)
|
18
|
+
end
|
19
|
+
RUBY
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Proxy
|
25
|
+
methods = instance_methods.map { |m| m.to_s } - %w[object_id __id__ __send__]
|
26
|
+
methods.each { |m| undef_method m }
|
27
|
+
|
28
|
+
def initialize(real_class, primary_key, load_method, arguments, block)
|
29
|
+
@real_class = real_class
|
30
|
+
@primary_key = primary_key
|
31
|
+
@load_method = load_method
|
32
|
+
@arguments = arguments
|
33
|
+
@block = block
|
34
|
+
end
|
35
|
+
|
36
|
+
def method_missing(method_name, *args, &block)
|
37
|
+
if @load_method.to_s == 'eager_method_missing'
|
38
|
+
if method_name.to_s == @arguments.first.to_s.gsub(/^find_by_/, '')
|
39
|
+
Watcher.log(self, method_name.to_sym, args, block, @arguments[1])
|
40
|
+
return @arguments[1]
|
41
|
+
else
|
42
|
+
return __subject__.__send__(method_name, *args, &block)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
if method_name.to_s == @primary_key.to_s
|
47
|
+
Watcher.log(self, method_name.to_sym, args, block, @arguments.first)
|
48
|
+
return @arguments.first
|
49
|
+
end
|
50
|
+
|
51
|
+
__subject__.__send__(method_name, *args, &block)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def __subject__
|
57
|
+
@__subject__ ||= @real_class.__send__(@load_method, *@arguments, &@block)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|