persistence 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/lib/persistence.rb +0 -1
  2. data/lib/persistence/adapter/abstract.rb +1 -1
  3. data/lib/persistence/adapter/abstract/enable_disable.rb +1 -1
  4. data/lib/persistence/adapter/abstract/primary_key/id_property_string.rb +1 -1
  5. data/lib/persistence/adapter/abstract/primary_key/simple.rb +1 -1
  6. data/lib/persistence/adapter/mock/bucket/index/index_interface.rb +1 -1
  7. data/lib/persistence/cursor/atomic.rb +2 -2
  8. data/lib/persistence/object.rb +1 -1
  9. data/lib/persistence/object/autodetermine.rb +5 -4
  10. data/lib/persistence/object/class_instance.rb +12 -8
  11. data/lib/persistence/object/complex.rb +4 -4
  12. data/lib/persistence/object/complex/array.rb +6 -4
  13. data/lib/persistence/object/complex/array/class_instance.rb +1 -1
  14. data/lib/persistence/object/complex/array/object_instance.rb +1 -1
  15. data/lib/persistence/object/complex/attributes/attributes_array.rb +1 -1
  16. data/lib/persistence/object/complex/hash.rb +6 -4
  17. data/lib/persistence/object/complex/hash/class_instance.rb +1 -1
  18. data/lib/persistence/object/complex/hash/object_instance.rb +1 -1
  19. data/lib/persistence/object/complex/object_instance.rb +3 -3
  20. data/lib/persistence/object/flat.rb +6 -5
  21. data/lib/persistence/object/flat/class_instance.rb +2 -2
  22. data/lib/persistence/object/flat/file.rb +4 -3
  23. data/lib/persistence/object/flat/file/class_instance.rb +23 -30
  24. data/lib/persistence/object/flat/file/file_persistence.rb +4 -53
  25. data/lib/persistence/object/flat/file/object_instance.rb +21 -42
  26. data/lib/persistence/object/index.rb +0 -1
  27. data/lib/persistence/object/index_hash.rb +1 -1
  28. data/lib/persistence/object/object_instance.rb +11 -11
  29. data/lib/persistence/object/parse_persistence_args.rb +2 -108
  30. data/lib/persistence/object/parse_persistence_args/class_instance.rb +124 -0
  31. data/lib/persistence/object/parse_persistence_args/object_instance.rb +124 -0
  32. data/lib/persistence/port/bucket/bucket_interface.rb +0 -18
  33. data/lib/persistence/port/port_interface.rb +1 -19
  34. data/lib/requires.rb +10 -9
  35. data/spec/persistence/object/complex/attributes_spec.rb +1 -140
  36. data/spec/persistence/object/flat/bignum_spec.rb +12 -4
  37. data/spec/persistence/object/flat/class_spec.rb +9 -4
  38. data/spec/persistence/object/flat/file/class_instance_spec.rb +0 -1
  39. data/spec/persistence/object/flat/file_spec.rb +6 -9
  40. data/spec/persistence/object/indexes/block_index_spec.rb +1 -1
  41. data/spec/persistence/object/parse_persistence_args/class_instance_spec.rb +61 -0
  42. data/spec/persistence/object/parse_persistence_args/object_instance_spec.rb +63 -0
  43. data/spec/persistence/object_spec.rb +48 -82
  44. data/spec/persistence/port/bucket/index/bucket_index_spec.rb +1 -0
  45. data/spec/{Persistence_spec.rb → persistence_spec.rb} +0 -0
  46. metadata +8 -5
  47. data/spec/persistence/object/parse_persistence_args_spec.rb +0 -65
@@ -42,22 +42,6 @@ module ::Persistence::Object::Flat::File::FilePersistence
42
42
  #
43
43
  attr_setting :persists_files_by_path? => :persist_files_by_path=
44
44
 
45
- #####################################
46
- # persists_file_paths_as_objects? #
47
- #####################################
48
-
49
- ###
50
- #
51
- # @method persists_file_paths_as_objects?
52
- #
53
- # Query whether File paths should be persisted as objects (rather than by strings).
54
- # Lookup chain is: File instance, File class, Persistence::Port::Bucket instance, Persistence::Port instance,
55
- # Persistence singleton.
56
- #
57
- # @return [true,false] Whether files should be persisted as objects rather than as strings.
58
- #
59
- attr_setting :persists_file_paths_as_objects? => :persist_file_paths_as_objects=
60
-
61
45
  #####################################
