mongoid_alize 0.5.0 → 0.6.0

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
- SHA1:
3
- metadata.gz: 8787d5a6ebd7c17e2619267de0cb2c3b536dad5b
4
- data.tar.gz: 1953945ef586e4ab4d319a22cd9acc1abb58e5b1
2
+ SHA256:
3
+ metadata.gz: b298e436dfc8fa64055e1a1fce7cff2b84d99e55fea84229872490669c6d1142
4
+ data.tar.gz: 12e69d1410466c984322d2029ff3a2900b19c0c74f7d2cf0cd7a4c141a105d13
5
5
  SHA512:
6
- metadata.gz: 16a40f35a37a71b24f599f4acb73ebac65622e033ac1b1583b2a551c605690818b75fc422a7bae072cc3af4bd74d39e44360b285d972ad56eec563f491e58b3a
7
- data.tar.gz: 0f6bb6a4efc21d697e723ab9ab5219dea616d4134bf9f80662beb1ddb0da97268ed85da0d9943d1961d770ebbc8817e24e7b763fb5b7eac12460a4e2f0d70010
6
+ metadata.gz: d07bf01de381cbd098c0bbd2525a229a55b8aa5545f33113039252a799735fc4d88449d292e89a53fb031c2a52e82eed2e542cd1d227d90984d092eb34f324ab
7
+ data.tar.gz: b5b9b581d68af949d0ba8feab28f670d003175f598afe23e0fef1ec857e4db11b75ac7192e990881b4223def3c74042cf464f63dc20fbbecf66cd7594b4e4f7b
@@ -51,11 +51,11 @@ module Mongoid
51
51
  end
52
52
 
53
53
  def callback_name
54
- "_#{aliased_callback_name}"
54
+ :"_#{aliased_callback_name}"
55
55
  end
56
56
 
57
57
  def aliased_callback_name
58
- "denormalize_#{direction}_#{relation}"
58
+ :"denormalize_#{direction}_#{relation}"
59
59
  end
60
60
 
61
61
  def define_denorm_attrs
@@ -72,7 +72,7 @@ module Mongoid
72
72
  end
73
73
 
74
74
  def denorm_attrs_name
75
- "#{callback_name}_attrs"
75
+ :"#{callback_name}_attrs"
76
76
  end
77
77
 
78
78
  def field_values(source, options={})
@@ -79,7 +79,7 @@ module Mongoid
79
79
  one = Mongoid::Relations::One
80
80
  many = Mongoid::Relations::Many
81
81
 
82
- def (many = many.dup).==(klass)
82
+ def (many).==(klass)
83
83
  [Mongoid::Relations::Many,
84
84
  Mongoid::Relations::Referenced::Many].map(&:name).include?(klass.name)
85
85
  end
@@ -1,3 +1,5 @@
1
+ require 'mongoid/compatibility'
2
+
1
3
  module Mongoid
2
4
  module Alize
3
5
  class ToCallback < Callback
@@ -56,7 +58,7 @@ module Mongoid
56
58
  is_one = #{is_one?}
57
59
  prefixed_name = #{prefixed_name}
58
60
  if is_one
59
- #{relation_set('prefixed_name', 'nil')}
61
+ #{relation_unset('prefixed_name')}
60
62
  else
61
63
  #{pull_from_inverse}
62
64
  end
@@ -91,15 +93,19 @@ module Mongoid
91
93
  end
92
94
 
93
95
  def relation_set(field, value)
94
- mongoid_four_or_newer? ? "relation.set(#{field} => #{value})" : "relation.set(#{field}, #{value})"
96
+ Mongoid::Compatibility::Version.mongoid4_or_newer? ? "relation.set(#{field}.to_sym => #{value})" : "relation.set(#{field}, #{value})"
97
+ end
98
+
99
+ def relation_unset(field)
100
+ "relation.unset(#{field}.to_sym)"
95
101
  end
96
102
 
97
103
  def relation_pull(field, value)
