seomoz-ripple 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. data/Gemfile +20 -0
  2. data/Guardfile +15 -0
  3. data/Rakefile +88 -0
  4. data/lib/rails/generators/ripple/configuration/configuration_generator.rb +13 -0
  5. data/lib/rails/generators/ripple/configuration/templates/ripple.yml +24 -0
  6. data/lib/rails/generators/ripple/js/js_generator.rb +13 -0
  7. data/lib/rails/generators/ripple/js/templates/js/contrib.js +63 -0
  8. data/lib/rails/generators/ripple/js/templates/js/iso8601.js +76 -0
  9. data/lib/rails/generators/ripple/js/templates/js/ripple.js +132 -0
  10. data/lib/rails/generators/ripple/model/model_generator.rb +20 -0
  11. data/lib/rails/generators/ripple/model/templates/model.rb +10 -0
  12. data/lib/rails/generators/ripple/observer/observer_generator.rb +16 -0
  13. data/lib/rails/generators/ripple/observer/templates/observer.rb +4 -0
  14. data/lib/rails/generators/ripple/test/templates/test_server.rb +46 -0
  15. data/lib/rails/generators/ripple/test/test_generator.rb +39 -0
  16. data/lib/rails/generators/ripple_generator.rb +78 -0
  17. data/lib/ripple.rb +79 -0
  18. data/lib/ripple/associations.rb +356 -0
  19. data/lib/ripple/associations/embedded.rb +35 -0
  20. data/lib/ripple/associations/instantiators.rb +26 -0
  21. data/lib/ripple/associations/linked.rb +65 -0
  22. data/lib/ripple/associations/many.rb +38 -0
  23. data/lib/ripple/associations/many_embedded_proxy.rb +38 -0
  24. data/lib/ripple/associations/many_linked_proxy.rb +66 -0
  25. data/lib/ripple/associations/many_reference_proxy.rb +93 -0
  26. data/lib/ripple/associations/many_stored_key_proxy.rb +76 -0
  27. data/lib/ripple/associations/one.rb +20 -0
  28. data/lib/ripple/associations/one_embedded_proxy.rb +35 -0
  29. data/lib/ripple/associations/one_key_proxy.rb +58 -0
  30. data/lib/ripple/associations/one_linked_proxy.rb +22 -0
  31. data/lib/ripple/associations/one_stored_key_proxy.rb +43 -0
  32. data/lib/ripple/associations/proxy.rb +118 -0
  33. data/lib/ripple/attribute_methods.rb +118 -0
  34. data/lib/ripple/attribute_methods/dirty.rb +50 -0
  35. data/lib/ripple/attribute_methods/query.rb +34 -0
  36. data/lib/ripple/attribute_methods/read.rb +26 -0
  37. data/lib/ripple/attribute_methods/write.rb +25 -0
  38. data/lib/ripple/callbacks.rb +74 -0
  39. data/lib/ripple/conflict/basic_resolver.rb +82 -0
  40. data/lib/ripple/conflict/document_hooks.rb +20 -0
  41. data/lib/ripple/conflict/resolver.rb +71 -0
  42. data/lib/ripple/conflict/test_helper.rb +33 -0
  43. data/lib/ripple/conversion.rb +28 -0
  44. data/lib/ripple/core_ext.rb +2 -0
  45. data/lib/ripple/core_ext/casting.rb +148 -0
  46. data/lib/ripple/core_ext/object.rb +8 -0
  47. data/lib/ripple/document.rb +104 -0
  48. data/lib/ripple/document/bucket_access.rb +25 -0
  49. data/lib/ripple/document/finders.rb +131 -0
  50. data/lib/ripple/document/key.rb +43 -0
  51. data/lib/ripple/document/link.rb +30 -0
  52. data/lib/ripple/document/persistence.rb +115 -0
  53. data/lib/ripple/embedded_document.rb +64 -0
  54. data/lib/ripple/embedded_document/around_callbacks.rb +18 -0
  55. data/lib/ripple/embedded_document/finders.rb +26 -0
  56. data/lib/ripple/embedded_document/persistence.rb +77 -0
  57. data/lib/ripple/i18n.rb +2 -0
  58. data/lib/ripple/inspection.rb +32 -0
  59. data/lib/ripple/locale/en.yml +21 -0
  60. data/lib/ripple/nested_attributes.rb +265 -0
  61. data/lib/ripple/observable.rb +28 -0
  62. data/lib/ripple/properties.rb +73 -0
  63. data/lib/ripple/property_type_mismatch.rb +12 -0
  64. data/lib/ripple/railtie.rb +13 -0
  65. data/lib/ripple/serialization.rb +84 -0
  66. data/lib/ripple/timestamps.rb +27 -0
  67. data/lib/ripple/translation.rb +14 -0
  68. data/lib/ripple/validations.rb +67 -0
  69. data/lib/ripple/validations/associated_validator.rb +43 -0
  70. data/ripple.gemspec +46 -0
  71. data/seomoz-ripple.gemspec +46 -0
  72. data/spec/fixtures/config.yml +8 -0
  73. data/spec/integration/ripple/associations_spec.rb +220 -0
  74. data/spec/integration/ripple/conflict_resolution_spec.rb +293 -0
  75. data/spec/integration/ripple/nested_attributes_spec.rb +264 -0
  76. data/spec/integration/ripple/persistence_spec.rb +57 -0
  77. data/spec/integration/ripple/search_associations_spec.rb +42 -0
  78. data/spec/ripple/associations/many_embedded_proxy_spec.rb +122 -0
  79. data/spec/ripple/associations/many_linked_proxy_spec.rb +191 -0
  80. data/spec/ripple/associations/many_reference_proxy_spec.rb +170 -0
  81. data/spec/ripple/associations/many_stored_key_proxy_spec.rb +158 -0
  82. data/spec/ripple/associations/one_embedded_proxy_spec.rb +125 -0
  83. data/spec/ripple/associations/one_key_proxy_spec.rb +82 -0
  84. data/spec/ripple/associations/one_linked_proxy_spec.rb +91 -0
  85. data/spec/ripple/associations/one_stored_key_proxy_spec.rb +72 -0
  86. data/spec/ripple/associations/proxy_spec.rb +84 -0
  87. data/spec/ripple/associations_spec.rb +129 -0
  88. data/spec/ripple/attribute_methods/dirty_spec.rb +80 -0
  89. data/spec/ripple/attribute_methods_spec.rb +230 -0
  90. data/spec/ripple/bucket_access_spec.rb +25 -0
  91. data/spec/ripple/callbacks_spec.rb +176 -0
  92. data/spec/ripple/conflict/resolver_spec.rb +42 -0
  93. data/spec/ripple/conversion_spec.rb +22 -0
  94. data/spec/ripple/core_ext_spec.rb +103 -0
  95. data/spec/ripple/document/link_spec.rb +67 -0
  96. data/spec/ripple/document_spec.rb +96 -0
  97. data/spec/ripple/embedded_document/finders_spec.rb +29 -0
  98. data/spec/ripple/embedded_document/persistence_spec.rb +80 -0
  99. data/spec/ripple/embedded_document_spec.rb +84 -0
  100. data/spec/ripple/finders_spec.rb +217 -0
  101. data/spec/ripple/inspection_spec.rb +51 -0
  102. data/spec/ripple/key_spec.rb +30 -0
  103. data/spec/ripple/observable_spec.rb +121 -0
  104. data/spec/ripple/persistence_spec.rb +326 -0
  105. data/spec/ripple/properties_spec.rb +262 -0
  106. data/spec/ripple/ripple_spec.rb +71 -0
  107. data/spec/ripple/serialization_spec.rb +51 -0
  108. data/spec/ripple/timestamps_spec.rb +76 -0
  109. data/spec/ripple/validations/associated_validator_spec.rb +77 -0
  110. data/spec/ripple/validations_spec.rb +104 -0
  111. data/spec/spec_helper.rb +26 -0
  112. data/spec/support/associations.rb +1 -0
  113. data/spec/support/associations/proxies.rb +17 -0
  114. data/spec/support/integration_setup.rb +11 -0
  115. data/spec/support/mocks.rb +4 -0
  116. data/spec/support/models.rb +4 -0
  117. data/spec/support/models/address.rb +12 -0
  118. data/spec/support/models/box.rb +13 -0
  119. data/spec/support/models/car.rb +16 -0
  120. data/spec/support/models/cardboard_box.rb +3 -0
  121. data/spec/support/models/clock.rb +12 -0
  122. data/spec/support/models/clock_observer.rb +3 -0
  123. data/spec/support/models/company.rb +23 -0
  124. data/spec/support/models/customer.rb +4 -0
  125. data/spec/support/models/driver.rb +6 -0
  126. data/spec/support/models/email.rb +4 -0
  127. data/spec/support/models/engine.rb +5 -0
  128. data/spec/support/models/family.rb +16 -0
  129. data/spec/support/models/favorite.rb +4 -0
  130. data/spec/support/models/invoice.rb +7 -0
  131. data/spec/support/models/late_invoice.rb +3 -0
  132. data/spec/support/models/ninja.rb +9 -0
  133. data/spec/support/models/note.rb +5 -0
  134. data/spec/support/models/page.rb +4 -0
  135. data/spec/support/models/paid_invoice.rb +4 -0
  136. data/spec/support/models/passenger.rb +6 -0
  137. data/spec/support/models/profile.rb +10 -0
  138. data/spec/support/models/seat.rb +5 -0
  139. data/spec/support/models/subscription.rb +27 -0
  140. data/spec/support/models/tasks.rb +14 -0
  141. data/spec/support/models/team.rb +11 -0
  142. data/spec/support/models/transactions.rb +17 -0
  143. data/spec/support/models/tree.rb +4 -0
  144. data/spec/support/models/user.rb +10 -0
  145. data/spec/support/models/wheel.rb +6 -0
  146. data/spec/support/models/widget.rb +22 -0
  147. data/spec/support/test_server.rb +18 -0
  148. data/spec/support/test_server.yml.example +2 -0
  149. metadata +362 -0