62
46
  # persists_file_paths_as_strings? #
63
47
  #####################################
@@ -72,7 +56,10 @@ module ::Persistence::Object::Flat::File::FilePersistence
72
56
  #
73
57
  # @return [true,false] Whether files should be persisted as strings rather than as objects.
74
58
  #
75
- attr_setting :persists_file_paths_as_strings? => :persist_file_paths_as_strings=
59
+ attr_setting :persists_file_paths_as_strings? => :persists_file_paths_as_strings=
60
+
61
+ alias_method( :persist_file_paths_as_strings?, :persists_file_paths_as_strings? )
62
+ alias_method( :persist_file_paths_as_strings=, :persists_file_paths_as_strings= )
76
63
 
77
64
  ############################
78
65
  # persist_files_by_path! #
@@ -107,41 +94,5 @@ module ::Persistence::Object::Flat::File::FilePersistence
107
94
  end
108
95
 
109
96
  alias_method( :persists_files_by_content!, :persist_files_by_content! )
110
-
111
- ####################################
112
- # persist_file_paths_as_objects! #
113
- ####################################
114
-
115
- ###
116
- # Declare that file paths should be persisted as objects (rather than strings).
117
- #
118
- def persist_file_paths_as_objects!
119
-
120
- self.persist_file_paths_as_objects = true
121
- self.persist_file_paths_as_strings = false
122
-
123
- return self
124
-
125
- end
126
-
127
- alias_method( :persists_file_paths_as_objects!, :persist_file_paths_as_objects! )
128
-
129
- ####################################
130
- # persist_file_paths_as_strings! #
131
- ####################################
132
-
133
- ###
134
- # Declare that file paths should be persisted as strings (rather than objects).
135
- #
136
- def persist_file_paths_as_strings!
137
-
138
- self.persist_file_paths_as_strings = true
139
- self.persist_file_paths_as_objects = false
140
-
141
- return self
142
-
143
- end
144
-
145
- alias_method( :persists_file_paths_as_strings!, :persist_file_paths_as_strings! )
146
97
 
147
98
  end
@@ -6,39 +6,36 @@ module ::Persistence::Object::Flat::File::ObjectInstance
6
6
 
7
7
  include ::Persistence::Object::Flat::File::FilePersistence
8
8
 
9
- ##############
10
- # persist! #
11
- ##############
9
+ ##############################
10
+ # persistence_hash_to_port #
11
+ ##############################
12
+
13
+ ###
14
+ # @private
15
+ #
16
+ # Generate hash representing object.
17
+ #
18
+ # @return [Hash] Hash representing information to reproduce object instance.
19
+ #
20
+ def persistence_hash_to_port
21
+
22
+ persistence_contents = nil
12
23
 
13
- def persist!( *args )
14
-
15
- starting_pos = self.pos
16
-
17
24
  # if we are persisting by file path
18
- persistence_instance = nil
19
25
  if persists_files_by_path?
20
- persistence_instance = ::Persistence::Object::Flat::File::Path.new( path )
26
+ persistence_contents = ::Persistence::Object::Flat::File::Path.new( path )
21
27
  # if we are persisting by file contents
22
28
  else
23
- persistence_instance = ::Persistence::Object::Flat::File::Contents.new( self.readlines.join )
29
+ starting_pos = self.pos
30
+ persistence_contents = ::Persistence::Object::Flat::File::Contents.new( self.readlines.join )
31
+ # rewind file to wherever we started
32
+ self.pos = starting_pos
24
33
  end
25
34
 
26
- persistence_instance.persistence_port = persistence_port
27
- persistence_instance.persistence_bucket = persistence_bucket
28
-
29
- # set new instance ID to current ID
30
- persistence_instance.persistence_id = persistence_id
31
-
32
- persistence_instance.persist!( *args )
33
-
34
- # if we got ID from persist! on our instance set our own ID to it
35
- self.persistence_id = persistence_instance.persistence_id
35
+ primary_key = persistence_bucket.primary_key_for_attribute_name( self, self.class.to_s )
36
36
 
37
- # rewind file to wherever we started
38
- self.pos = starting_pos
37
+ return { primary_key => persistence_contents }
39
38
 
40
- return self
41
-
42
39
  end