98
- mongoid_four_or_newer? ? "relation.pull(#{field} => #{value})" : "relation.pull(#{field}, #{value})"
104
+ Mongoid::Compatibility::Version.mongoid4_or_newer? ? "relation.pull(#{field}.to_sym => #{value})" : "relation.pull(#{field}, #{value})"
99
105
  end
100
106
 
101
107
  def relation_push(field, value)
102
- mongoid_four_or_newer? ? "relation.push(#{field} => #{value})" : "relation.push(#{field}, #{value})"
108
+ Mongoid::Compatibility::Version.mongoid4_or_newer? ? "relation.push(#{field}.to_sym => #{value})" : "relation.push(#{field}, #{value})"
103
109
  end
104
110
 
105
111
  def is_one?
@@ -144,21 +150,17 @@ module Mongoid
144
150
  end
145
151
 
146
152
  def aliased_destroy_callback_name
147
- "denormalize_destroy_#{direction}_#{relation}"
153
+ :"denormalize_destroy_#{direction}_#{relation}"
148
154
  end
149
155
 
150
156
  def destroy_callback_name
151
- "_#{aliased_destroy_callback_name}"
157
+ :"_#{aliased_destroy_callback_name}"
152
158
  end
153
159
 
154
160
  def direction
155
161
  "to"
156
162
  end
157
163
 
158
- def mongoid_four_or_newer?
159
- Mongoid::VERSION.split('.').first.to_i >= 4
160
- end
161
-
162
164
  end
163
165
  end
164
166
  end
@@ -2,18 +2,26 @@ class Head
2
2
  include Mongoid::Document
3
3
  include Mongoid::Alize
4
4
 
5
- if SpecHelper.mongoid_four_or_newer?
5
+ if Mongoid::Compatibility::Version.mongoid4_or_newer?
6
6
  include Mongoid::Attributes::Dynamic
7
7
  end
8
8
 
9
9
  field :size, type: Integer
10
10
  field :weight
11
11
 
12
- # to whom it's attached
13
- belongs_to :person
12
+ if Mongoid::Compatibility::Version.mongoid6_or_newer?
13
+ # to whom it's attached
14
+ belongs_to :person, optional: true
14
15
 
15
- # in whose possession it is
16
- belongs_to :captor, :class_name => "Person", :inverse_of => :heads
16
+ # in whose possession it is
17
+ belongs_to :captor, :class_name => "Person", :inverse_of => :heads, optional: true
18
+ else
19
+ # to whom it's attached
20
+ belongs_to :person
21
+
22
+ # in whose possession it is
23
+ belongs_to :captor, :class_name => "Person", :inverse_of => :heads
24
+ end
17
25
 
18
26
  # who'd otherwise like to possess it
19
27
  has_and_belongs_to_many :wanted_by, :class_name => "Person", :inverse_of => :wants
@@ -24,8 +32,13 @@ class Head
24
32
  # a relation with no inverse
25
33
  has_many :admirer, :class_name => "Person", :inverse_of => nil
26
34
 
27
- # a polymorphic one-to-one relation
28
- belongs_to :nearest, :polymorphic => true
35
+ if Mongoid::Compatibility::Version.mongoid6_or_newer?
36
+ # a polymorphic one-to-one relation
37
+ belongs_to :nearest, :polymorphic => true, optional: true
38
+ else
39
+ # a polymorphic one-to-one relation
40
+ belongs_to :nearest, :polymorphic => true
41
+ end
29
42
 
30
43
  # a polymorphic one-to-many relation
31
44
  has_many :below_people, :class_name => "Person", :as => :above
@@ -2,14 +2,14 @@ class Person
2
2
  include Mongoid::Document
3
3
  include Mongoid::Alize
4
4
 
5
- if SpecHelper.mongoid_four_or_newer?
5
+ if Mongoid::Compatibility::Version.mongoid4_or_newer?
6
6
  include Mongoid::Attributes::Dynamic
7
7
  end
8
8
 
9
9
  field :name, type: String
10
10
  field :created_at, type: Time
11
11
 
12
- if SpecHelper.mongoid_three_or_newer?
12
+ if Mongoid::Compatibility::Version.mongoid4_or_newer?
13
13
  field :my_date, type: Date
