ripple 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. data/lib/ripple.rb +17 -1
  2. data/lib/ripple/associations.rb +157 -0
  3. data/lib/ripple/associations/embedded.rb +42 -0
  4. data/lib/ripple/associations/instantiators.rb +39 -0
  5. data/lib/ripple/{document/associations → associations}/linked.rb +9 -11
  6. data/lib/ripple/{document/associations/one_embedded_proxy.rb → associations/many.rb} +30 -21
  7. data/lib/ripple/{document/associations/embedded.rb → associations/many_embedded_proxy.rb} +26 -23
  8. data/lib/ripple/{embedded_document/conversion.rb → associations/one.rb} +8 -13
  9. data/lib/ripple/{document/associations/instantiators.rb → associations/one_embedded_proxy.rb} +17 -19
  10. data/lib/ripple/associations/proxy.rb +123 -0
  11. data/lib/ripple/attribute_methods.rb +116 -0
  12. data/lib/ripple/{document/attribute_methods/write.rb → attribute_methods/dirty.rb} +26 -16
  13. data/lib/ripple/attribute_methods/query.rb +48 -0
  14. data/lib/ripple/{document/attribute_methods → attribute_methods}/read.rb +16 -18
  15. data/lib/ripple/attribute_methods/write.rb +38 -0
  16. data/lib/ripple/callbacks.rb +73 -0
  17. data/lib/ripple/{document/associations/one.rb → conversion.rb} +18 -13
  18. data/lib/ripple/document.rb +19 -9
  19. data/lib/ripple/embedded_document.rb +10 -10
  20. data/lib/ripple/embedded_document/persistence.rb +12 -53
  21. data/lib/ripple/properties.rb +83 -0
  22. data/lib/ripple/timestamps.rb +34 -0
  23. data/lib/ripple/{document/validations.rb → validations.rb} +31 -33
  24. data/lib/ripple/{document/validations → validations}/associated_validator.rb +9 -10
  25. data/spec/integration/ripple/associations_spec.rb +1 -2
  26. data/spec/integration/ripple/persistence_spec.rb +2 -3
  27. data/spec/ripple/associations/many_embedded_proxy_spec.rb +2 -2
  28. data/spec/ripple/associations/one_embedded_proxy_spec.rb +2 -2
  29. data/spec/ripple/associations/proxy_spec.rb +1 -1
  30. data/spec/ripple/associations_spec.rb +15 -20
  31. data/spec/ripple/attribute_methods_spec.rb +3 -6
  32. data/spec/ripple/callbacks_spec.rb +1 -1
  33. data/spec/ripple/{embedded_document/conversion_spec.rb → conversion_spec.rb} +4 -4
  34. data/spec/ripple/embedded_document/persistence_spec.rb +4 -16
  35. data/spec/ripple/properties_spec.rb +17 -18
  36. data/spec/ripple/timestamps_spec.rb +1 -1
  37. data/spec/ripple/validations_spec.rb +1 -1
  38. data/spec/spec_helper.rb +1 -1
  39. data/spec/support/associations/proxies.rb +4 -4
  40. metadata +27 -29
  41. data/lib/ripple/document/associations.rb +0 -154
  42. data/lib/ripple/document/associations/many.rb +0 -52
  43. data/lib/ripple/document/associations/many_embedded_proxy.rb +0 -49
  44. data/lib/ripple/document/associations/proxy.rb +0 -125
  45. data/lib/ripple/document/attribute_methods.rb +0 -118
  46. data/lib/ripple/document/attribute_methods/dirty.rb +0 -52
  47. data/lib/ripple/document/attribute_methods/query.rb +0 -49
  48. data/lib/ripple/document/callbacks.rb +0 -75
  49. data/lib/ripple/document/properties.rb +0 -85
  50. data/lib/ripple/document/timestamps.rb +0 -22
  51. data/spec/support/integration.rb +0 -4
@@ -18,20 +18,19 @@
18
18
  require 'ripple'
19
19
 
20
20
  module Ripple
21
- module Document
22
- module Validations
23
- class AssociatedValidator < ActiveModel::EachValidator
24
- def validate_each(record, attribute, value)
25
- return if (value.is_a?(Array) ? value : [value]).collect{ |r| r.nil? || r.valid? }.all?
21
+ module Validations
22
+ class AssociatedValidator < ActiveModel::EachValidator
23
+ def validate_each(record, attribute, value)
24
+ unless Array(value).all? {|r| r.nil? || r.valid? }
26
25
  record.errors.add(attribute, :invalid, :default => options[:message], :value => value)
27
26
  end
28
27
  end
28
+ end
29
29
 
30
- module ClassMethods
31
- def validates_associated(*attr_names)
32
- validates_with AssociatedValidator, _merge_attributes(attr_names)
33
- end
30
+ module ClassMethods
31
+ def validates_associated(*attr_names)
32
+ validates_with AssociatedValidator, _merge_attributes(attr_names)
34
33
  end
35
34
  end
36
35
  end
37
- end
36
+ end
@@ -15,7 +15,6 @@ require File.expand_path("../../../spec_helper", __FILE__)
15
15
 
16
16
  describe "Ripple Associations" do
17
17
  before :all do
18
- switch_to_test_node
19
18
  Object.module_eval do
20
19
  class User
21
20
  include Ripple::Document
@@ -78,4 +77,4 @@ describe "Ripple Associations" do
78
77
  Object.send(:remove_const, :Address)
79
78
  end
80
79
 
81
- end
80
+ end
@@ -14,8 +14,7 @@
14
14
  require File.expand_path("../../../spec_helper", __FILE__)
15
15
 
16
16
  describe "Ripple Persistence" do
17
- before :all do
18
- switch_to_test_node
17
+ before :all do
19
18
  Object.module_eval do
20
19
  class Widget
21
20
  include Ripple::Document
@@ -51,4 +50,4 @@ describe "Ripple Persistence" do
51
50
  Object.send(:remove_const, :Widget)
52
51
  end
53
52
 
54
- end
53
+ end
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
  require File.expand_path("../../../spec_helper", __FILE__)
15
15
 
16
- describe Ripple::Document::Associations::ManyEmbeddedProxy do
16
+ describe Ripple::Associations::ManyEmbeddedProxy do
17
17
  require 'support/models/user'
18
18
  require 'support/models/address'
19
19
  require 'support/models/note'
@@ -121,4 +121,4 @@ describe Ripple::Document::Associations::ManyEmbeddedProxy do
121
121
  @user.addresses << @address
122
122
  @user.addresses.to_ary.should == [@address]
123
123
  end
124
- end
124
+ end
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
  require File.expand_path("../../../spec_helper", __FILE__)
15
15
 
16
- describe Ripple::Document::Associations::OneEmbeddedProxy do
16
+ describe Ripple::Associations::OneEmbeddedProxy do
17
17
  require 'support/models/family'
18
18
  require 'support/models/user'
19
19
  require 'support/models/address'
@@ -127,4 +127,4 @@ describe Ripple::Document::Associations::OneEmbeddedProxy do
127
127
  end
128
128
  end
129
129
 
130
- end
130
+ end
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
  require File.expand_path("../../../spec_helper", __FILE__)
15
15
 
16
- describe Ripple::Document::Associations::Proxy do
16
+ describe Ripple::Associations::Proxy do
17
17
  require 'support/associations/proxies'
18
18
 
19
19
  before :each do
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
  require File.expand_path("../../spec_helper", __FILE__)
15
15
 
16
- describe Ripple::Document::Associations do
16
+ describe Ripple::Associations do
17
17
  require 'support/models/invoice'
18
18
  require 'support/models/customer'
19
19
  require 'support/models/note'
@@ -22,14 +22,9 @@ describe Ripple::Document::Associations do
22
22
  Invoice.should respond_to(:associations)
23
23
  Invoice.associations.should be_kind_of(Hash)
24
24
  end
25
-
26
- it "should add associations on the class instance" do
27
- Invoice.new.should respond_to(:associations)
28
- Invoice.new.associations.should be_kind_of(Hash)
29
- end
30
-
25
+
31
26
  it "should collect the embedded associations" do
32
- Invoice.new.embedded_associations.should == Array(Invoice.associations[:note])
27
+ Invoice.embedded_associations.should == Array(Invoice.associations[:note])
33
28
  end
34
29
 
35
30
  it "should copy associations to a subclass" do
@@ -56,24 +51,24 @@ describe Ripple::Document::Associations do
56
51
  end
57
52
  end
58
53
 
59
- describe Ripple::Document::Association do
54
+ describe Ripple::Association do
60
55
  it "should initialize with a type and name" do
61
- lambda { Ripple::Document::Association.new(:many, :pages) }.should_not raise_error
56
+ lambda { Ripple::Association.new(:many, :pages) }.should_not raise_error
62
57
  end
63
58
 
64
59
  describe "determining the class name" do
65
60
  it "should default to the camelized class name on :one relationships" do