43
40
 
44
41
  ################################
@@ -77,24 +74,6 @@ module ::Persistence::Object::Flat::File::ObjectInstance
77
74
 
78
75
  end
79
76
 
80
- #####################################
81
- # persists_file_paths_as_objects? #
82
- #####################################
83
-
84
- def persists_file_paths_as_objects?
85
-
86
- persists_file_paths_as_objects = nil
87
-
88
- persists_file_paths_as_objects = super
89
-
90
- if persists_file_paths_as_objects.nil?
91
- persists_file_paths_as_objects = self.class.persists_file_paths_as_objects?
92
- end
93
-
94
- return persists_file_paths_as_objects
95
-
96
- end
97
-
98
77
  #####################################
99
78
  # persists_file_paths_as_strings? #
100
79
  #####################################
@@ -75,7 +75,6 @@ module ::Persistence::Object::Index
75
75
  # @return [true,false] Whether index permits duplicates.
76
76
  #
77
77
  attr_setting :permits_duplicates?
78
-
79
78
  self.permits_duplicates = false
80
79
 
81
80
  ###################
@@ -32,7 +32,7 @@ module ::Persistence::Object::IndexHash
32
32
  end
33
33
 
34
34
  child_index_instance = index_instance.class.new( nil, parent_bucket, nil, nil, nil, index_instance )
35
-
35
+
36
36
  return child_index_instance
37
37
 
38
38
  end
@@ -4,7 +4,7 @@
4
4
  #
5
5
  module ::Persistence::Object::ObjectInstance
6
6
 
7
- include ::Persistence::Object::ParsePersistenceArgs
7
+ include ::Persistence::Object::ParsePersistenceArgs::ObjectInstance
8
8
 
9
9
  include ::CascadingConfiguration::Setting
10
10
  include ::CascadingConfiguration::Hash
@@ -178,7 +178,7 @@ module ::Persistence::Object::ObjectInstance
178
178
  #
179
179
  def persist!( *args )
180
180
 
181
- index_instance, key, no_key = parse_args_for_index_value_no_value( args, false )
181
+ index_instance, key, no_key = parse_object_args_for_index_value_no_value( args, false )
182
182
 
183
183
  # call super to ensure object is persisted
184
184
  unless persistence_port
@@ -196,7 +196,7 @@ module ::Persistence::Object::ObjectInstance
196
196
  end
197
197
 
198
198
  # and make sure we have an index that permits arbitrary keys
199
- unless explicit_indexes[ index_instance.name ] == index_instance
199
+ unless self.class.explicit_indexes[ index_instance.name ] == index_instance
200
200
  raise ::Persistence::Exception::ExplicitIndexRequired,
201
201
  'Index ' + index_instance.name.to_s + ' was not declared as an explicit index '
202
202
  'and thus does not permit arbitrary keys.'
@@ -206,8 +206,8 @@ module ::Persistence::Object::ObjectInstance
206
206
 
207
207
  end
208
208
 
209
- unless block_indexes.empty?
210
- block_indexes.each do |this_index_name, this_block_index|
209
+ unless self.class.block_indexes.empty?
210
+ self.class.block_indexes.each do |this_index_name, this_block_index|
211
211
  this_block_index.index_object( self )
212
212
  end
213
213
  end
@@ -242,7 +242,7 @@ module ::Persistence::Object::ObjectInstance
242
242
  #
243
243
  def cease!
244
244
 
245
- indexes.each do |this_index_name, this_index|
245
+ self.class.indexes.each do |this_index_name, this_index|
246
246
  this_index.delete_keys_for_object_id!( persistence_id )
247
247
  end
248
248
 
@@ -266,7 +266,7 @@ module ::Persistence::Object::ObjectInstance
266
266
  #
267
267
  # @return [CompositingHash{Symbol,String=>Persistence::Object::Index::ExplicitIndex}]
268
268
  #
269
- attr_hash :explicit_indexes, ::Persistence::Object::IndexHash
269
+ attr_module_hash :explicit_indexes, ::Persistence::Object::IndexHash
270
270
 
271
271
  ###################
272
272
  # block_indexes #
@@ -280,7 +280,7 @@ module ::Persistence::Object::ObjectInstance
280
280
  #
281
281
  # @return [CompositingHash{Symbol,String=>Persistence::Object::Index::BlockIndex}]
