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/associations/one'
3
+ require 'ripple/associations/embedded'
4
+
5
+ module Ripple
6
+ module Associations
7
+ class OneEmbeddedProxy < Proxy
8
+ include One
9
+ include Embedded
10
+
11
+ def replace(doc)
12
+ @reflection.verify_type!(doc, @owner)
13
+ @_doc = doc.respond_to?(:attributes_for_persistence) ? doc.attributes_for_persistence : doc
14
+ assign_references(doc)
15
+
16
+ if doc.is_a?(@reflection.klass)
17
+ loaded
18
+ @target = doc
19
+ else
20
+ reset
21
+ end
22
+
23
+ @_doc
24
+ end
25
+
26
+ protected
27
+ def find_target
28
+ return nil unless @_doc
29
+ klass.instantiate(@_doc).tap do |doc|
30
+ assign_references(doc)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,58 @@
1
+ require 'ripple/associations/proxy'
2
+ require 'ripple/associations/one'
3
+
4
+ module Ripple
5
+ module Associations
6
+ class OneKeyProxy < Proxy
7
+ include One
8
+
9
+ def replace(doc)
10
+ @reflection.verify_type!(doc, owner)
11
+
12
+ reset_previous_target_key_delegate
13
+ assign_new_target_key_delegate(doc)
14
+
15
+ loaded
16
+ @target = doc
17
+ end
18
+
19
+ def find_target
20
+ klass.find(owner.key)
21
+ end
22
+
23
+ protected
24
+ def instantiate_target(instantiator, attrs={})
25
+ @target = super
26
+ @target.key = owner.key
27
+ @target
28
+ end
29
+
30
+ private
31
+ def reset_previous_target_key_delegate
32
+ @target.key_delegate = @target if @target
33
+ end
34
+
35
+ def assign_new_target_key_delegate(doc)
36
+ doc.class.send(:include, Ripple::Associations::KeyDelegator) unless doc.class.include?(Ripple::Associations::KeyDelegator)
37
+ owner.key_delegate = doc.key_delegate = owner
38
+ end
39
+
40
+ end
41
+
42
+ module KeyDelegator
43
+ attr_accessor :key_delegate
44
+
45
+ def key_delegate
46
+ @key_delegate || self
47
+ end
48
+
49
+ def key
50
+ self === key_delegate ? super : key_delegate.key
51
+ end
52
+
53
+ def key=(value)
54
+ self === key_delegate ? super(value) : key_delegate.key = value
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,22 @@
1
+ require 'ripple/associations/proxy'
2
+ require 'ripple/associations/one'
3
+ require 'ripple/associations/linked'
4
+
5
+ module Ripple
6
+ module Associations
7
+ class OneLinkedProxy < Proxy
8
+ include One
9
+ include Linked
10
+
11
+ protected
12
+ def find_target
13
+ return nil if links.blank?
14
+
15
+ robjs = robjects
16
+ return nil if robjs.blank?
17
+
18
+ klass.send(:instantiate, robjs.first)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,43 @@
1
+ require 'ripple/associations/proxy'
2
+ require 'ripple/associations/one'
3
+
4
+ module Ripple
5
+ module Associations
6
+ class OneStoredKeyProxy < Proxy
7
+ include One
8
+
9
+ def replace(value)
10
+ @reflection.verify_type!(value, owner)
11
+
12
+ if value
13
+ assign_key(value.key)
14
+ else
15
+ assign_key(nil)
16
+ end
17
+
18
+ @target = value
19
+ loaded
20
+ end
21
+
22
+ protected
23
+
24
+ def key
25
+ @owner.send(key_name)
26
+ end
27
+
28
+ def assign_key(value)
29
+ @owner.send("#{key_name}=", value)
30
+ end
31
+
32
+ def key_name
33
+ "#{@reflection.name}_key"
34
+ end
35
+
36
+ def find_target
37
+ return nil if key.blank?
38
+
39
+ klass.find(key)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,118 @@
1
+ require 'ripple/associations'
2
+
3
+ module Ripple
4
+ module Associations
5
+ class Proxy
6
+ alias :proxy_respond_to? :respond_to?
7
+ alias :proxy_extend :extend
8
+
9
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^object_id$)/ }
10
+
11
+ attr_reader :owner, :reflection, :target
12
+
13
+ alias :proxy_owner :owner
14
+ alias :proxy_target :target
15
+ alias :proxy_reflection :reflection
16
+
17
+ delegate :klass, :to => :proxy_reflection
18
+ delegate :options, :to => :proxy_reflection
19
+ delegate :collection, :to => :klass
20
+
21
+ def initialize(owner, reflection)
22
+ @owner, @reflection = owner, reflection
23
+ Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) }
24
+ reset
25
+ end
26
+
27
+ def inspect
28
+ load_target
29
+ target.inspect
30
+ end
31
+
32
+ def loaded?
33
+ @loaded
34
+ end
35
+
36
+ def loaded
37
+ @loaded = true
38
+ end
39
+
40
+ def nil?
41
+ load_target
42
+ target.nil?
43
+ end
44
+
45
+ def blank?
46
+ load_target
47
+ target.blank?
48
+ end
49
+
50
+ def present?
51
+ load_target
52
+ target.present?
53
+ end
54
+
55
+ def reload
56
+ reset
57
+ load_target
58
+ self unless target.nil?
59
+ end
60
+
61
+ def replace(v)
62
+ raise NotImplementedError
63
+ end
64
+
65
+ def reset
66
+ @loaded = false
67
+ @target = nil
68
+ end
69
+
70
+ def respond_to?(*args)
71
+ proxy_respond_to?(*args) || (load_target && target.respond_to?(*args))
72
+ end
73
+
74
+ def send(method, *args, &block)
75
+ if proxy_respond_to?(method)
76
+ super
77
+ else
78
+ load_target
79
+ target.send(method, *args, &block)
80
+ end
81
+ end
82
+
83
+ def ===(other)
84
+ load_target
85
+ other === target
86
+ end
87
+
88
+ def loaded_documents
89
+ loaded? ? Array(target) : []
90
+ end
91
+
92
+ def has_changed_documents?
93
+ loaded_documents.any? { |doc| doc.changed? }
94
+ end
95
+
96
+ protected
97
+ def method_missing(method, *args, &block)
98
+ load_target
99
+
100
+ if block_given?
101
+ target.send(method, *args) { |*block_args| block.call(*block_args) }
102
+ else
103
+ target.send(method, *args)
104
+ end
105
+ end
106
+
107
+ def load_target
108
+ @target = find_target unless loaded?
109
+ loaded
110
+ @target
111
+ end
112
+
113
+ def find_target
114
+ raise NotImplementedError
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,118 @@
1
+ require 'ripple/translation'
2
+ require 'active_support/concern'
3
+ require 'active_model/attribute_methods'
4
+ require 'active_model/mass_assignment_security'
5
+ require 'ripple/attribute_methods/read'
6
+ require 'ripple/attribute_methods/write'
7
+ require 'ripple/attribute_methods/query'
8
+ require 'ripple/attribute_methods/dirty'
9
+
10
+ module Ripple
11
+ # Makes ActiveRecord-like attribute accessors based on your
12
+ # {Document}'s properties.
13
+ module AttributeMethods
14
+ include Translation
15
+ extend ActiveSupport::Concern
16
+ include ActiveModel::AttributeMethods
17
+
18
+ included do
19
+ include Read
20
+ include Write
21
+ include Query
22
+ include Dirty
23
+ include ActiveModel::MassAssignmentSecurity
24
+
25
+ attr_protected :key
26
+ end
27
+
28
+ module ClassMethods
29
+ # @private
30
+ def property(key, type, options={})
31
+ undefine_attribute_methods
32
+ super
33
+ end
34
+
35
+ # Generates all the attribute-related methods for properties defined
36
+ # on the document, including accessors, mutators and query methods.
37
+ def define_attribute_methods
38
+ super(properties.keys)
39
+ end
40
+ end
41
+
42
+ module InstanceMethods
43
+ # A copy of the values of all attributes in the Document. The result
44
+ # is not memoized, so use sparingly. This does not include associated objects,
45
+ # nor embedded documents.
46
+ # @return [Hash] all document attributes, by key
47
+ def attributes
48
+ raw_attributes.reject { |k, v| !respond_to?(k) }
49
+ end
50
+
51
+ def raw_attributes
52
+ self.class.properties.values.inject(@attributes.with_indifferent_access) do |hash, prop|
53
+ hash[prop.key] = attribute(prop.key)
54
+ hash
55
+ end
56
+ end
57
+
58
+ # Mass assign the document's attributes.
59
+ # @param [Hash] attrs the attributes to assign
60
+ def attributes=(attrs)
61
+ raise ArgumentError, t('attribute_hash') unless Hash === attrs
62
+ sanitize_for_mass_assignment(attrs).each do |k,v|
63
+ if respond_to?("#{k}=")
64
+ __send__("#{k}=",v)
65
+ else
66
+ raise ArgumentError, t('undefined_property', :prop => k, :class => self.class.name)
67
+ end
68
+ end
69
+ end
70
+
71
+ # @private
72
+ def raw_attributes=(attrs)
73
+ raise ArgumentError, t('attribute_hash') unless Hash === attrs
74
+ attrs.each do |k,v|
75
+ next if k.to_sym == :key
76
+ if respond_to?("#{k}=")
77
+ __send__("#{k}=",v)
78
+ else
79
+ __send__(:attribute=,k,v)
80
+ end
81
+ end
82
+ end
83
+
84
+ # @private
85
+ def initialize(attrs={})
86
+ super()
87
+ @attributes = attributes_from_property_defaults
88
+ self.attributes = attrs
89
+ yield self if block_given?
90
+ end
91
+
92
+ # @private
93
+ def method_missing(method, *args, &block)
94
+ self.class.define_attribute_methods
95
+ super
96
+ end
97
+
98
+ # @private
99
+ def respond_to?(*args)
100
+ self.class.define_attribute_methods
101
+ super
102
+ end
103
+
104
+ protected
105
+ # @private
106
+ def attribute_method?(attr_name)
107
+ self.class.properties.include?(attr_name)
108
+ end
109
+
110
+ def attributes_from_property_defaults
111
+ self.class.properties.values.inject({}) do |hash, prop|
112
+ hash[prop.key] = prop.default unless prop.default.nil?
113
+ hash
114
+ end.with_indifferent_access
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_support/concern'
2
+ require 'active_model/dirty'
3
+
4
+ module Ripple
5
+ module AttributeMethods
6
+ module Dirty
7
+ extend ActiveSupport::Concern
8
+ include ActiveModel::Dirty
9
+
10
+ # @private
11
+ def really_save(*args)
12
+ if result = super
13
+ @previously_changed = changes
14
+ changed_attributes.clear
15
+ end
16
+ result
17
+ end
18
+
19
+ # @private
20
+ def reload
21
+ super.tap do
22
+ changed_attributes.clear
23
+ end
24
+ end
25
+
26
+ # @private
27
+ def initialize(attrs={})
28
+ super(attrs)
29
+ changed_attributes.clear
30
+ end
31
+
32
+ # Determines if the document has any chnages.
33
+ # @return [Boolean] true if this document, or any of its embedded
34
+ # documents at any level, have changed.
35
+ def changed?
36
+ super || self.class.embedded_associations.any? do |association|
37
+ send(association.name).has_changed_documents?
38
+ end
39
+ end
40
+
41
+ private
42
+ def attribute=(attr_name, value)
43
+ if self.class.properties.include?(attr_name.to_sym) && @attributes[attr_name] != value
44
+ attribute_will_change!(attr_name)
45
+ end
46
+ super
47
+ end
48
+ end
49
+ end
50
+ end