ocean-dynamo 0.4.2 → 0.4.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a28d5f280ebcb046fad6b812a8a4394ca2b64dfc
4
- data.tar.gz: fc0f313738efd5370163056bf2915b83645f53b7
3
+ metadata.gz: e62e3f71a4c942cf37151bdaa588ce917c4fd269
4
+ data.tar.gz: ec345bbecb5a322e74d196f5586e75997e717e24
5
5
  SHA512:
6
- metadata.gz: 0259caaebfa02274f8103695c741670d6190be669ecd1c0d92d8165ea1a058f628ae96072c946fc77fc6561a0d7bb1ba075c0773201e6d3f641a69bdfec1b70b
7
- data.tar.gz: 2b0108bbef441a611ebdaed44b76512b7ae62d75491fe0788d30f2d0ea1d5f0e80fa08a3c6a10547197d2f2640db9f7970f15e3e9e126c10ffbf778e4692b2ea
6
+ metadata.gz: 3ab10073d477aee182164feb1042cd5ea82dbe0aaac3db4ec5204f4d832b2a968d1799783ab662528be9472c12d382097f0cc3e915d5a3209a6eca4df8b96096
7
+ data.tar.gz: 4b75d7ac49d8647f4183825b21c8f20177fc8a8eb3603f7c06637b19ff295099be0ef0c65ae54c579a745027a64cf95044dc28cf45750bd3a335b2c23b888133
data/README.rdoc CHANGED
@@ -162,12 +162,10 @@ controllers. OceanDynamo implements much of the infrastructure of ActiveRecord;
162
162
  for instance, +read_attribute+, +write_attribute+, and much of the control logic and
163
163
  internal organisation.
164
164
 
165
- * +belongs_to+ now handles setting and retrieving the parent association correctly in
166
- all cases, including direct and mass assignment (+:master+, +:master_id+, etc).
167
- * +has_many=+ now destroys rather than deletes.
168
- * Got rid of the +fake_dynamo+ purge message.
169
- * Relations now take an optional reload arg, as in <tt>parent.children(true)</tt>.
170
-
165
+ * +has_many+ now destroys all children before destroying itself. It thus behaves like
166
+ <tt>dependent: :destroy</tt> was declared in ActiveRecord. Soon, +has_many+ will take
167
+ a <tt>dependent:</tt> value of <tt>:destroy</tt>, <tt>:delete</tt>, or <tt>:nullify</tt>,
168
+ for full compatibility.
171
169
 
172
170
  === Future milestones
173
171
 
@@ -247,9 +245,8 @@ NB: +aws.yml+ is excluded from source control. This allows you to enter your AWS
247
245
  safely. Note that +aws.yml.example+ is under source control: don't edit it.
248
246
 
249
247
  Make sure your have version 0.1.3 of the +fake_dynamo+ gem. It implements the +2011-12-05+ version
250
- of the DynamoDB API. We're not yet using the +2012-08-10+ version, as the +aws-sdk+ ruby gem
251
- doesn't fully support it. We'll make the change as soon as +aws-sdk+ is updated. Reportedly,
252
- it's in the works.
248
+ of the DynamoDB API. We're not using the +2012-08-10+ version, as the +aws-sdk+ ruby gem
249
+ doesn't fully support it.
253
250
 
254
251
  Next, start +fake_dynamo+:
255
252
 
@@ -15,7 +15,7 @@ module OceanDynamo
15
15
  module ClassMethods
16
16
 
17
17
  #
18
- #
18
+ # Class macro to define the +belongs_to+ relation.
19
19
  #
20
20
  def belongs_to(target) # :master, "master", Master
21
21
  target_attr = target.to_s.underscore # "master"
@@ -55,29 +55,15 @@ module OceanDynamo
55
55
  end"
56
56
 
57
57
  self.class_eval "def #{target_attr}=(value)