14
14
  field :my_datetime, type: DateTime
15
15
  end
@@ -23,14 +23,24 @@ class Person
23
23
  # the heads wanted from others
24
24
  has_and_belongs_to_many :wants, :class_name => "Head", :inverse_of => :wanted_by
25
25
 
26
- # the only head that is watching
27
- belongs_to :seen_by, :class_name => "Head", :inverse_of => :sees
26
+ if Mongoid::Compatibility::Version.mongoid6_or_newer?
27
+ # the only head that is watching
28
+ belongs_to :seen_by, :class_name => "Head", :inverse_of => :sees, optional: true
29
+ else
30
+ # the only head that is watching
31
+ belongs_to :seen_by, :class_name => "Head", :inverse_of => :sees
32
+ end
28
33
 
29
34
  # a polymorphic one-to-one relation
30
35
  has_one :nearest_head, :class_name => "Head", :as => :nearest
31
36
 
32
- # a polymorphic one-to-many relation
33
- belongs_to :above, :polymorphic => true
37
+ if Mongoid::Compatibility::Version.mongoid6_or_newer?
38
+ # a polymorphic one-to-many relation
39
+ belongs_to :above, :polymorphic => true, optional: true
40
+ else
41
+ # a polymorphic one-to-many relation
42
+ belongs_to :above, :polymorphic => true
43
+ end
34
44
 
35
45
  def location
36
46
  "Paris"
@@ -57,8 +57,8 @@ describe Mongoid::Alize::Callback do
57
57
 
58
58
  describe "#alias_callback" do
59
59
  it "should alias the callback on the klass and make it public" do
60
- mock(@callback.klass).alias_method("denormalize_spec_person", "_denormalize_spec_person")
61
- mock(@callback.klass).public("denormalize_spec_person")
60
+ mock(@callback.klass).alias_method(:denormalize_spec_person, :_denormalize_spec_person)
61
+ mock(@callback.klass).public(:denormalize_spec_person)
62
62
  @callback.send(:alias_callback)
63
63
  end
64
64
 
@@ -76,15 +76,15 @@ describe Mongoid::Alize::Callback do
76
76
  end
77
77
 
78
78
  it "should have a callback name" do
79
- @callback.callback_name.should == "_denormalize_spec_person"
79
+ @callback.callback_name.should == :_denormalize_spec_person
80
80
  end
81
81
 
82
82
  it "should have aliased callback name" do
83
- @callback.aliased_callback_name.should == "denormalize_spec_person"
83
+ @callback.aliased_callback_name.should == :denormalize_spec_person
84
84
  end
85
85
 
86
86
  it "should add _attrs to the callback name" do
87
- @callback.denorm_attrs_name.should == "_denormalize_spec_person_attrs"
87
+ @callback.denorm_attrs_name.should == :_denormalize_spec_person_attrs
88
88
  end
89
89
  end
90
90
 
@@ -101,7 +101,7 @@ describe Mongoid::Alize::Callback do
101
101
  it "should return the denorm_attrs w/ to_s applied" do
102
102
  define_denorm_attrs
103
103
  @head = Head.new
104
- @head.send("_denormalize_spec_person_attrs", nil).should == ["name", "created_at"]
104
+ @head.send(:_denormalize_spec_person_attrs, nil).should == ["name", "created_at"]
105
105
  end
106
106
  end
107
107
 
@@ -113,7 +113,7 @@ describe Mongoid::Alize::Callback do
113
113
  it "should return the denorm_attrs w/ to_s applied" do
114
114
  define_denorm_attrs
115
115
  @head = Head.new
116
- @head.send("_denormalize_spec_person_attrs", Person.new).should == ["name", "created_at"]
116
+ @head.send(:_denormalize_spec_person_attrs, Person.new).should == ["name", "created_at"]
117
117
  end
118
118
  end
119
119
  end
@@ -51,8 +51,6 @@ describe Mongoid::Alize::Callbacks::From::Many do
51
51
  end
52
52
 
53
53
  before do
54
- now = Time.now
55
- stub(Time).now { now }
56
54
  @head = Head.create
57
55
  @person = Person.create(:name => "Bob")
58
56
 
@@ -103,8 +101,6 @@ describe Mongoid::Alize::Callbacks::From::Many do
103
101
  end
104
102
 
105
103
  before do
106
- now = Time.now
107
- stub(Time).now { now }
108
104
  @head = Head.create
109
105
  @person = Person.create(:name => "Bob")
110
106
 
@@ -31,7 +31,7 @@ describe Mongoid::Alize::FromCallback do
31
31
 
32
32
  describe "#set_callback" do
33
33
  it "should set a callback on the klass" do
34
- mock(@callback.klass).set_callback(:save, :before, "denormalize_from_person")
34
+ mock(@callback.klass).set_callback(:save, :before, :denormalize_from_person)
35
35
  @callback.send(:set_callback)
36
36
  end
37
37
 
@@ -4,14 +4,13 @@ describe Mongoid::Alize::ToCallback do
4
4
 
5
5
  before do
6
6
  @now = Time.parse('2013-01-05T12:00:22-700')
7
- stub(Time).now { @now }
8
7
 
9
8
  Head.class_eval do
10
9
  field :sees_fields, :type => Array, :default => []
11
10
  end
12
11
  Person.class_eval do
13
12
  fields = [:name, :location, :created_at]
14
- fields += [:my_date, :my_datetime] if SpecHelper.mongoid_3?
13
+ fields += [:my_date, :my_datetime] if Mongoid::Compatibility::Version.mongoid4_or_newer?
15
14
  alize_to :seen_by, fields: fields
16
15
  end
17
16
 
@@ -25,7 +24,7 @@ describe Mongoid::Alize::ToCallback do
25
24
  "created_at" => @now }
26
25
 
27
26
  fields.merge!( "my_date" => @now.to_date,
28
- "my_datetime" => @now.to_datetime ) if SpecHelper.mongoid_3?
27
+ "my_datetime" => @now.to_datetime ) if Mongoid::Compatibility::Version.mongoid4_or_newer?
29
28
 
30
29
  fields
31
30
  end
@@ -38,7 +37,7 @@ describe Mongoid::Alize::ToCallback do
38
37
  fields = sees_fields_with_id.merge!( "created_at" => @now.utc )
39
38
 
40
39
  fields.merge!( "my_date" => @now.utc.to_date,
41
- "my_datetime" => @now.utc ) if SpecHelper.mongoid_3?
40
+ "my_datetime" => @now.utc ) if Mongoid::Compatibility::Version.mongoid4_or_newer?
42
41
 
43
42
  fields
44
43
  end
@@ -26,11 +26,11 @@ describe Mongoid::Alize::ToCallback do
26
26
 
27
27
  describe "names" do
28
28
  it "should assign a destroy callback name" do
29
- @callback.destroy_callback_name.should == "_denormalize_destroy_to_head"
29
+ @callback.destroy_callback_name.should == :_denormalize_destroy_to_head
30
30
  end
31
31
 
32
32
  it "should assign an aliased destroy callback name" do
33
- @callback.aliased_destroy_callback_name.should == "denormalize_destroy_to_head"
33
+ @callback.aliased_destroy_callback_name.should == :denormalize_destroy_to_head
34
34
  end
35
35
 
36
36
  it "should assign a prefixed name from the inverse if present" do
@@ -56,7 +56,7 @@ describe Mongoid::Alize::ToCallback do
56
56
 
57
57
  describe "#set_callback" do
58
58
  it "should set a callback on the klass" do
59
- mock(@callback.klass).set_callback(:save, :after, "denormalize_to_head")
59
+ mock(@callback.klass).set_callback(:save, :after, :denormalize_to_head)
60
60
  @callback.send(:set_callback)
61
61
  end
62
62
 
@@ -69,7 +69,7 @@ describe Mongoid::Alize::ToCallback do
69
69
 
70
70
  describe "#set_destroy_callback" do