282
282
  #
283
- attr_hash :block_indexes, ::Persistence::Object::IndexHash
283
+ attr_module_hash :block_indexes, ::Persistence::Object::IndexHash
284
284
 
285
285
  #############
286
286
  # indexes #
@@ -294,16 +294,16 @@ module ::Persistence::Object::ObjectInstance
294
294
  #
295
295
  # @return [CompositingHash{Symbol,String=>Persistence::Object::Index}]
296
296
  #
297
- attr_hash :indexes do
297
+ attr_module_hash :indexes do
298
298
 
299
299
  #======================#
300
300
  # child_pre_set_hook #
301
301
  #======================#
302
302
 
303
303
  def child_pre_set_hook( index_name, index_instance )
304
-
304
+
305
305
  child_instance = nil
306
-
306
+
307
307
  case index_instance
308
308
  when ::Persistence::Object::Index::ExplicitIndex
309
309
  child_instance = configuration_instance.explicit_indexes[ index_name ]
@@ -4,115 +4,9 @@
4
4
  #
5
5
  # Internal helper for parsing args of the format: method, method( global_id ), method( index_name, value ),
6
6
  # method( index_instance, value ), method( index_name => value ), method( index_instance => value ).
7
+ #
7
8
  module ::Persistence::Object::ParsePersistenceArgs
8
9
 
9
- #########################################
10
- # parse_args_for_index_value_no_value #
11
- #########################################
12
-
13
- ###
14
- # Parse *args for index, key_value, no_value.
15
- #
16
- # @param args [Array] An array of args of the format: method, method( global_id ), method( index_name, value ),
17
- # method( index_instance, value ), method( index_name => value ), method( index_instance => value ).
18
- #
19
- # @param require_value [true,false] Whether key value must be provided; will throw exception if true and
20
- # key value is not provided.
21
- #
22
- # @return [Array] Array containing index instance, key value, whether key value was provided.
23
- #
24
- def parse_args_for_index_value_no_value( args, require_value = false )
25
-
26
- # * nil
27
- # - Cursor to primary bucket
28
- # * :index
29
- # - Cursor to index
30
- # * :index => persistence_key
31
- # - Object(s) for indexed key value
32
-
33
- index = nil
34
- key_value = nil
35
- no_value = nil
36
- case args.count
37
-
38
- when 0
39
-
40
- no_value = true
41
- if require_value
42
- raise ::Persistence::Exception::KeyValueRequired,
43
- 'Key value required.'
44
- end
45
-
46
- when 1
47
-
48
- index_or_id = args[ 0 ]
49
- if index_or_id.is_a?( Symbol ) or
50
- index_or_id.is_a?( String )
51
-
52
- index = indexes[ index_or_id ]
53
- unless index
54
- raise ::Persistence::Exception::ExplicitIndexRequired,
55
- 'Explicit index ' + index_or_id.to_s + ' did not exist.'
56
- end
57
- no_value = true
58
-
59
- elsif index_or_id.respond_to?( :index_object )
60
-
61
- index = index_or_id
62
- no_value = true
63
-
64
- elsif index_or_id.is_a?( Hash )
65
-
66
- key_value = index_or_id.values[ 0 ]
67
- index_name = index_or_id.keys[ 0 ]
68
- if index_name.is_a?( Symbol ) or
69
- index_name.is_a?( String )
70
-
71
- index = indexes[ index_name ]
72
- unless index
73
- raise ::Persistence::Exception::ExplicitIndexRequired,
74
- 'Explicit index :' + index_name.to_s + ' did not exist.'
75
- end
76
-
77
- end
78
- no_value = false
79
-
80
- else
81
-
82
- # persistence_id - anything other than Symbol, String, Hash
83
- key_value = args[ 0 ]
84
- no_value = false
85
-
86
- end
87
-
88
- when 2
89
-
90
- index_or_name = args[ 0 ]
91
-
92
- index = index_or_name.respond_to?( :index_object ) ? index_or_name : indexes[ index_or_name ]
93
- key_value = args[ 1 ]
94
-
95
- if ! index and ! key_value
96
- raise ::Persistence::Exception::ExplicitIndexRequired,
97
- 'Index ' + index_or_name.to_s + ' did not exist.'
98
- end
99
- no_value = false
100
-
101
- else
102
-
103
- raise 'Unexpected arguments ' + args.inspect + '.'
104
-
105
- end
106
-
107
- # if we have a file we need to persist it like we would a sub-object
108
- if key_value.is_a?( File )
109
- key_value = process_file_key( key_value )
110
- end
111
-
112
- return index, key_value, no_value
113
-
114
- end
115
-
116
10
  ######################
117
11
  # process_file_key #
118
12
  ######################
@@ -130,7 +24,7 @@ module ::Persistence::Object::ParsePersistenceArgs
130
24
 
131
25
  processed_key = nil
132
26
 
133
- if persists_files_by_path?
27
+ if file_key.persists_files_by_path?
134
28
  processed_key = ::Persistence::Object::Flat::File::Path.new( file_key.path )
135
29
  else
136
30
  starting_pos = file_key.pos
@@ -0,0 +1,124 @@
1
+
2
+ ###
3
+ # @private
4
+ #
5
+ # Class-specific implementation for parsing persistence args.
6
+ #
7
+ # Internal helper for parsing args of the format: method, method( global_id ), method( index_name, value ),
8
+ # method( index_instance, value ), method( index_name => value ), method( index_instance => value ).
9
+ #
10
+ module ::Persistence::Object::ParsePersistenceArgs::ClassInstance
11
+
12
+ include ::Persistence::Object::ParsePersistenceArgs
13
+
14
+ ###############################################
15
+ # parse_class_args_for_index_value_no_value #
16
+ ###############################################
17
+
18
+ ###
19
+ # Parse *args for index, key_value, no_value.
20
+ #
21
+ # @param args [Array] An array of args of the format: method, method( global_id ), method( index_name, value ),
22
+ # method( index_instance, value ), method( index_name => value ), method( index_instance => value ).
23
+ #
24
+ # @param require_value [true,false] Whether key value must be provided; will throw exception if true and
25
+ # key value is not provided.
26
+ #
27
+ # @return [Array] Array containing index instance, key value, whether key value was provided.
28
+ #
29
+ def parse_class_args_for_index_value_no_value( args, require_value = false )
30
+
31
+ # * nil
32
+ # - Cursor to primary bucket
33
+ # * :index
34
+ # - Cursor to index
35
+ # * :index => persistence_key
36
+ # - Object(s) for indexed key value
37
+
38
+ index = nil
39
+ key_value = nil
40
+ no_value = nil
41
+ case args.count
42
+
43
+ when 0
44
+
45
+ no_value = true
46
+ if require_value
47
+ raise ::Persistence::Exception::KeyValueRequired,
48
+ 'Key value required.'
49
+ end
50
+
51
+ when 1
52
+
53
+ index_or_id = args[ 0 ]
54
+
55
+ case index_or_id
56
+
57
+ when ::Symbol, ::String
58
+
59
+ index = index( index_or_id, true )
60
+ no_value = true
61
+
62
+ when ::Hash
63
+
64
+ key_value = index_or_id.values[ 0 ]
65
+ index_name = index_or_id.keys[ 0 ]
66
+
67
+ case index_name
68
+
69
+ when ::Symbol, ::String
70
+
71
+ index = index( index_name, true )
72
+
73
+ end
74
+
75
+ no_value = false
76
+
77
+ else
78
+
79
+ if index_or_id.respond_to?( :index_object )
80
+
81
+ index = index_or_id
82
+ no_value = true
83
+
84
+ else
85
+
86
+ # persistence_id - anything other than Symbol, String, Hash
87
+ key_value = args[ 0 ]
88
+ no_value = false
89
+
90
+ end
91
+
92
+ end
93
+
94
+ when 2
95
+
96
+ index_or_name = args[ 0 ]
97
+
98
+
99
+ if index_or_name.respond_to?( :index_object )
100
+ index = index_or_name
101
+ elsif index_or_name
102
+ index = index( index_or_name, true )
103
+ end
104
+
105
+ key_value = args[ 1 ]
106
+
107
+ no_value = false
108
+
109
+ else
110
+
111
+ raise 'Unexpected arguments ' << args.inspect + '.'
112
+
113
+ end
114
+
115
+ # if we have a file we need to persist it like we would a sub-object
116
+ if key_value.is_a?( ::File )
117
+ key_value = process_file_key( key_value )
118
+ end
119
+
120
+ return index, key_value, no_value
121
+
122
+ end
123
+
124
+ end