58
- if !value
59
- target_id = nil
60
- target = nil
61
- elsif !value.kind_of?(#{target_class})
62
- raise AssociationTypeMismatch, \"can't save a #\{value.class\} in a #{target_class} foreign key\"
63
- else
64
- target_id = value.hash_key
65
- target = value
66
- end
58
+ target, target_id = type_check_target(#{target_class}, value)
67
59
  write_attribute('#{target_attr_id}', target_id)
68
60
  @#{target_attr} = target
69
61
  value
70
62
  end"
71
63
 
72
64
  self.class_eval "def #{target_attr_id}=(value)
73
- if !value
74
- target_id = nil
75
- elsif value.is_a?(String)
76
- target_id = value
77
- else
78
- raise AssociationTypeMismatch, 'Foreign key #{target_attr_id} must be nil or a string'
79
- end
80
- write_attribute('#{target_attr_id}', target_id)
65
+ type_check_foreign_key('#{target_attr_id}', value)
66
+ write_attribute('#{target_attr_id}', value)
81
67
  @#{target_attr} = nil
82
68
  value
83
69
  end"
@@ -132,16 +118,17 @@ module OceanDynamo
132
118
  # Instance variables and methods
133
119
  #
134
120
  # ---------------------------------------------------------
121
+ # def initialize(attrs={})
122
+ # super
123
+ # end
135
124
 
136
- def initialize(attrs={})
137
- super
138
- end
139
125
 
140
126
 
141
127
  #
142
128
  # This is run by #initialize and by #assign_attributes to set the
143
129
  # association instance variables (@master, for instance) and their associated
144
- # attributes (such as master_id) from a given value.
130
+ # attributes (such as master_id) from one single given value such as :master
131
+ # and :master_id.
145
132
  #
146
133
  def set_belongs_to_association(attrs) # :nodoc:
147
134
  parent_class = self.class.belongs_to_class
@@ -157,5 +144,19 @@ module OceanDynamo
157
144
  fields[name][:target_class].find(v, consistent: true)
158
145
  end
159
146
 
147
+
148
+ def type_check_foreign_key(name, value)
149
+ return unless value
150
+ return if value.is_a?(String)
151
+ raise AssociationTypeMismatch, "Foreign key #{name} must be nil or a string"
152
+ end
153
+
154
+
155
+ def type_check_target(target_class, value)
156
+ return nil unless value
157
+ return [value, value.hash_key] if value.kind_of?(target_class)
158
+ raise AssociationTypeMismatch, "can't save a #{value.class} in a #{target_class} foreign key"
159
+ end
160
+
160
161
  end
161
162
  end
@@ -22,8 +22,14 @@ module OceanDynamo
22
22
  children_attr = children.to_s.underscore # "children"
23
23
  child_class = children_attr.singularize.camelize.constantize # Child
24
24
  register_relation(child_class, :has_many)
25
- attr_accessor children_attr
25
+
26
+ before_destroy do |p|
27
+ map_children(child_class, &:destroy)
28
+ true
29
+ end
30
+
26
31
  # Define accessors for instances
32
+ attr_accessor children_attr
27
33
  self.class_eval "def #{children_attr}(force_reload=false)
28
34
  @#{children_attr} = false if force_reload
29
35
  @#{children_attr} ||= read_children(#{child_class})
@@ -47,37 +53,6 @@ module OceanDynamo
47
53
  # ---------------------------------------------------------
48
54
 
49
55
 
50
- #
51
- # Reads all children of a has_many relation.
52
- #
53
- def read_children(child_class)
54
- if new_record?
55
- nil
56
- else
57
- result = Array.new
58
- _late_connect?
59
- child_items = child_class.dynamo_items
60
- child_items.query(hash_value: id, range_gte: "0") do |item_data|
61
- result << child_class.new._setup_from_dynamo(item_data)
62
- end
63
- result
64
- end
65
- end
66
-
67
-
68
- #
69
- # Write all children in the arg, which should be nil or an array.
70
- #
71
- def write_children(child_class, arg)
72
- return nil if arg.blank?
73
- raise AssociationTypeMismatch, "not an array or nil" if !arg.is_a?(Array)
74
- raise AssociationTypeMismatch, "an array element is not a #{child_class}" unless arg.all? { |m| m.is_a?(child_class) }
75
- # We now know that arg is an array containing only members of the child_class
76
- arg.each(&:save!)
77
- arg
78
- end
79
-
80
-
81
56
  #
82
57
  # Sets all has_many relations to nil.
83
58
  #
@@ -98,7 +73,7 @@ module OceanDynamo
98
73
  #
99
74
  # This version also writes back any relations. TODO: only
100
75
  # dirty relations should be persisted. Introduce a dirty flag.
101
- # Easiest done via an association proxy object.
76
+ # Easiest done via an collection proxy object.
102
77
  #
103
78
  def dynamo_persist(*) # :nodoc:
104
79
  result = super
@@ -119,5 +94,48 @@ module OceanDynamo
119
94
  result
120
95
  end
121
96
 
97
+
98
+ #
99
+ # Reads all children of a has_many relation.
100
+ #
101
+ def read_children(child_class) # :nodoc:
102
+ if new_record?
103
+ nil
104
+ else
105
+ result = Array.new
106
+ _late_connect?
107
+ child_items = child_class.dynamo_items
108
+ child_items.query(hash_value: id, range_gte: "0") do |item_data|
109
+ result << child_class.new._setup_from_dynamo(item_data)
110
+ end
111
+ result
112
+ end
113
+ end
114
+
115
+
116
+ #
117
+ # Write all children in the arg, which should be nil or an array.
118
+ #
119
+ def write_children(child_class, arg) # :nodoc:
120
+ return nil if arg.blank?
121
+ raise AssociationTypeMismatch, "not an array or nil" if !arg.is_a?(Array)
122
+ raise AssociationTypeMismatch, "an array element is not a #{child_class}" unless arg.all? { |m| m.is_a?(child_class) }
123
+ # We now know that arg is an array containing only members of the child_class
124
+ arg.each(&:save!)
125
+ arg
126
+ end
127
+
128
+
129
+ #
130
+ # Takes a block and yields each child to it. Batched for scalability.
131
+ #
132
+ def map_children(child_class)
133
+ return if new_record?
134
+ child_items = child_class.dynamo_items
135
+ child_items.query(hash_value: id, range_gte: "0", batch_size: 1000) do |item_data|
136
+ yield child_class.new._setup_from_dynamo(item_data)
137
+ end
138
+ end
139
+
122
140
  end
123
141
  end
@@ -77,9 +77,9 @@ module OceanDynamo
77
77
  end
78
78
 
79
79
 
80
- def dynamo_schema(*)
81
- super
82
- end
80
+ # def dynamo_schema(*)
81
+ # super
82
+ # end
83
83
 
84
84
 
85
85
  def destroyed?
@@ -319,17 +319,11 @@ module OceanDynamo
319
319
 
320
320
  def serialize_attribute(attribute, value, metadata=fields[attribute],
321
321
  target_class: metadata[:target_class],
322
- type: metadata[:type]
323
- )
322
+ type: metadata[:type])
324
323
  return nil if value == nil
325
- #value = value.id if value.kind_of?(target_class)
326
324
  case type
327
325
  when :reference
328
- raise DynamoError, ":reference must always have a :target_class" unless target_class
329
- return value if value.is_a?(String)
330
- # The next two lines should be superfluous now
331
- return value.id if value.kind_of?(target_class)
332
- raise AssociationTypeMismatch, "can't save a #{value.class} in a #{target_class} :reference"
326
+ value
333
327
  when :string
334
328
  return nil if ["", []].include?(value)
335
329
  value
@@ -24,11 +24,12 @@ module OceanDynamo
24
24
  rescue RecordNotFound
25
25
  nil
26
26
  end
27
+ alias find_by_id find_by_key
27
28
 
28
29
 
29
- def find_by_id(*args)
30
- find_by_key(*args)
31
- end
30
+ # def find_by_id(*args)
31
+ # find_by_key(*args)
32
+ # end
32
33
 
33
34
 
34
35
  #
@@ -1,3 +1,3 @@
1
1
  module OceanDynamo
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocean-dynamo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Bengtson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-27 00:00:00.000000000 Z
11
+ date: 2013-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk