duck_record 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ module DuckRecord
2
+ # = Active Record Has Many Association
3
+ module Associations
4
+ # This is the proxy that handles a has many association.
5
+ #
6
+ # If the association has a <tt>:through</tt> option further specialization
7
+ # is provided by its child HasManyThroughAssociation.
8
+ class HasManyAssociation < CollectionAssociation #:nodoc:
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module DuckRecord
2
+ # = Active Record Has One Association
3
+ module Associations
4
+ class HasOneAssociation < SingularAssociation #:nodoc:
5
+ def replace(record)
6
+ raise_on_type_mismatch!(record) if record
7
+
8
+ self.target = record
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,39 @@
1
+ module DuckRecord
2
+ module Associations
3
+ class SingularAssociation < Association #:nodoc:
4
+ # Implements the reader method, e.g. foo.bar for Foo.has_one :bar
5
+ def reader
6
+ target
7
+ end
8
+
9
+ # Implements the writer method, e.g. foo.bar= for Foo.belongs_to :bar
10
+ def writer(record)
11
+ replace(record)
12
+ end
13
+
14
+ def build(attributes = {})
15
+ record = build_record(attributes)
16
+ yield(record) if block_given?
17
+ set_new_record(record)
18
+ record
19
+ end
20
+
21
+ # Implements the reload reader method, e.g. foo.reload_bar for
22
+ # Foo.has_one :bar
23
+ def force_reload_reader
24
+ klass.uncached { reload }
25
+ target
26
+ end
27
+
28
+ private
29
+
30
+ def replace(_record)
31
+ raise NotImplementedError, "Subclasses must implement a replace(record) method"
32
+ end
33
+
34
+ def set_new_record(record)
35
+ replace(record)
36
+ end
37
+ end
38
+ end
39
+ end