active_mongoid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.coveralls.yml +1 -0
  4. data/.gitignore +22 -0
  5. data/.ruby_gemset +1 -0
  6. data/.ruby_version +1 -0
  7. data/.travis.yml +11 -0
  8. data/Appraisals +7 -0
  9. data/Gemfile +4 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +198 -0
  12. data/Rakefile +7 -0
  13. data/active_mongoid.gemspec +36 -0
  14. data/gemfiles/mongoid_2.8.gemfile +7 -0
  15. data/gemfiles/mongoid_2.8.gemfile.lock +105 -0
  16. data/gemfiles/mongoid_3.1.gemfile +7 -0
  17. data/gemfiles/mongoid_3.1.gemfile.lock +108 -0
  18. data/lib/active_mongoid.rb +8 -0
  19. data/lib/active_mongoid/associations.rb +82 -0
  20. data/lib/active_mongoid/associations/binding.rb +77 -0
  21. data/lib/active_mongoid/associations/builder.rb +26 -0
  22. data/lib/active_mongoid/associations/builders/in.rb +17 -0
  23. data/lib/active_mongoid/associations/builders/many.rb +15 -0
  24. data/lib/active_mongoid/associations/builders/one.rb +15 -0
  25. data/lib/active_mongoid/associations/document_relation/accessors.rb +100 -0
  26. data/lib/active_mongoid/associations/document_relation/associations.rb +33 -0
  27. data/lib/active_mongoid/associations/document_relation/auto_save.rb +31 -0
  28. data/lib/active_mongoid/associations/document_relation/bindings/in.rb +48 -0
  29. data/lib/active_mongoid/associations/document_relation/bindings/many.rb +19 -0
  30. data/lib/active_mongoid/associations/document_relation/bindings/one.rb +19 -0
  31. data/lib/active_mongoid/associations/document_relation/builders.rb +31 -0
  32. data/lib/active_mongoid/associations/document_relation/dependent.rb +26 -0
  33. data/lib/active_mongoid/associations/document_relation/macros.rb +51 -0
  34. data/lib/active_mongoid/associations/document_relation/referenced/in.rb +72 -0
  35. data/lib/active_mongoid/associations/document_relation/referenced/many.rb +125 -0
  36. data/lib/active_mongoid/associations/document_relation/referenced/one.rb +75 -0
  37. data/lib/active_mongoid/associations/many.rb +211 -0
  38. data/lib/active_mongoid/associations/metadata.rb +229 -0
  39. data/lib/active_mongoid/associations/one.rb +21 -0
  40. data/lib/active_mongoid/associations/proxy.rb +38 -0
  41. data/lib/active_mongoid/associations/record_relation/accessors.rb +80 -0
  42. data/lib/active_mongoid/associations/record_relation/associations.rb +33 -0
  43. data/lib/active_mongoid/associations/record_relation/auto_save.rb +43 -0
  44. data/lib/active_mongoid/associations/record_relation/bindings/in.rb +48 -0
  45. data/lib/active_mongoid/associations/record_relation/bindings/many.rb +19 -0
  46. data/lib/active_mongoid/associations/record_relation/bindings/one.rb +19 -0
  47. data/lib/active_mongoid/associations/record_relation/builders.rb +31 -0
  48. data/lib/active_mongoid/associations/record_relation/dependent.rb +26 -0
  49. data/lib/active_mongoid/associations/record_relation/macros.rb +65 -0
  50. data/lib/active_mongoid/associations/record_relation/referenced/in.rb +72 -0
  51. data/lib/active_mongoid/associations/record_relation/referenced/many.rb +128 -0
  52. data/lib/active_mongoid/associations/record_relation/referenced/one.rb +75 -0
  53. data/lib/active_mongoid/associations/targets/enumerable.rb +161 -0
  54. data/lib/active_mongoid/bson_id.rb +44 -0
  55. data/lib/active_mongoid/finder_proxy.rb +55 -0
  56. data/lib/active_mongoid/finders.rb +60 -0
  57. data/lib/active_mongoid/version.rb +3 -0
  58. data/spec/lib/associations/document_relation/accessors_spec.rb +330 -0
  59. data/spec/lib/associations/document_relation/auto_save_spec.rb +157 -0
  60. data/spec/lib/associations/document_relation/bindings/in_spec.rb +39 -0
  61. data/spec/lib/associations/document_relation/bindings/many_spec.rb +36 -0
  62. data/spec/lib/associations/document_relation/bindings/one_spec.rb +39 -0
  63. data/spec/lib/associations/document_relation/builders_spec.rb +117 -0
  64. data/spec/lib/associations/document_relation/dependent_spec.rb +87 -0
  65. data/spec/lib/associations/document_relation/macros_spec.rb +68 -0
  66. data/spec/lib/associations/document_relation/referenced/in_spec.rb +27 -0
  67. data/spec/lib/associations/document_relation/referenced/many_spec.rb +32 -0
  68. data/spec/lib/associations/document_relation/referenced/one_spec.rb +28 -0
  69. data/spec/lib/associations/metadata_spec.rb +157 -0
  70. data/spec/lib/associations/record_relation/accessors_spec.rb +328 -0
  71. data/spec/lib/associations/record_relation/auto_save_spec.rb +157 -0
  72. data/spec/lib/associations/record_relation/bindings/in_spec.rb +39 -0
  73. data/spec/lib/associations/record_relation/bindings/many_spec.rb +39 -0
  74. data/spec/lib/associations/record_relation/bindings/one_spec.rb +57 -0
  75. data/spec/lib/associations/record_relation/builders_spec.rb +118 -0
  76. data/spec/lib/associations/record_relation/dependent_spec.rb +87 -0
  77. data/spec/lib/associations/record_relation/macros_spec.rb +73 -0
  78. data/spec/lib/associations/record_relation/referenced/in_spec.rb +27 -0
  79. data/spec/lib/associations/record_relation/referenced/many_spec.rb +32 -0
  80. data/spec/lib/associations/record_relation/referenced/one_spec.rb +27 -0
  81. data/spec/lib/bson_id_spec.rb +48 -0
  82. data/spec/lib/finders_spec.rb +105 -0
  83. data/spec/spec_helper.rb +89 -0
  84. data/spec/support/models/active_record/address.rb +6 -0
  85. data/spec/support/models/active_record/division.rb +16 -0
  86. data/spec/support/models/active_record/division_setting.rb +9 -0
  87. data/spec/support/models/active_record/player.rb +12 -0
  88. data/spec/support/models/mongoid/league.rb +10 -0
  89. data/spec/support/models/mongoid/person.rb +9 -0
  90. data/spec/support/models/mongoid/post.rb +9 -0
  91. data/spec/support/models/mongoid/stat.rb +8 -0
  92. data/spec/support/models/mongoid/team.rb +9 -0
  93. data/spec/support/shared_examples/shared_many_spec.rb +411 -0
  94. metadata +370 -0
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "mongoid", "~> 3.1.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,108 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ active_mongoid (0.0.1)
5
+ activerecord
6
+ activesupport
7
+ after_do
8
+ bson_ext
9
+ mongo
10
+ mongoid
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ activemodel (3.2.21)
16
+ activesupport (= 3.2.21)
17
+ builder (~> 3.0.0)
18
+ activerecord (3.2.21)
19
+ activemodel (= 3.2.21)
20
+ activesupport (= 3.2.21)
21
+ arel (~> 3.0.2)
22
+ tzinfo (~> 0.3.29)
23
+ activesupport (3.2.21)
24
+ i18n (~> 0.6, >= 0.6.4)
25
+ multi_json (~> 1.0)
26
+ after_do (0.3.1)
27
+ appraisal (1.0.2)
28
+ bundler
29
+ rake
30
+ thor (>= 0.14.0)
31
+ arel (3.0.3)
32
+ bson (1.11.1)
33
+ bson_ext (1.11.1)
34
+ bson (~> 1.11.1)
35
+ builder (3.0.4)
36
+ coderay (1.1.0)
37
+ coveralls (0.7.1)
38
+ multi_json (~> 1.3)
39
+ rest-client
40
+ simplecov (>= 0.7)
41
+ term-ansicolor
42
+ thor
43
+ database_cleaner (1.3.0)
44
+ diff-lcs (1.2.5)
45
+ docile (1.1.5)
46
+ i18n (0.6.11)
47
+ method_source (0.8.2)
48
+ mime-types (2.4.3)
49
+ mongo (1.11.1)
50
+ bson (= 1.11.1)
51
+ mongoid (3.1.6)
52
+ activemodel (~> 3.2)
53
+ moped (~> 1.4)
54
+ origin (~> 1.0)
55
+ tzinfo (~> 0.3.29)
56
+ moped (1.5.2)
57
+ multi_json (1.10.1)
58
+ netrc (0.8.0)
59
+ origin (1.1.0)
60
+ pry (0.10.1)
61
+ coderay (~> 1.1.0)
62
+ method_source (~> 0.8.1)
63
+ slop (~> 3.4)
64
+ rake (10.3.2)
65
+ rest-client (1.7.2)
66
+ mime-types (>= 1.16, < 3.0)
67
+ netrc (~> 0.7)
68
+ rspec (3.1.0)
69
+ rspec-core (~> 3.1.0)
70
+ rspec-expectations (~> 3.1.0)
71
+ rspec-mocks (~> 3.1.0)
72
+ rspec-core (3.1.7)
73
+ rspec-support (~> 3.1.0)
74
+ rspec-expectations (3.1.2)
75
+ diff-lcs (>= 1.2.0, < 2.0)
76
+ rspec-support (~> 3.1.0)
77
+ rspec-mocks (3.1.3)
78
+ rspec-support (~> 3.1.0)
79
+ rspec-support (3.1.2)
80
+ simplecov (0.9.1)
81
+ docile (~> 1.1.0)
82
+ multi_json (~> 1.0)
83
+ simplecov-html (~> 0.8.0)
84
+ simplecov-gem-adapter (1.0.1)
85
+ simplecov
86
+ simplecov-html (0.8.0)
87
+ slop (3.6.0)
88
+ sqlite3 (1.3.10)
89
+ term-ansicolor (1.3.0)
90
+ tins (~> 1.0)
91
+ thor (0.19.1)
92
+ tins (1.3.3)
93
+ tzinfo (0.3.42)
94
+
95
+ PLATFORMS
96
+ ruby
97
+
98
+ DEPENDENCIES
99
+ active_mongoid!
100
+ appraisal
101
+ coveralls
102
+ database_cleaner
103
+ mongoid (~> 3.1.0)
104
+ pry
105
+ rspec
106
+ simplecov
107
+ simplecov-gem-adapter
108
+ sqlite3
@@ -0,0 +1,8 @@
1
+ require "active_mongoid/associations"
2
+ require "active_mongoid/bson_id"
3
+ require "active_mongoid/finder_proxy"
4
+ require "active_mongoid/finders"
5
+
6
+ module ActiveMongoid
7
+ BSON = ::Gem::Version.new(Mongoid::VERSION) < ::Gem::Version.new("3.0.0") ? ::BSON : ::Moped::BSON
8
+ end
@@ -0,0 +1,82 @@
1
+ require "active_mongoid/associations/metadata"
2
+ require "active_mongoid/associations/builder"
3
+ require "active_mongoid/associations/binding"
4
+ require "active_mongoid/associations/proxy"
5
+ require "active_mongoid/associations/one"
6
+ require "active_mongoid/associations/many"
7
+ require "active_mongoid/associations/builders/in"
8
+ require "active_mongoid/associations/builders/one"
9
+ require "active_mongoid/associations/builders/many"
10
+ require "active_mongoid/associations/document_relation/associations"
11
+ require "active_mongoid/associations/record_relation/associations"
12
+ require "active_mongoid/associations/targets/enumerable"
13
+ require 'after_do'
14
+
15
+ module ActiveMongoid
16
+ module Associations
17
+ extend ActiveSupport::Concern
18
+
19
+ included do
20
+ extend ::AfterDo
21
+ class_attribute :am_relations
22
+ self.am_relations = {}
23
+
24
+ if defined?(::ActiveRecord::Base) && self <= ::ActiveRecord::Base
25
+ include DocumentRelation::Associations
26
+ elsif defined?(::Mongoid::Document) && self.included_modules.include?(::Mongoid::Document)
27
+ include RecordRelation::Associations
28
+ else
29
+ raise
30
+ end
31
+
32
+ before :reload do
33
+ am_relations.each_pair do |name, meta|
34
+ if instance_variable_defined?("@#{name}")
35
+ if instance_variable_get("@#{name}")
36
+ remove_instance_variable("@#{name}")
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ def reflect_on_am_association(name)
44
+ self.class.reflect_on_am_association(name.to_s)
45
+ end
46
+
47
+ def referenced_many_records?
48
+ __metadata__ && __metadata__.macro == :has_many_records
49
+ end
50
+
51
+ def referenced_one_record?
52
+ __metadata__ && __metadata__.macro == :has_one_record
53
+ end
54
+
55
+ def referenced_many_documents?
56
+ __metadata__ && __metadata__.macro == :has_many_documents
57
+ end
58
+
59
+ def referenced_one_document?
60
+ __metadata__ && __metadata__.macro == :has_one_document
61
+ end
62
+
63
+ module ClassMethods
64
+
65
+ def reflect_on_am_association(name)
66
+ am_relations[name.to_s]
67
+ end
68
+
69
+ private
70
+
71
+ def characterize_association(name, relation, options)
72
+ ActiveMongoid::Associations::Metadata.new({
73
+ :relation => relation,
74
+ :inverse_class_name => self.name,
75
+ :name => name
76
+ }.merge(options))
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,77 @@
1
+ require 'mongoid/threaded'
2
+
3
+ module ActiveMongoid
4
+ module Associations
5
+ class Binding
6
+ include ::Mongoid::Threaded::Lifecycle
7
+
8
+ attr_reader :base, :target, :__metadata__
9
+
10
+ def initialize(base, target, metadata)
11
+ @base = base
12
+ @target = target
13
+ @__metadata__ = metadata
14
+ end
15
+
16
+ private
17
+
18
+ def check_inverse!(object)
19
+ # check for inverse relation and raise exception when not
20
+ true
21
+ end
22
+
23
+ def bind_foreign_key(base, id)
24
+ return if base.send(__metadata__.foreign_key) == id
25
+ base.send(__metadata__.foreign_key_setter, id)
26
+ end
27
+
28
+ def bind_polymorphic_type(base, name)
29
+ if __metadata__.type
30
+ base.send(__metadata__.type_setter, name)
31
+ end
32
+ end
33
+
34
+ def bind_polymorphic_inverse_type(base, name)
35
+ if __metadata__.inverse_type
36
+ base.send(__metadata__.inverse_type_setter, name)
37
+ end
38
+ end
39
+
40
+ def bind_inverse(object, inverse)
41
+ unless _binding?
42
+ _binding do
43
+ if object.respond_to?(__metadata__.inverse_setter)
44
+ object.send(__metadata__.inverse_setter, inverse)
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def record_id(base)
51
+ base.send(__metadata__.primary_key)
52
+ end
53
+
54
+ def set_base_metadata
55
+ inverse_metadata = __metadata__.inverse_metadata(target)
56
+ if inverse_metadata != __metadata__ && !inverse_metadata.nil?
57
+ base.__metadata__ = inverse_metadata
58
+ end
59
+ end
60
+
61
+ def bind_from_relational_parent(object)
62
+ check_inverse!(object)
63
+ bind_foreign_key(object, record_id(base))
64
+ bind_polymorphic_type(object, base.class.name)
65
+ bind_inverse(object, base)
66
+ end
67
+
68
+ def unbind_from_relational_parent(object)
69
+ check_inverse!(object)
70
+ bind_foreign_key(object, nil)
71
+ bind_polymorphic_type(object, nil)
72
+ bind_inverse(object, nil)
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,26 @@
1
+ module ActiveMongoid
2
+ module Associations
3
+ class Builder
4
+
5
+ attr_reader :base, :__metadata__, :object
6
+
7
+ def initialize(base, metadata, object)
8
+ @base = base
9
+ @__metadata__ = metadata
10
+ @object = object
11
+ end
12
+
13
+ protected
14
+
15
+ def klass
16
+ @klass ||= __metadata__.klass
17
+ end
18
+
19
+ def query?
20
+ obj = Array(object).first
21
+ !obj.is_a?(::Mongoid::Document) && !obj.is_a?(::ActiveRecord::Base) && !obj.nil?
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveMongoid
2
+ module Associations
3
+ module Builders
4
+ class In < Builder
5
+
6
+ def build(type = nil)
7
+ return object unless query?
8
+ return nil if object.blank?
9
+ type = type.constantize if type and type.is_a?(String)
10
+ model = type ? type : __metadata__.klass
11
+ __metadata__.criteria(object, model).first
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveMongoid
2
+ module Associations
3
+ module Builders
4
+ class Many < Builder
5
+
6
+ def build(type = nil)
7
+ return object unless query?
8
+ return [] if object.is_a?(Array)
9
+ __metadata__.criteria(object, base.class)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveMongoid
2
+ module Associations
3
+ module Builders
4
+ class One < Builder
5
+
6
+ def build(type = nil)
7
+ return object unless query?
8
+ return nil if base.new_record?
9
+ __metadata__.criteria(object, base.class).first
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,100 @@
1
+ module ActiveMongoid
2
+ module Associations
3
+ module DocumentRelation
4
+ module Accessors
5
+ extend ActiveSupport::Concern
6
+
7
+ def build_document(name, object, metadata)
8
+ relation = create_document_relation(object, metadata)
9
+ set_document_relation(name, relation)
10
+ end
11
+
12
+ def set_document_relation(name, object)
13
+ instance_variable_set("@#{name}", object)
14
+ end
15
+
16
+ def create_document_relation(object, metadata)
17
+ type = self.attributes[metadata.inverse_type]
18
+ target = metadata.builder(self, object).build(type)
19
+ target ? metadata.relation.new(self, target, metadata) : nil
20
+ end
21
+
22
+ private
23
+
24
+ def get_document_relation(name, metadata, object, reload = false)
25
+ if !reload && (value = instance_variable_get("@#{name}")) != nil
26
+ value
27
+ else
28
+ if object && needs_no_database_query?(object, metadata)
29
+ build_document(name, object, metadata)
30
+ else
31
+ build_document(name, self.send(metadata.key), metadata)
32
+ end
33
+ end
34
+ end
35
+
36
+ def needs_no_database_query?(object, metadata)
37
+ object.is_a?(::Mongoid::Document) && object.id.to_s == attributes[metadata.key].to_s
38
+ end
39
+
40
+
41
+ module ClassMethods
42
+
43
+ private
44
+
45
+ def existence_check(name)
46
+ module_eval <<-END
47
+ def #{name}?
48
+ !__send__(:#{name}).blank?
49
+ end
50
+ alias :has_#{name}? :#{name}?
51
+ END
52
+ self
53
+ end
54
+
55
+ # Getters
56
+
57
+ def document_getter(name, metadata)
58
+ self.instance_eval do
59
+ define_method(name) do |reload = false|
60
+ get_document_relation(name, metadata, nil, reload)
61
+ end
62
+ end
63
+ end
64
+
65
+ def document_id_getter(name, metadata)
66
+ self.instance_eval do
67
+ define_method("#{name}_id") do
68
+ attribute = read_attribute("#{name}_id")
69
+ attribute.nil? ? nil : ::ActiveMongoid::BSON::ObjectId.from_string(attribute)
70
+ end
71
+ end
72
+ end
73
+
74
+ def document_setter(name, metadata)
75
+ self.instance_eval do
76
+ define_method("#{name}=") do |object|
77
+ if value = get_document_relation(name, metadata, object)
78
+ set_document_relation(name, value.substitute(object))
79
+ else
80
+ build_document(name, object, metadata)
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ def document_id_setter(name, metadata)
87
+ self.instance_eval do
88
+ define_method("#{name}_id=") do |bson_id|
89
+ attribute = bson_id.nil? ? nil : bson_id.to_s
90
+ write_attribute("#{name}_id", attribute)
91
+ end
92
+ end
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+ end
99
+ end
100
+ end