71
71
  it "should set a destroy callback on the klass" do
72
- mock(@callback.klass).set_callback(:destroy, :after, "denormalize_destroy_to_head")
72
+ mock(@callback.klass).set_callback(:destroy, :after, :denormalize_destroy_to_head)
73
73
  @callback.send(:set_destroy_callback)
74
74
  end
75
75
 
@@ -82,8 +82,8 @@ describe Mongoid::Alize::ToCallback do
82
82
 
83
83
  describe "#alias_destroy_callback" do
84
84
  it "should alias the destroy callback on the klass" do
85
- mock(@callback.klass).alias_method("denormalize_destroy_to_head", "_denormalize_destroy_to_head")
86
- mock(@callback.klass).public("denormalize_destroy_to_head")
85
+ mock(@callback.klass).alias_method(:denormalize_destroy_to_head, :_denormalize_destroy_to_head)
86
+ mock(@callback.klass).public(:denormalize_destroy_to_head)
87
87
  @callback.send(:alias_destroy_callback)
88
88
  end
89
89
 
@@ -11,7 +11,6 @@ describe Mongoid::Alize do
11
11
 
12
12
  before do
13
13
  @now = Time.now
14
- stub(Time).now { @now }
15
14
  @head = Head.new(:size => @size = 10, created_at: @now)
16
15
  @person = Person.new(:name => @name = "Bob", created_at: @now)
17
16
  end
@@ -1,30 +1,13 @@
1
1
  require 'rubygems'
2
2
  require 'mongoid'
3
+ require 'mongoid/compatibility'
4
+ require 'rr'
3
5
 
4
6
  unless ENV['CI']
5
7
  require 'awesome_print'
6
8
  require 'wirble'
7
9
  end
8
10
 
9
- module SpecHelper
10
-
11
- # for mongoize_spec
12
- def self.mongoid_3?
13
- defined?(Mongoid::VERSION) && Mongoid::VERSION.split('.').first.to_i == 3
14
- end
15
-
16
- # different connection scheme starting with three
17
- def self.mongoid_three_or_newer?
18
- defined?(Mongoid::VERSION) && Mongoid::VERSION.split('.').first.to_i >= 3
19
- end
20
-
21
- # dynamic attributes required starting with four
22
- def self.mongoid_four_or_newer?
23
- defined?(Mongoid::VERSION) && Mongoid::VERSION.split('.').first.to_i >= 4
24
- end
25
-
26
- end
27
-
28
11
  Mongoid.configure do |config|
29
12
 
30
13
  if defined?(Moped)
@@ -35,12 +18,8 @@ Mongoid.configure do |config|
35
18
  logger.level = Logger::INFO
36
19
  end
37
20
 
38
- if SpecHelper.mongoid_three_or_newer?
39
- config.connect_to("mongoid_alize_test")
40
- else
41
- config.master = Mongo::Connection.new("localhost", 27017,
42
- :logger => logger).db("mongoid_alize_test")
43
- end
21
+ name = "mongoid_alize_test"
22
+ config.respond_to?(:connect_to) ? config.connect_to(name) : config.master = Mongo::Connection.new.db(name)
44
23
  end
45
24
 
46
25
  require File.expand_path("../../lib/mongoid_alize", __FILE__)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_alize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Dzielak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-04 00:00:00.000000000 Z
11
+ date: 2018-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mongoid-compatibility
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +53,7 @@ dependencies:
39
53
  - !ruby/object:Gem::Version
40
54
  version: 2.6.0
41
55
  description: Keep data in sync as you denormalize across any type of relation.
42
- email: github_public@dz.oib.com
56
+ email: jdzielak@gmail.com
43
57
  executables: []
44
58
  extensions: []
45
59
  extra_rdoc_files: []
@@ -95,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
109
  version: '0'
96
110
  requirements: []
97
111
  rubyforge_project:
98
- rubygems_version: 2.4.5
112
+ rubygems_version: 2.7.3
99
113
  signing_key:
100
114
  specification_version: 4
101
115
  summary: Comprehensive field denormalization for Mongoid that stays in sync.