la_maquina 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/Gemfile +5 -0
- data/Gemfile.lock +20 -1
- data/README.md +7 -7
- data/lib/la_maquina/dependency_map/base.rb +19 -0
- data/lib/la_maquina/dependency_map/constant_map.rb +13 -0
- data/lib/la_maquina/dependency_map/yaml_map.rb +15 -0
- data/lib/la_maquina/dependency_map.rb +7 -0
- data/lib/la_maquina/engine.rb +39 -0
- data/lib/la_maquina/error_notifier/base.rb +1 -1
- data/lib/la_maquina/{volante.rb → notifier.rb} +10 -8
- data/lib/la_maquina/piston/base.rb +2 -2
- data/lib/la_maquina/piston/cache_piston.rb +18 -15
- data/lib/la_maquina/piston/sunspot_piston.rb +22 -0
- data/lib/la_maquina/piston.rb +3 -2
- data/lib/la_maquina/version.rb +1 -1
- data/lib/la_maquina.rb +3 -2
- data/test/dummy/app/models/admin.rb +7 -3
- data/test/dummy/app/models/admin_trait.rb +3 -3
- data/test/dummy/app/models/admin_trait_modifier.rb +3 -3
- data/test/dummy/app/models/guest.rb +2 -2
- data/test/dummy/app/models/guest_trait.rb +3 -3
- data/test/dummy/app/models/guest_trait_modifier.rb +2 -3
- data/test/dummy/app/models/property.rb +2 -3
- data/test/dummy/app/models/standalone.rb +2 -2
- data/test/dummy/config/database.yml +6 -4
- data/test/dummy/config/initializers/la_maquina.rb +8 -2
- data/test/dummy/config/la_maquina/dependency_maps/bad_map.yml +1 -0
- data/test/dummy/config/la_maquina/dependency_maps/sunspot_piston.yml +1 -0
- data/test/dummy/config/sunspot.yml +5 -0
- data/test/dummy/db/schema.rb +0 -1
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/lib/dummy_comm_object.rb +1 -1
- data/test/dummy/lib/exploding_comm_object.rb +1 -1
- data/test/dummy/lib/exploding_piston.rb +5 -0
- data/test/dummy/lib/extended_sunspot_piston.rb +13 -0
- data/test/dummy/lib/primitive_piston.rb +5 -0
- data/test/dummy/lib/test_notifier.rb +2 -2
- data/test/dummy/log/development.log +34 -0
- data/test/unit/dependency_maps/yamp_map_test.rb +26 -0
- data/test/unit/engine_test.rb +32 -0
- data/test/unit/error_notifier_test.rb +21 -0
- data/test/{la_maquina_test.rb → unit/la_maquina_test.rb} +12 -0
- data/test/unit/notifier_test.rb +123 -0
- data/test/unit/piston_test.rb +10 -0
- data/test/unit/pistons/cache_piston_test.rb +26 -0
- data/test/unit/pistons/sunspot_piston_test.rb +25 -0
- metadata +39 -12
- data/lib/la_maquina/ciguenal.rb +0 -27
- data/test/ciguenal_test.rb +0 -30
- data/test/dummy/lib/test_piston.rb +0 -5
- data/test/volante_test.rb +0 -136
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3e37589f3930b79eecc19e0743e9b6b1fcf1d9f
|
4
|
+
data.tar.gz: c332e847878318ab3db7c764f7f19738b41d2338
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22aad45ed90d3542aedb927e47399297c0369fd17fcf6b786958704a75a62c1382832f99525987c477856c24f595e95c1ee9ad2b2bbc6a094a4e70c0ff0885ab
|
7
|
+
data.tar.gz: ae9ddc8d32213ca73d940d3eea5abb1dce9e984f4423be28bf4750d432c0d4a03d53e5b4db7f8f9bc6b5b5adfce687e44873dba279735036b7b883a872fa54a0
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -18,5 +18,10 @@ gem "rails", rails
|
|
18
18
|
gem "redis"
|
19
19
|
gem "redis-namespace"
|
20
20
|
|
21
|
+
# solr
|
22
|
+
gem 'sunspot_rails'
|
23
|
+
gem 'sunspot_solr' # optional pre-packaged Solr distribution for use in development
|
24
|
+
gem 'progress_bar'
|
25
|
+
|
21
26
|
gem 'test_after_commit', :group => :test
|
22
27
|
gem 'simplecov', :group => :test
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
la_maquina (0.0.
|
4
|
+
la_maquina (0.0.3)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -47,6 +47,7 @@ GEM
|
|
47
47
|
erubis (2.7.0)
|
48
48
|
globalid (0.3.3)
|
49
49
|
activesupport (>= 4.1.0)
|
50
|
+
highline (1.6.21)
|
50
51
|
hike (1.2.3)
|
51
52
|
i18n (0.7.0)
|
52
53
|
json (1.8.2)
|
@@ -60,6 +61,11 @@ GEM
|
|
60
61
|
multi_json (1.10.1)
|
61
62
|
nokogiri (1.6.6.2)
|
62
63
|
mini_portile (~> 0.6.0)
|
64
|
+
options (2.3.2)
|
65
|
+
pr_geohash (1.0.0)
|
66
|
+
progress_bar (1.0.3)
|
67
|
+
highline (~> 1.6.1)
|
68
|
+
options (~> 2.3.0)
|
63
69
|
rack (1.6.0)
|
64
70
|
rack-test (0.6.3)
|
65
71
|
rack (>= 1.0)
|
@@ -91,6 +97,8 @@ GEM
|
|
91
97
|
redis (3.2.1)
|
92
98
|
redis-namespace (1.5.1)
|
93
99
|
redis (~> 3.0, >= 3.0.4)
|
100
|
+
rsolr (1.0.12)
|
101
|
+
builder (>= 2.1.2)
|
94
102
|
simplecov (0.9.1)
|
95
103
|
docile (~> 1.1.0)
|
96
104
|
multi_json (~> 1.0)
|
@@ -106,6 +114,14 @@ GEM
|
|
106
114
|
activesupport (>= 3.0)
|
107
115
|
sprockets (>= 2.8, < 4.0)
|
108
116
|
sqlite3 (1.3.10)
|
117
|
+
sunspot (2.2.0)
|
118
|
+
pr_geohash (~> 1.0)
|
119
|
+
rsolr (~> 1.0.7)
|
120
|
+
sunspot_rails (2.2.0)
|
121
|
+
nokogiri
|
122
|
+
rails (>= 3)
|
123
|
+
sunspot (= 2.2.0)
|
124
|
+
sunspot_solr (2.2.0)
|
109
125
|
test_after_commit (0.4.0)
|
110
126
|
activerecord (>= 3.2)
|
111
127
|
thor (0.19.1)
|
@@ -120,10 +136,13 @@ PLATFORMS
|
|
120
136
|
DEPENDENCIES
|
121
137
|
bundler (~> 1.6)
|
122
138
|
la_maquina!
|
139
|
+
progress_bar
|
123
140
|
rails (>= 4.2.0)
|
124
141
|
rake
|
125
142
|
redis
|
126
143
|
redis-namespace
|
127
144
|
simplecov
|
128
145
|
sqlite3
|
146
|
+
sunspot_rails
|
147
|
+
sunspot_solr
|
129
148
|
test_after_commit
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# La Maquina
|
2
2
|
|
3
|
-
|
3
|
+
Model graph traversal
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -22,7 +22,7 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
### Pistons
|
24
24
|
|
25
|
-
Once the `
|
25
|
+
Once the `Engine` is called it fires the `Piston`s, the behavior of which is entirely up to you.
|
26
26
|
|
27
27
|
So, let's say, we have a couple objects that look like:
|
28
28
|
|
@@ -41,8 +41,8 @@ end
|
|
41
41
|
class Machete < ActiveRecord::Base
|
42
42
|
belongs_to :danny_trejo
|
43
43
|
|
44
|
-
include LaMaquina::
|
45
|
-
|
44
|
+
include LaMaquina::Notifier
|
45
|
+
notifies_about :danny_trejo
|
46
46
|
end
|
47
47
|
```
|
48
48
|
and we want to let `DannyTrejo` know when his `Machete` has been updated so that we can reindex him.
|
@@ -52,8 +52,8 @@ and we want to let `DannyTrejo` know when his `Machete` has been updated so that
|
|
52
52
|
```ruby
|
53
53
|
class SunspotPiston < LaMaquina::Piston::Base
|
54
54
|
class << self
|
55
|
-
def fire!(
|
56
|
-
indexed_class.find(id).index!
|
55
|
+
def fire!( notified_class, id, notifier_class = "" )
|
56
|
+
indexed_class(notified_class).find(id).index!
|
57
57
|
end
|
58
58
|
|
59
59
|
private
|
@@ -76,7 +76,7 @@ For example, if you're using the CachePiston and need to set up Redis, here's ho
|
|
76
76
|
|
77
77
|
```ruby
|
78
78
|
LaMaquina::Piston::CachePiston.redis = Redis::Namespace.new(:cache_piston, redis: Redis.new)
|
79
|
-
LaMaquina::
|
79
|
+
LaMaquina::Engine.install LaMaquina::Piston::CachePiston, TestPiston
|
80
80
|
LaMaquina.error_notifier = LaMaquina::ErrorNotifier::HoneybadgerNotifier
|
81
81
|
```
|
82
82
|
#### ErrorNotifier
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module LaMaquina
|
2
|
+
module DependencyMap
|
3
|
+
class Base
|
4
|
+
def initialize( path = nil )
|
5
|
+
if path
|
6
|
+
self.map = YAML.load_file(path).with_indifferent_access
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def mapping_for(*args)
|
11
|
+
raise "A dependency map has to implement 'mapping_for'"
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
|
16
|
+
attr_accessor :map
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module LaMaquina
|
2
|
+
module DependencyMap
|
3
|
+
class ConstantMap < LaMaquina::DependencyMap::Base
|
4
|
+
|
5
|
+
def mapping_for(notified_class, id)
|
6
|
+
notified_class.camelize.constantize
|
7
|
+
rescue => e
|
8
|
+
LaMaquina.error_notifier.notify( e,
|
9
|
+
missing_class: notified_class )
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module LaMaquina
|
2
|
+
module DependencyMap
|
3
|
+
class YamlMap < LaMaquina::DependencyMap::Base
|
4
|
+
def mapping_for(*args)
|
5
|
+
submap = map
|
6
|
+
args.each do |key|
|
7
|
+
submap = submap[key]
|
8
|
+
end
|
9
|
+
submap
|
10
|
+
rescue => e
|
11
|
+
LaMaquina.error_notifier.notify( e )
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module LaMaquina
|
2
|
+
class Engine
|
3
|
+
|
4
|
+
class << self
|
5
|
+
protected
|
6
|
+
|
7
|
+
attr_accessor :pistons
|
8
|
+
end
|
9
|
+
self.pistons = []
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
def notify!( notified_class, id, notifier_class = "" )
|
14
|
+
pistons.each do |piston|
|
15
|
+
begin
|
16
|
+
# We can't thread this because Rails executes this code before the commit somehow.
|
17
|
+
# What. yeah. I don't know either.
|
18
|
+
|
19
|
+
# Thread.new{
|
20
|
+
piston.fire! notified_class, id, notifier_class
|
21
|
+
# }.join
|
22
|
+
rescue => e
|
23
|
+
LaMaquina.error_notifier.notify( e,
|
24
|
+
misfiring_piston: piston,
|
25
|
+
notified_class: notified_class,
|
26
|
+
notified_id: id,
|
27
|
+
notifier_class: notifier_class )
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def install(*attrs)
|
33
|
+
attrs.each do |piston|
|
34
|
+
self.pistons << piston
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module LaMaquina
|
2
|
-
module
|
2
|
+
module Notifier
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
include ActiveRecord::Callbacks
|
5
5
|
|
@@ -13,7 +13,7 @@ module LaMaquina
|
|
13
13
|
|
14
14
|
|
15
15
|
class << self
|
16
|
-
def
|
16
|
+
def notifies_about(object, opts = {})
|
17
17
|
notified_objects << {:object => object, :options => opts}
|
18
18
|
end
|
19
19
|
end
|
@@ -32,18 +32,20 @@ module LaMaquina
|
|
32
32
|
klass = notified_klass( object, options )
|
33
33
|
id = notified_id( object, options )
|
34
34
|
|
35
|
+
notifier_class = LaMaquina.format_object_name(self)
|
36
|
+
|
35
37
|
begin
|
36
38
|
if comm_object
|
37
|
-
comm_object.notify(:notified_class => klass, :notified_id => id, :notifier_class =>
|
39
|
+
comm_object.notify(:notified_class => klass, :notified_id => id, :notifier_class => notifier_class)
|
38
40
|
else
|
39
|
-
LaMaquina::
|
41
|
+
LaMaquina::Engine.notify! klass, id, notifier_class
|
40
42
|
end
|
41
43
|
rescue => e
|
42
44
|
LaMaquina.error_notifier.notify( e,
|
43
|
-
:
|
44
|
-
:
|
45
|
-
:notifier_class
|
46
|
-
:
|
45
|
+
notified_class: klass,
|
46
|
+
notified_id: id,
|
47
|
+
notifier_class: notifier_class,
|
48
|
+
notifier_id: self.id)
|
47
49
|
end
|
48
50
|
end
|
49
51
|
end
|
@@ -4,32 +4,35 @@ module LaMaquina
|
|
4
4
|
class << self
|
5
5
|
attr_accessor :redis
|
6
6
|
|
7
|
-
def fire!(
|
8
|
-
|
9
|
-
updated_at = SecureRandom.uuid()
|
10
|
-
redis_key_string = redis_key klass, id
|
11
|
-
# make a class var with a default
|
12
|
-
|
13
|
-
cache_key = "#{redis_key_string}/#{updated_at}"
|
14
|
-
redis.set redis_key_string, cache_key
|
15
|
-
|
16
|
-
cache_key
|
7
|
+
def fire!( notified_class, id, notifier_class = "" )
|
8
|
+
update_cache notified_class, id
|
17
9
|
end
|
18
10
|
|
19
|
-
def cache_key(
|
11
|
+
def cache_key(notified_class, id)
|
20
12
|
|
21
|
-
|
13
|
+
key = redis_key notified_class, id
|
22
14
|
|
23
|
-
|
15
|
+
cache_key = redis.get key
|
24
16
|
# key found
|
25
|
-
return
|
17
|
+
return cache_key if cache_key
|
26
18
|
|
27
19
|
# there's no key; we can't very well rerun nil for cache_key. Let's make a new entry
|
28
|
-
|
20
|
+
update_cache notified_class, id
|
29
21
|
end
|
30
22
|
|
31
23
|
protected
|
32
24
|
|
25
|
+
def update_cache( notified_class, id)
|
26
|
+
uuid = SecureRandom.uuid()
|
27
|
+
key = redis_key notified_class, id
|
28
|
+
# make a class var with a default
|
29
|
+
|
30
|
+
cache_key = "#{key}/#{uuid}"
|
31
|
+
redis.set key, cache_key
|
32
|
+
|
33
|
+
cache_key
|
34
|
+
end
|
35
|
+
|
33
36
|
def redis_key(klass, id)
|
34
37
|
"#{klass}/#{id}"
|
35
38
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module LaMaquina
|
2
|
+
module Piston
|
3
|
+
class SunspotPiston < LaMaquina::Piston::Base
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def fire!( notified_class, id, notifier_class = "" )
|
7
|
+
target_class = map.mapping_for notified_class, id
|
8
|
+
target = target_class.find(id)
|
9
|
+
target.solr_index!
|
10
|
+
rescue => e
|
11
|
+
LaMaquina.error_notifier.notify( e,
|
12
|
+
notified_class: klass,
|
13
|
+
notified_id: id,
|
14
|
+
notifier_class: notifier_class )
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_accessor :map
|
18
|
+
end
|
19
|
+
self.map = LaMaquina::DependencyMap::ConstantMap.new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/la_maquina/piston.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module LaMaquina
|
2
2
|
module Piston
|
3
|
-
autoload :Base,
|
4
|
-
autoload :CachePiston,
|
3
|
+
autoload :Base, 'la_maquina/piston/base'
|
4
|
+
autoload :CachePiston, 'la_maquina/piston/cache_piston'
|
5
|
+
autoload :SunspotPiston, 'la_maquina/piston/sunspot_piston'
|
5
6
|
end
|
6
7
|
end
|
data/lib/la_maquina/version.rb
CHANGED
data/lib/la_maquina.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require "la_maquina/version"
|
2
2
|
|
3
3
|
module LaMaquina
|
4
|
-
autoload :
|
4
|
+
autoload :DependencyMap, 'la_maquina/dependency_map'
|
5
|
+
autoload :Engine, 'la_maquina/engine'
|
5
6
|
autoload :ErrorNotifier, 'la_maquina/error_notifier'
|
6
7
|
autoload :Piston, 'la_maquina/piston'
|
7
|
-
autoload :
|
8
|
+
autoload :Notifier, 'la_maquina/notifier'
|
8
9
|
|
9
10
|
mattr_accessor :error_notifier
|
10
11
|
error_notifier ||= LaMaquina::ErrorNotifier::Base
|
@@ -4,8 +4,12 @@ class Admin < ActiveRecord::Base
|
|
4
4
|
|
5
5
|
has_many :properties, :as => :user
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
searchable do
|
8
|
+
text :name
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
+
include LaMaquina::Notifier
|
12
|
+
notifies_about :self
|
13
|
+
|
14
|
+
notifies_about :self, using: ::DummyCommObject
|
11
15
|
end
|
@@ -2,8 +2,8 @@ class AdminTrait < ActiveRecord::Base
|
|
2
2
|
belongs_to :user, :class_name => "Admin"
|
3
3
|
belongs_to :thing, :class_name => "Trait"
|
4
4
|
|
5
|
-
include LaMaquina::
|
5
|
+
include LaMaquina::Notifier
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
notifies_about :user, :class => Admin
|
8
|
+
notifies_about :thing, :class_name => "Trait"
|
9
9
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
class AdminTraitModifier < ActiveRecord::Base
|
2
2
|
belongs_to :admin_trait
|
3
3
|
|
4
|
-
include LaMaquina::
|
4
|
+
include LaMaquina::Notifier
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
notifies_about :user, :through => :admin_trait, :class_name => "Admin"
|
7
|
+
notifies_about :thing, :through => :admin_trait, :class => Trait
|
8
8
|
end
|
@@ -17,10 +17,12 @@ development:
|
|
17
17
|
# re-generated from your development database when you run "rake".
|
18
18
|
# Do not set this db to the same as development or production.
|
19
19
|
test:
|
20
|
-
|
21
|
-
database:
|
22
|
-
|
23
|
-
|
20
|
+
<<: *default
|
21
|
+
database: db/test.sqlite3
|
22
|
+
# adapter: sqlite3
|
23
|
+
# database: ":memory:"
|
24
|
+
# pool: 5
|
25
|
+
# timeout: 5000
|
24
26
|
|
25
27
|
production:
|
26
28
|
<<: *default
|
@@ -1,3 +1,9 @@
|
|
1
1
|
LaMaquina::Piston::CachePiston.redis = Redis::Namespace.new(:cache_piston, redis: Redis.new)
|
2
|
-
|
3
|
-
|
2
|
+
ExtendedSunspotPiston.yaml_map = LaMaquina::DependencyMap::YamlMap.new( Rails.root + 'config/la_maquina/dependency_maps/sunspot_piston.yml' )
|
3
|
+
|
4
|
+
LaMaquina::Engine.install LaMaquina::Piston::CachePiston,
|
5
|
+
LaMaquina::Piston::SunspotPiston,
|
6
|
+
PrimitivePiston,
|
7
|
+
ExtendedSunspotPiston
|
8
|
+
|
9
|
+
LaMaquina.error_notifier = TestNotifier
|
@@ -0,0 +1 @@
|
|
1
|
+
admin: lol
|
@@ -0,0 +1 @@
|
|
1
|
+
admin: admin
|
data/test/dummy/db/schema.rb
CHANGED
Binary file
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class ExtendedSunspotPiston < LaMaquina::Piston::SunspotPiston
|
2
|
+
class << self
|
3
|
+
def fire!( notified_class, id, notifier_class = "" )
|
4
|
+
if yaml_map.mapping_for notified_class
|
5
|
+
super
|
6
|
+
$last_reindexed = notified_class
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_accessor :yaml_map
|
11
|
+
end
|
12
|
+
self.map = LaMaquina::DependencyMap::ConstantMap.new
|
13
|
+
end
|