66
- @association = Ripple::Document::Association.new(:one, :page)
61
+ @association = Ripple::Association.new(:one, :page)
67
62
  @association.class_name.should == "Page"
68
63
  end
69
64
 
70
65
  it "should default to the singularized camelized class name on :many relationships" do
71
- @association = Ripple::Document::Association.new(:many, :pages)
66
+ @association = Ripple::Association.new(:many, :pages)
72
67
  @association.class_name.should == "Page"
73
68
  end
74
69
 
75
70
  it "should use the :class_name option when given" do
76
- @association = Ripple::Document::Association.new(:many, :pages, :class_name => "Note")
71
+ @association = Ripple::Association.new(:many, :pages, :class_name => "Note")
77
72
  @association.class_name.should == "Note"
78
73
  end
79
74
  end
@@ -82,30 +77,30 @@ describe Ripple::Document::Association do
82
77
  require 'support/models/tree'
83
78
 
84
79
  it "should default to the constantized class name" do
85
- @association = Ripple::Document::Association.new(:one, :t, :class_name => "Trunk")
80
+ @association = Ripple::Association.new(:one, :t, :class_name => "Trunk")
86
81
  @association.klass.should == Trunk
87
82
  end
88
83
 
89
84
  it "should be determined by the derived class name" do
90
- @association = Ripple::Document::Association.new(:many, :branches)
85
+ @association = Ripple::Association.new(:many, :branches)
91
86
  @association.klass.should == Branch
92
87
  end
93
88
 
94
89
  it "should use the :class option when given" do
95
- @association = Ripple::Document::Association.new(:many, :pages, :class => Leaf)
90
+ @association = Ripple::Association.new(:many, :pages, :class => Leaf)
96
91
  @association.klass.should == Leaf
97
92
  end
98
93
  end
99
94
 
100
95
  it "should be many when type is :many" do
101
- Ripple::Document::Association.new(:many, :pages).should be_many
96
+ Ripple::Association.new(:many, :pages).should be_many
102
97
  end
103
98
 
104
99
  it "should be one when type is :one" do
105
- Ripple::Document::Association.new(:one, :pages).should be_one
100
+ Ripple::Association.new(:one, :pages).should be_one
106
101
  end
107
102
 
108
103
  it "should determine an instance variable based on the name" do
109
- Ripple::Document::Association.new(:many, :pages).ivar.should == "@_pages"
104
+ Ripple::Association.new(:many, :pages).ivar.should == "@_pages"
110
105
  end
111
- end
106
+ end
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
  require File.expand_path("../../spec_helper", __FILE__)
15
15
 
16
- describe Ripple::Document::AttributeMethods do
16
+ describe Ripple::AttributeMethods do
17
17
  require 'support/models/widget'
18
18
 
19
19
  before :each do
@@ -42,7 +42,6 @@ describe Ripple::Document::AttributeMethods do
42
42
  @widget.key = 10
43
43
  @widget.key.should == "10"
44
44
  end
45
-
46
45
  end
47
46
 
48
47
  describe "accessors" do
@@ -130,7 +129,6 @@ describe Ripple::Document::AttributeMethods do
130
129
  @widget.changes.should == {"name" => ["widget", "foobar"]}
131
130
  end
132
131
 
133
-
134
132
  it "should refresh the attribute methods when adding a new property" do
135
133
  Widget.should_receive(:undefine_attribute_methods)
136
134
  Widget.property :start_date, Date
@@ -157,13 +155,13 @@ describe Ripple::Document::AttributeMethods do
157
155
  @widget.changes.should be_blank
158
156
  end
159
157
 
160
- it "should allow adding to the @attributes hash for attributes that do no exist" do
158
+ it "should allow adding to the @attributes hash for attributes that do not exist" do
161
159
  @widget = Widget.new
162
160
  @widget['foo'] = 'bar'
163
161
  @widget.instance_eval { @attributes['foo'] }.should == 'bar'
164
162
  end
165
163
 
166
- it "should allow reading from the @attributes hash for attributes that do no exist" do
164
+ it "should allow reading from the @attributes hash for attributes that do not exist" do
167
165
  @widget = Widget.new
168
166
  @widget['foo'] = 'bar'
169
167
  @widget['foo'].should == 'bar'
@@ -173,5 +171,4 @@ describe Ripple::Document::AttributeMethods do
173
171
  @widget = Widget.new { |w| w.key = 'some-key' }
174
172
  @widget.key.should == 'some-key'
175
173
  end
176
-
177
174
  end
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
  require File.expand_path("../../spec_helper", __FILE__)
15
15
 
16
- describe Ripple::Document::Callbacks do
16
+ describe Ripple::Callbacks do
17
17
  require 'support/models/box'
18
18
 
19
19
  it "should add create, update, save, and destroy callback declarations" do
@@ -11,9 +11,9 @@
11
11
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
- require File.expand_path("../../../spec_helper", __FILE__)
14
+ require File.expand_path("../../spec_helper", __FILE__)
15
15
 
16
- describe Ripple::EmbeddedDocument::Conversion do
16
+ describe Ripple::Conversion do
17
17
  require 'support/models/address'
18
18
 
19
19
  before :each do
@@ -24,11 +24,11 @@ describe Ripple::EmbeddedDocument::Conversion do
24
24
  it "should return the key as an array for to_key" do
25
25
  @addr.to_key.should == ['some-key']
26
26
  end
27
-
27
+
28
28
  it "should be able to be converted to a param" do
29
29
  @addr.to_param.should == 'some-key'
30
30
  end
31
-
31
+
32
32
  it "should be able to be converted to a model" do
33
33
  @addr.to_model.should == @addr
34
34
  end
@@ -19,21 +19,9 @@ describe Ripple::EmbeddedDocument::Persistence do
19
19
 
20
20
  before :each do
21
21
  @root = User.new
22
- @addr = Address.new
22
+ @addr = Address.new(:street => "196 Broadway")
23
23
  @addr._parent_document = @root
24
24
  end
25
-
26
- it "should be embeddable if including Ripple::EmbeddedDocument" do
27
- @addr.should be_embeddable
28
- end
29
-
30
- it "should not be a root document if including Ripple::EmbeddedDocument" do
31
- @addr.should_not be__root_document
32
- end
33
-
34
- it "should be a root document if including Ripple::Document" do
35
- @root.should be__root_document
36
- end
37
25
 
38
26
  it "should delegate new? to the root document" do
39
27
  @root.should_receive(:new?).and_return(true)
@@ -46,17 +34,17 @@ describe Ripple::EmbeddedDocument::Persistence do
46
34
  end
47
35
 
48
36
  it "should delegate save! to the root document" do
49
- @root.should_receive(:save!).and_return(true)
37
+ @root.should_receive(:save).and_return(true)
50
38
  @addr.save!.should be_true
51
39
  end
52
40
 
53
41
  it "should raise NoRootDocument when calling save without a root document" do
54
- @addr = Address.new
42
+ @addr._parent_document = nil
55
43
  lambda { @addr.save }.should raise_error(Ripple::NoRootDocument)
56
44
  end
57
45
 
58
46
  it "should raise NoRootDocument when calling save! without a root document" do
59
- @addr = Address.new
47
+ @addr._parent_document = nil
60
48
  lambda { @addr.save! }.should raise_error(Ripple::NoRootDocument)
61
49
  end
62
50
 
@@ -13,7 +13,7 @@
13
13
  # limitations under the License.
14
14
  require File.expand_path("../../spec_helper", __FILE__)
15
15
 
16
- describe Ripple::Document::Properties do
16
+ describe Ripple::Properties do
17
17
  require 'support/models/email'
18
18
 
19
19
  it "should make the model class have a property definition method" do
@@ -35,51 +35,50 @@ describe Ripple::Document::Properties do
35
35
  class Forward < Email; end
36
36
  Forward.properties[:foo].should == "bar"
37
37
  end
38
-
39
38
  end
40
39
 
41
- describe Ripple::Document::Property do
40
+ describe Ripple::Property do
42
41
  it "should have a key symbol" do
43
- prop = Ripple::Document::Property.new('foo', String)
42
+ prop = Ripple::Property.new('foo', String)
44
43
  prop.should respond_to(:key)
45
44
  prop.key.should == :foo
46
45
  end
47
46
 
48
47
  it "should have a type" do
49
- prop = Ripple::Document::Property.new('foo', String)
48
+ prop = Ripple::Property.new('foo', String)
50
49
  prop.should respond_to(:type)
51
50
  prop.type.should == String
52
51
  end
53
52
 
54
53
  it "should capture extra options" do
55
- prop = Ripple::Document::Property.new('foo', String, 'default' => "bar")
54
+ prop = Ripple::Property.new('foo', String, 'default' => "bar")
56
55
  prop.should respond_to(:options)
57
56
  prop.options.should == {:default => "bar"}
58
57
  end
59
58
 
60
59
  it "should expose validation options" do