@@ -0,0 +1,35 @@
1
+ require 'ripple/associations/proxy'
2
+ require 'ripple/validations/associated_validator'
3
+
4
+ module Ripple
5
+ module Associations
6
+ module Embedded
7
+
8
+ def initialize(*args)
9
+ super
10
+ lazy_load_validates_associated
11
+ end
12
+
13
+ protected
14
+
15
+ def lazy_load_validates_associated
16
+ return if @owner.class.validators_on(@reflection.name).any? {|v| Ripple::Validations::AssociatedValidator === v}
17
+ @owner.class.validates @reflection.name, :associated => true
18
+ end
19
+
20
+ def assign_references(docs)
21
+ Array(docs).each do |doc|
22
+ next unless doc.respond_to?(:_parent_document=)
23
+ doc._parent_document = owner
24
+ end
25
+ end
26
+
27
+ def instantiate_target(*args)
28
+ doc = super
29
+ assign_references(doc)
30
+ doc
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'ripple/associations'
2
+
3
+ module Ripple
4
+ module Associations
5
+ module Instantiators
6
+
7
+ def build(attrs={})
8
+ instantiate_target(:new, attrs)
9
+ end
10
+
11
+ def create(attrs={})
12
+ instantiate_target(:create, attrs)
13
+ end
14
+
15
+ def create!(attrs={})
16
+ instantiate_target(:create!, attrs)
17
+ end
18
+
19
+ protected
20
+ def instantiate_target
21
+ raise NotImplementedError
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,65 @@
1
+ require 'ripple/associations'
2
+ require 'set'
3
+
4
+ module Ripple
5
+ module Associations
6
+ module Linked
7
+ def replace(value)
8
+ @reflection.verify_type!(value, @owner)
9
+ @owner.robject.links -= links
10
+ Array(value).compact.each do |doc|
11
+ @owner.robject.links << doc.to_link(@reflection.link_tag)
12
+ end
13
+ loaded
14
+ @keys = nil
15
+ @target = value
16
+ end
17
+
18
+ def replace_links(value)
19
+ @owner.robject.links -= links
20
+ Array(value).each do |link|
21
+ @owner.robject.links << link
22
+ end
23
+ reset
24
+ end
25
+
26
+ def keys
27
+ @keys ||= Set.new(links.map { |l| l.key })
28
+ end
29
+
30
+ def reset
31
+ super
32
+ @keys = nil
33
+ end
34
+
35
+ def include?(document)
36
+ return false unless document.respond_to?(:robject)
37
+
38
+ # TODO: when we allow polymorphic assocations, this will have to change
39
+ # since @reflection.bucket_name will be '_' in that case.
40
+ return false unless document.robject.bucket.name == @reflection.bucket_name
41
+ keys.include?(document.key)
42
+ end
43
+
44
+ protected
45
+ def links
46
+ @owner.robject.links.select(&@reflection.link_filter)
47
+ end
48
+
49
+ def robjects
50
+ walk_result = begin
51
+ @owner.robject.walk(*Array(@reflection.link_spec)).first || []
52
+ rescue
53
+ []
54
+ end
55
+
56
+ # We can get more robject results that we have links when there is conflict,
57
+ # since link-walking returns the robjects for the union of all sibling links.
58
+ # Here, we filter out robjects that should not be included.
59
+ walk_result.select do |robject|
60
+ links.include?(robject.to_link(@reflection.link_tag))
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,38 @@
1
+ require 'ripple/associations'
2
+
3
+ module Ripple
4
+ module Associations
5
+ module Many
6
+ include Instantiators
7
+
8
+ def to_ary
9
+ load_target
10
+ Array === target ? target.to_ary : Array(target)
11
+ end
12
+
13
+ def count
14
+ load_target
15
+ target.size
16
+ end
17
+
18
+ def reset
19
+ super
20
+ @target = []
21
+ end
22
+
23
+ def <<(value)
24
+ raise NotImplementedError
25
+ end
26
+
27
+ alias_method :push, :<<
28
+ alias_method :concat, :<<
29
+
30
+ protected
31
+ def instantiate_target(instantiator, attrs={})
32
+ doc = klass.send(instantiator, attrs)
33
+ self << doc
34
+ doc
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ require 'ripple/associations/proxy'
2
+ require 'ripple/associations/many'
3
+ require 'ripple/associations/embedded'
4
+
5
+ module Ripple
6
+ module Associations
7
+ class ManyEmbeddedProxy < Proxy
8
+ include Many
9
+ include Embedded
10
+
11
+ def <<(docs)
12
+ load_target
13
+ @reflection.verify_type!(Array(docs), @owner)
14
+ assign_references(docs)
15
+ @target += Array(docs)
16
+ self
17
+ end
18
+
19
+ def replace(docs)
20
+ @reflection.verify_type!(docs, @owner)
21
+ @_docs = docs.map { |doc| attrs = doc.respond_to?(:attributes_for_persistence) ? doc.attributes_for_persistence : doc }
22
+ assign_references(docs)
23
+ reset
24
+ @_docs
25
+ end
26
+
27
+ protected
28
+ def find_target
29
+ (@_docs || []).map do |attrs|
30
+ klass.instantiate(attrs).tap do |doc|
31
+ assign_references(doc)
32
+ end
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,66 @@
1
+ require 'ripple/associations/proxy'
2
+ require 'ripple/associations/many'
3
+ require 'ripple/associations/linked'
4
+
5
+ module Ripple
6
+ module Associations
7
+ class ManyLinkedProxy < Proxy
8
+ include Many
9
+ include Linked
10
+
11
+ def count
12
+ # avoid having to load all documents by using our keys set instead
13
+ keys.size
14
+ end
15
+
16
+ def <<(value)
17
+ if loaded?
18
+ new_target = @target.concat(Array(value))
19
+ replace new_target
20
+ else
21
+ @reflection.verify_type!([value], @owner)
22
+ @owner.robject.links << value.to_link(@reflection.link_tag)
23
+ appended_documents << value
24
+ @keys = nil
25
+ end
26
+
27
+ self
28
+ end
29
+
30
+ def delete(value)
31
+ load_target
32
+ @target.delete(value)
33
+ replace @target
34
+ self
35
+ end
36
+
37
+ def reset
38
+ @appended_documents = nil
39
+ super
40
+ end
41
+
42
+ def loaded_documents
43
+ (super + appended_documents).uniq
44
+ end
45
+
46
+ protected
47
+
48
+ def find_target
49
+ robjs = robjects
50
+
51
+ robjs.delete_if do |robj|
52
+ appended_documents.any? do |doc|
53
+ doc.key == robj.key &&
54
+ doc.class.bucket_name == robj.bucket.name
55
+ end
56
+ end
57
+
58
+ appended_documents + robjs.map {|robj| klass.send(:instantiate, robj) }
59
+ end
60
+
61
+ def appended_documents
62
+ @appended_documents ||= []
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,93 @@
1
+ require 'ripple/associations/proxy'
2
+ require 'ripple/associations/many'
3
+
4
+ require 'set'
5
+
6
+ module Ripple
7
+ module Associations
8
+ class ManyReferenceProxy < Proxy
9
+ include Many
10
+
11
+ def <<(value)
12
+ @reflection.verify_type!([value], @owner)
13
+
14
+ assign_key(value)
15
+ load_target
16
+ @target << value
17
+
18
+ self
19
+ end
20
+
21
+ def replace(value)
22
+ @reflection.verify_type!(value, @owner)
23
+ delete_all
24
+ Array(value).compact.each do |doc|
25
+ assign_key(doc)
26
+ end
27
+ loaded
28
+ @keys = nil
29
+ @target = Set.new(value)
30
+ end
31
+
32
+ def delete_all
33
+ load_target
34
+ @target.each do |e|
35
+ delete(e)
36
+ end
37
+ end
38
+
39
+ def delete(value)
40
+ load_target
41
+ assign_key(value, nil)
42
+ @target.delete(value)
43
+ end
44
+
45
+ def target
46
+ @target.to_a
47
+ end
48
+
49
+ def keys
50
+ @keys ||= Ripple.client.search(klass.bucket_name, "#{key_name}: #{@owner.key}")["response"]["docs"].inject(Set.new) do |set, search_document|
51
+ set << search_document["id"]
52
+ end
53
+ end
54
+
55
+ def reset
56
+ @keys = nil
57
+ super
58
+ end
59
+
60
+ def include?(document)
61
+ return false unless document.class.respond_to?(:bucket_name)
62
+
63
+ return false unless document.class.bucket_name == @reflection.bucket_name
64
+ keys.include?(document.key)
65
+ end
66
+
67
+ def count
68
+ if loaded?
69
+ @target.count
70
+ else
71
+ keys.count
72
+ end
73
+ end
74
+
75
+ protected
76
+ def find_target
77
+ Set.new(klass.find(keys.to_a))
78
+ end
79
+
80
+ def key_name
81
+ "#{@owner.class.name.underscore}_key"
82
+ end
83
+
84
+ def assign_key(target, key=@owner.key)
85
+ if target.new_record?
86
+ target.send("#{key_name}=", key)
87
+ else
88
+ target.update_attribute(key_name, key)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,76 @@
1
+ require 'ripple/associations/proxy'
2
+ require 'ripple/associations/many'
3
+
4
+ module Ripple
5
+ module Associations
6
+ class ManyStoredKeyProxy < Proxy
7
+ include Many
8
+
9
+ def count
10
+ keys.size
11
+ end
12
+
13
+ def <<(value)
14
+ @reflection.verify_type!([value], @owner)
15
+
16
+ raise "Unable to append if the document isn't first saved." if value.new_record?
17
+ load_target
18
+ @target << value
19
+ keys << value.key
20
+
21
+ self
22
+ end
23
+
24
+ def replace(value)
25
+ @reflection.verify_type!(value, @owner)
26
+
27
+ reset_owner_keys
28
+ value.each { |doc| self << doc }
29
+ @target = value
30
+ loaded
31
+ end
32
+
33
+ def delete(value)
34
+ keys.delete(value.key)
35
+ self
36
+ end
37
+
38
+ def keys
39
+ if @owner.send(keys_name).nil?
40
+ reset_owner_keys
41
+ end
42
+
43
+ @owner.send(keys_name)
44
+ end
45
+
46
+ def reset
47
+ super
48
+ self.owner_keys = @owner.robject.data ? @owner.robject.data[keys_name] : []
49
+ end
50
+
51
+ def include?(document)
52
+ return false unless document.respond_to?(:robject)
53
+ return false unless document.robject.bucket.name == @reflection.bucket_name
54
+ keys.include?(document.key)
55
+ end
56
+
57
+ def reset_owner_keys
58
+ self.owner_keys = []
59
+ end
60
+
61
+
62
+ protected
63
+ def find_target
64
+ klass.find(keys.to_a)
65
+ end
66
+
67
+ def keys_name
68
+ "#{@reflection.name.to_s.singularize}_keys"
69
+ end
70
+
71
+ def owner_keys=(new_keys)
72
+ @owner.send("#{keys_name}=", @owner.class.properties[keys_name].type.new(new_keys))
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,20 @@
1
+ require 'ripple/associations/instantiators'
2
+
3
+ module Ripple
4
+ module Associations
5
+ module One
6
+ include Instantiators
7
+
8
+ def to_a
9
+ [self]
10
+ end
11
+
12
+ protected
13
+ def instantiate_target(instantiator, attrs={})
14
+ @target = klass.send(instantiator, attrs)
15
+ loaded
16
+ @target
17
+ end
18
+ end
19
+ end
20
+ end