schema_monkey 2.1.2 → 2.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed3dab4edfea62a3b257c74ee6a1dbf073eccaae
4
- data.tar.gz: 401761ac3271ad840abd0df3da2e3260487688ee
3
+ metadata.gz: bd2b4e25c9e2bcc5fda7640439069570006e8551
4
+ data.tar.gz: 2270a11166bdffb014753139c575abfd2679d877
5
5
  SHA512:
6
- metadata.gz: 8548b6671077e6415af86d914f953e177a872d266c5080bb99a1f2f23d6cb92e7a6a8d279f672da2957fb643da56202f7ee6276e0adb080cf47fe0bb741eb139
7
- data.tar.gz: 51e70f4c17c995572d86749dacfe9601ab38aa6642eef9457a4f7b7aa50a1aa5f258d03992b156bc692b91686cda5e96e8a88d2c682b18c2a73695c78ad5ec36
6
+ metadata.gz: 3e7ba30f9468eca1f5f65590df3fbf99f67cfee9eaa75beb1c687215f85da7ace697de4fd543753bb4a129249d57777f84bbd147e10c89fca65c94d8c2975e2f
7
+ data.tar.gz: df9a386fd3ee4d6e97596bca33137826a362d2adebcd9239311588026a0627cf1c80321aa33aeeb4f991bbcb077262b18c7aa372ebf15ad9e0266fc7729f861c
data/README.md CHANGED
@@ -232,6 +232,7 @@ SchemaMonkey is tested on:
232
232
  <!-- SCHEMA_DEV: MATRIX - end -->
233
233
 
234
234
  ## Release Notes
235
+ * 2.1.3 -- Guard against multiple insertion of modules.
235
236
  * 2.1.2 -- Insert self earlier; don't wait for connection adapter to be instantiated. Fixes #6 re `db:schema:load`
236
237
  * 2.1.1 -- Bug fix: don't choke if a module contains a BasicObject const
237
238
  * 2.1.0 -- First version to support all of schema_plus's needs for the 1.8.7 -> 2.0 upgrade
@@ -3,7 +3,7 @@ module SchemaMonkey
3
3
 
4
4
  def initialize(mod)
5
5
  @root = mod
6
- @inserted_middleware = {}
6
+ @inserted = {}
7
7
  end
8
8
 
9
9
  def insert(dbm: nil)
@@ -16,6 +16,7 @@ module SchemaMonkey
16
16
  def insert_active_record(dbm: nil)
17
17
  # Kernel.warn "--- inserting active_record for #{@root}, dbm=#{dbm.inspect}"
18
18
  find_modules(:ActiveRecord, dbm: dbm).each do |mod|
19
+ next if @inserted[mod]
19
20
  relative_path = canonicalize_path(mod, :ActiveRecord, dbm)
20
21
  ActiveRecord.insert(relative_path, mod)
21
22
  end
@@ -23,10 +24,10 @@ module SchemaMonkey
23
24
 
24
25
  def insert_middleware(dbm: nil)
25
26
  find_modules(:Middleware, dbm: dbm).each do |mod|
26
- next if @inserted_middleware[mod]
27
+ next if @inserted[mod]
27
28
  relative_path = canonicalize_path(mod, :Middleware, dbm)
28
29
  Stack.insert(relative_path, mod) unless relative_path.empty?
29
- @inserted_middleware[mod] = true
30
+ @inserted[mod] = true
30
31
  end
31
32
  end
32
33
 
@@ -5,25 +5,23 @@ module SchemaMonkey
5
5
 
6
6
  class Monkey
7
7
 
8
- attr_reader :clients, :stacks
9
-
10
8
  def initialize
11
- @clients = []
9
+ @client_map = {}
12
10
  @inserted = nil
13
11
  @inserted_dbm = nil
14
12
  Module.insert ::ActiveRecord::ConnectionAdapters::AbstractAdapter, SchemaMonkey::ActiveRecord::ConnectionAdapters::AbstractAdapter
15
13
  end
16
14
 
17
15
  def register(mod)
18
- client = Client.new(mod)
19
- clients << client
20
- client.insert if @inserted
21
- client.insert(dbm: @inserted_dbm) if @inserted_dbm
16
+ @client_map[mod] ||= Client.new(mod).tap { |client|
17
+ client.insert if @inserted
18
+ client.insert(dbm: @inserted_dbm) if @inserted_dbm
19
+ }
22
20
  end
23
21
 
24
22
  def insert(dbm: nil)
25
23
  insert if dbm and not @inserted # first do all non-dbm-specific insertions
26
- clients.each &it.insert(dbm: dbm)
24
+ @client_map.values.each &it.insert(dbm: dbm)
27
25
  @inserted = true
28
26
  @inserted_dbm = dbm if dbm
29
27
  end
@@ -1,3 +1,3 @@
1
1
  module SchemaMonkey
2
- VERSION = "2.1.2"
2
+ VERSION = "2.1.3"
3
3
  end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe SchemaMonkey do
4
+
5
+ it "doesn't register same client's middleware twice" do
6
+ base = Module.new.tap(&it.module_eval(<<-END))
7
+ module Middleware
8
+ module Group
9
+ module Stack
10
+ ENV = [:result]
11
+ end
12
+ end
13
+ end
14
+ END
15
+ mod = Module.new.tap(&it.module_eval(<<-END))
16
+ module Middleware
17
+ module Group
18
+ module Stack
19
+ def after(env)
20
+ env.result += 1
21
+ end
22
+ end
23
+ end
24
+ end
25
+ END
26
+
27
+ SchemaMonkey.register(base)
28
+ expect( SchemaMonkey::Middleware::Group::Stack.start(result: 0) {}.result ).to eq 0
29
+ SchemaMonkey.register(mod)
30
+ expect( SchemaMonkey::Middleware::Group::Stack.start(result: 0) {}.result ).to eq 1
31
+ SchemaMonkey.register(mod)
32
+ expect( SchemaMonkey::Middleware::Group::Stack.start(result: 0) {}.result ).to eq 1
33
+ end
34
+
35
+ it "doesn't register same client's ActiveRecord extensions twice" do
36
+ mod = Module.new.tap(&it.module_eval(<<-END))
37
+ module ActiveRecord
38
+ module Base
39
+ def monkey_test
40
+ (super rescue 0) + 1
41
+ end
42
+ end
43
+ end
44
+ END
45
+ ActiveRecord::Migration.suppress_messages do
46
+ ActiveRecord::Migration.create_table "dummy", force: :cascade
47
+ end
48
+ record = Class.new(ActiveRecord::Base).tap{ |kls| kls.table_name = "dummy" }.new
49
+ SchemaMonkey.register mod
50
+ expect( record.monkey_test ).to eq 1
51
+ SchemaMonkey.register mod
52
+ expect( record.monkey_test ).to eq 1
53
+ end
54
+
55
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_monkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ronen barzel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-09 00:00:00.000000000 Z
11
+ date: 2015-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -183,6 +183,7 @@ files:
183
183
  - spec/connection_spec.rb
184
184
  - spec/middleware_spec.rb
185
185
  - spec/module_spec.rb
186
+ - spec/schema_monkey_spec.rb
186
187
  - spec/spec_helper.rb
187
188
  homepage: https://github.com/SchemaPlus/schema_monkey
188
189
  licenses:
@@ -213,4 +214,5 @@ test_files:
213
214
  - spec/connection_spec.rb
214
215
  - spec/middleware_spec.rb
215
216
  - spec/module_spec.rb
217
+ - spec/schema_monkey_spec.rb
216
218
  - spec/spec_helper.rb