61
- prop = Ripple::Document::Property.new('foo', String, 'default' => "bar", :presence => true)
60
+ prop = Ripple::Property.new('foo', String, 'default' => "bar", :presence => true)
62
61
  prop.validation_options.should == {:presence => true}
63
62
  end
64
63
 
65
64
  describe "default value" do
66
65
  it "should be nil when not specified" do
67
- prop = Ripple::Document::Property.new('foo', String)
66
+ prop = Ripple::Property.new('foo', String)
68
67
  prop.default.should be_nil
69
68
  end
70
69
 
71
70
  it "should allow literal values" do
72
- prop = Ripple::Document::Property.new('foo', String, :default => "bar")
71
+ prop = Ripple::Property.new('foo', String, :default => "bar")
73
72
  prop.default.should == "bar"
74
73
  end
75
74
 
76
75
  it "should cast to the proper type" do
77
- prop = Ripple::Document::Property.new('foo', String, :default => :bar)
76
+ prop = Ripple::Property.new('foo', String, :default => :bar)
78
77
  prop.default.should == "bar"
79
78
  end
80
79
 
81
80
  it "should allow lambdas for deferred evaluation" do
82
- prop = Ripple::Document::Property.new('foo', String, :default => lambda { "bar" })
81
+ prop = Ripple::Property.new('foo', String, :default => lambda { "bar" })
83
82
  prop.default.should == "bar"
84
83
  end
85
84
  end
@@ -87,7 +86,7 @@ describe Ripple::Document::Property do
87
86
  describe "casting a value" do
88
87
  describe "when type is Boolean" do
89
88
  before :each do
90
- @prop = Ripple::Document::Property.new('foo', Boolean)
89
+ @prop = Ripple::Property.new('foo', Boolean)
91
90
  end
92
91
 
93
92
  [0, 0.0, "", [], false, "f", "FALSE"].each do |v|
@@ -109,7 +108,7 @@ describe Ripple::Document::Property do
109
108
 
110
109
  describe "when type is String" do
111
110
  before :each do
112
- @prop = Ripple::Document::Property.new('foo', String)
111
+ @prop = Ripple::Property.new('foo', String)
113
112
  end
114
113
 
115
114
  it "should cast anything to a string using to_s" do
@@ -126,7 +125,7 @@ describe Ripple::Document::Property do
126
125
 
127
126
  describe "when type is an Integer type" do
128
127
  before :each do
129
- @prop = Ripple::Document::Property.new(:foo, Integer)
128
+ @prop = Ripple::Property.new(:foo, Integer)
130
129
  end
131
130
 
132
131
  [5.0, "5", " 5", "05", Rational(10,2)].each do |v|
@@ -150,7 +149,7 @@ describe Ripple::Document::Property do
150
149
 
151
150
  describe "when type is a Float type" do
152
151
  before :each do
153
- @prop = Ripple::Document::Property.new(:foo, Float)
152
+ @prop = Ripple::Property.new(:foo, Float)
154
153
  end
155
154
 
156
155
  [0, "0", "0.0", " 0.0", ""].each do |v|
@@ -174,7 +173,7 @@ describe Ripple::Document::Property do
174
173
 
175
174
  describe "when type is a Numeric type" do
176
175
  before :each do
177
- @prop = Ripple::Document::Property.new(:foo, Numeric)
176
+ @prop = Ripple::Property.new(:foo, Numeric)
178
177
  end
179
178
 
180
179
  [5.0, "5", " 5.0", "05"].each do |v|
@@ -192,7 +191,7 @@ describe Ripple::Document::Property do
192
191
 
193
192
  describe "when type is a Time type" do
194
193
  before :each do
195
- @prop = Ripple::Document::Property.new(:foo, Time)
194
+ @prop = Ripple::Property.new(:foo, Time)
196
195
  end
197
196
 
198
197
  ["Tue, 16 Mar 2010 12:00:00 -0000","2010/03/16 12:00:00 GMT", Time.utc(2010,03,16,12)].each do |v|
@@ -204,7 +203,7 @@ describe Ripple::Document::Property do
204
203
 
205
204
  describe "when type is a Date type" do
206
205
  before :each do
207
- @prop = Ripple::Document::Property.new(:foo, Date)
206
+ @prop = Ripple::Property.new(:foo, Date)
208
207
  end
209
208
 
210
209
  ["Tue, 16 Mar 2010 00:00:00 -0000", "2010/03/16 12:00:00 GMT", Time.utc(2010,03,16,12), "2010/03/16"].each do |v|