simple_record 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
File without changes
data/Manifest.txt CHANGED
File without changes
data/README.txt CHANGED
File without changes
data/Rakefile CHANGED
File without changes
data/bin/simple_record CHANGED
File without changes
data/lib/simple_record.rb CHANGED
@@ -29,7 +29,7 @@ require 'local_cache'
29
29
 
30
30
  module SimpleRecord
31
31
 
32
- VERSION = '1.0.5'
32
+ VERSION = '1.0.6'
33
33
 
34
34
  class Base < RightAws::ActiveSdb::Base
35
35
 
@@ -69,8 +69,6 @@ module SimpleRecord
69
69
  end
70
70
 
71
71
 
72
- # @@domain_name_for_class = nil
73
-
74
72
  def domain
75
73
  super # super.domain
76
74
  end
@@ -113,6 +111,7 @@ module SimpleRecord
113
111
  def self.has_attributes(*args)
114
112
  @@attributes = args
115
113
  args.each do |arg|
114
+ # define reader method
116
115
  send :define_method, arg do
117
116
  ret = nil
118
117
  if self[arg.to_s].class==Array
@@ -128,6 +127,7 @@ module SimpleRecord
128
127
  return un_offset_if_int(arg, ret)
129
128
  end
130
129
 
130
+ # define writer method
131
131
  method_name = (arg.to_s+"=")
132
132
  send(:define_method, method_name) do |value|
133
133
  self[arg.to_s]=value# end
@@ -173,21 +173,23 @@ module SimpleRecord
173
173
 
174
174
  @@belongs_to_map = {}
175
175
  # One belongs_to association per call. Call multiple times if there are more than one.
176
+ #
177
+ # This method will also create an {association)_id method that will return the ID of the foreign object
178
+ # without actually materializing it.
176
179
  def self.belongs_to(association_id, options = {})
177
180
  @@belongs_to_map[association_id] = options
178
181
  arg = association_id
182
+ arg_id = arg.to_s + '_id'
179
183
 
180
184
  # todo: should also handle foreign_key http://74.125.95.132/search?q=cache:KqLkxuXiBBQJ:wiki.rubyonrails.org/rails/show/belongs_to+rails+belongs_to&hl=en&ct=clnk&cd=1&gl=us
181
185
  # puts "arg_id=#{arg}_id"
182
186
  # puts "is defined? " + eval("(defined? #{arg}_id)").to_s
183
187
  # puts 'atts=' + @attributes.inspect
188
+
189
+ # Define reader method
184
190
  send(:define_method, arg) do
185
191
  options2 = @@belongs_to_map[arg]
186
192
  class_name = options2[:class_name] || arg.to_s[0...1].capitalize + arg.to_s[1...arg.to_s.length]
187
- # return eval("#{arg.to_s.capitalize}.find(self['#{arg}_id']) if !self['#{arg}_id'].nil?") # (defined? #{arg}_id)
188
- # return eval("#{arg.to_s.capitalize}.find(#{arg}_id) if(defined? #{arg}_id)")
189
- # ORIGINAL return eval("#{arg.to_s.capitalize}.find(#{arg}_id)")
190
-
191
193
  # puts "attr=" + @attributes[arg_id].inspect
192
194
  # puts 'val=' + @attributes[arg_id][0].inspect unless @attributes[arg_id].nil?
193
195
  ret = nil
@@ -218,7 +220,17 @@ module SimpleRecord
218
220
  # puts 'ret=' + ret.inspect
219
221
  return ret
220
222
  end
221
- send(:define_method, arg.to_s+"=") do |value|
223
+
224
+ # Define reader ID method
225
+ send(:define_method, arg_id) do
226
+ if !@attributes[arg_id].nil? && @attributes[arg_id].size > 0 && @attributes[arg_id][0] != nil && @attributes[arg_id][0] != ''
227
+ return @attributes[arg_id][0]
228
+ end
229
+ return nil
230
+ end
231
+
232
+ # Define writer method
233
+ send(:define_method, arg.to_s + "=") do |value|
222
234
  arg_id = arg.to_s + '_id'
223
235
  if value.nil?
224
236
  self[arg_id]=nil unless self[arg_id].nil? # if it went from something to nil, then we have to remember and remove attribute on save
@@ -226,6 +238,7 @@ module SimpleRecord
226
238
  self[arg_id]=value.id
227
239
  end
228
240
  end
241
+
229
242
  send(:define_method, "create_"+arg.to_s) do |*params|
230
243
  newsubrecord=eval(arg.to_s.classify).new(*params)
231
244
  newsubrecord.save
@@ -234,56 +247,6 @@ module SimpleRecord
234
247
  end
235
248
  end
236
249
 
237
- def self.belongs_to_OLD_and_wrong(*args)
238
- #create the accesor functions
239
- args.each do |arg|
240
- # puts 'belongs_to ' + arg.to_s
241
- send(:define_method, arg) do
242
- # return eval("#{arg.to_s.capitalize}.find(self['#{arg}_id']) if !self['#{arg}_id'].nil?") # (defined? #{arg}_id)
243
- # return eval("#{arg.to_s.capitalize}.find(#{arg}_id) if(defined? #{arg}_id)")
244
- # ORIGINAL return eval("#{arg.to_s.capitalize}.find(#{arg}_id)")
245
- puts "arg_id=#{arg}_id"
246
- # puts "is defined? " + eval("(defined? #{arg}_id)").to_s
247
- # puts 'atts=' + @attributes.inspect
248
- puts "attr=" + @attributes[arg.to_s + '_id'].inspect
249
- puts 'val=' + @attributes[arg.to_s + '_id'][0].inspect unless @attributes[arg.to_s + '_id'].nil?
250
- to_eval = "#{arg.to_s[0...1].capitalize + arg.to_s[1...arg.to_s.length]}.find(@attributes['#{arg}_id'][0], :auto_load=>true) unless @attributes['#{arg}_id'].nil?"
251
- puts 'to eval=' + to_eval
252
- ret = eval(to_eval) # (defined? #{arg}_id)
253
- puts 'ret=' + ret.inspect
254
- ret
255
- end
256
- end
257
-
258
- args.each do |arg|
259
- send(:define_method, arg.to_s+"=") do |value|
260
- self[arg.to_s+"_id"]=value.id
261
- end
262
- end
263
-
264
- #create the build_subrecord and creat_subrecord methods
265
- #args.each do |arg|
266
- # send(:define_method, "build_"+arg.to_s) do |*params|
267
- # self.arg.to_s=eval(arg.to_s.classify).new
268
- # end
269
- #end
270
-
271
- args.each do |arg|
272
- send(:define_method, "create_"+arg.to_s) do |*params|
273
- newsubrecord=eval(arg.to_s.classify).new(*params)
274
- newsubrecord.save
275
- self[arg.to_s+"_id"]=newsubrecord.id
276
- end
277
- end
278
- end #belongs_to
279
-
280
- # def self.has_many(*args)
281
- # args.each do |arg|
282
- # send(:define_method, arg) do
283
- # return eval(%{#{(arg.to_s).classify}.find(:all, :conditions => ["#{(self.class.name).tableize.singularize}_id = ?",id])})
284
- # end
285
- # end
286
- # end
287
250
  def self.has_many(*args)
288
251
  args.each do |arg|
289
252
  #okay, this creates an instance method with the pluralized name of the symbol passed to belongs_to
@@ -748,7 +711,6 @@ This is done on getters now
748
711
  results=super(*params)
749
712
  cache_results(results)
750
713
  rescue RightAws::AwsError, RightAws::ActiveSdb::ActiveSdbError
751
- puts "RESCUED: " + $!
752
714
  if ($!.message().index("NoSuchDomain") != nil)
753
715
  # this is ok
754
716
  elsif ($!.message() =~ @@regex_no_id)
@@ -1,97 +1,116 @@
1
- require "test/unit"
1
+ require 'minitest/unit'
2
2
  require File.expand_path(File.dirname(__FILE__) + "/../lib/simple_record")
3
3
  require "yaml"
4
4
  require 'right_aws'
5
5
  require 'my_model'
6
+ require 'my_child_model'
6
7
 
7
8
  class TestSimpleRecord < Test::Unit::TestCase
8
9
 
9
- def setup
10
- @config = YAML::load(File.read('test-config.yml'))
11
- puts 'akey=' + @config['amazon']['access_key']
12
- puts 'skey=' + @config['amazon']['secret_key']
13
- RightAws::ActiveSdb.establish_connection(@config['amazon']['access_key'], @config['amazon']['secret_key'])
14
- SimpleRecord::Base.set_domain_prefix("simplerecord_tests_")
15
- end
10
+ def setup
11
+ @config = YAML::load(File.read('test-config.yml'))
12
+ puts 'akey=' + @config['amazon']['access_key']
13
+ puts 'skey=' + @config['amazon']['secret_key']
14
+ RightAws::ActiveSdb.establish_connection(@config['amazon']['access_key'], @config['amazon']['secret_key'])
15
+ SimpleRecord::Base.set_domain_prefix("simplerecord_tests_")
16
+ end
17
+
18
+ def teardown
19
+ RightAws::ActiveSdb.close_connection()
20
+ end
21
+
22
+ def test_save_get
23
+ mm = MyModel.new
24
+ mm.name = "Travis"
25
+ mm.age = 32
26
+ mm.cool = true
27
+ mm.save
28
+ id = mm.id
29
+ puts 'id=' + id.to_s
30
+ # Get the object back
31
+ mm2 = MyModel.find(id)
32
+ puts 'got=' + mm2.name + ' and he/she is ' + mm2.age.to_s + ' years old and he/she is cool? ' + mm2.cool.to_s
33
+ puts mm2.cool.class.name
34
+ assert mm2.id == mm.id
35
+ assert mm2.age == mm.age
36
+ assert mm2.cool == mm.cool
37
+ end
38
+
39
+ def test_batch_save
40
+ items = []
41
+ mm = MyModel.new
42
+ mm.name = "Travis"
43
+ mm.age = 32
44
+ mm.cool = true
45
+ items << mm
46
+ mm = MyModel.new
47
+ mm.name = "Tritt"
48
+ mm.age = 44
49
+ mm.cool = false
50
+ items << mm
51
+ MyModel.batch_save(items)
52
+ items.each do |item|
53
+ puts 'id=' + item.id
54
+ new_item = MyModel.find(item.id)
55
+ puts 'new=' + new_item.inspect
56
+ assert item.id == new_item.id
57
+ assert item.name == new_item.name
58
+ assert item.cool == new_item.cool
59
+ end
60
+ end
16
61
 
17
- def teardown
18
- RightAws::ActiveSdb.close_connection()
19
- end
62
+ # Testing getting the association ID without materializing the obejct
63
+ def test_get_belongs_to_id
64
+ mm = MyModel.new
65
+ mm.name = "Parent"
66
+ mm.age = 55
67
+ mm.cool = true
68
+ mm.save
20
69
 
21
- def test_save_get
22
- mm = MyModel.new
23
- mm.name = "Travis"
24
- mm.age = 32
25
- mm.cool = true
26
- mm.save
27
- id = mm.id
28
- puts 'id=' + id.to_s
29
- # Get the object back
30
- mm2 = MyModel.find(id)
31
- puts 'got=' + mm2.name + ' and he/she is ' + mm2.age.to_s + ' years old and he/she is cool? ' + mm2.cool.to_s
32
- puts mm2.cool.class.name
33
- assert mm2.id == mm.id
34
- assert mm2.age == mm.age
35
- assert mm2.cool == mm.cool
36
- end
70
+ child = MyChildModel.new
71
+ child.name = "Child"
72
+ child.my_model = mm
73
+ child.save
37
74
 
38
- def test_batch_save
39
- items = []
40
- mm = MyModel.new
41
- mm.name = "Travis"
42
- mm.age = 32
43
- mm.cool = true
44
- items << mm
45
- mm = MyModel.new
46
- mm.name = "Tritt"
47
- mm.age = 44
48
- mm.cool = false
49
- items << mm
50
- MyModel.batch_save(items)
51
- items.each do |item|
52
- puts 'id=' + item.id
53
- new_item = MyModel.find(item.id)
54
- puts 'new=' + new_item.inspect
55
- assert item.id == new_item.id
56
- assert item.name == new_item.name
57
- assert item.cool == new_item.cool
75
+ child = MyChildModel.find(child.id)
76
+ puts child.my_model_id
77
+ assert child.my_model_id == mm.id
58
78
  end
59
- end
60
79
 
61
- def test_callbacks
62
- # these DO NOT work right now, all objects get all callbacks
63
- # I tried like this, seem to be getting somewhere.
64
- #
65
- # class << self;
66
- # @@callbacks.each do |callback|
67
- # #we first have to make an initialized array for each of the callbacks, to prevent problems if they are not called
68
- # puts 'setting callback ' + callback.to_s + ' on ' + self.inspect
69
- # eval %{
70
- #
71
- # # add the methods to the class
72
- # def #{callback}(*args)
73
- # args.each do |arg|
74
- # cb_names = self.instance_variable_get(:@#{callback}_names)
75
- # cb_names = [] if cb_names.nil?
76
- # cb_names << arg.to_s if cb_names.index(arg.to_s).nil?
77
- # self.instance_variable_set(:@#{callback}_names, cb_names)
78
- # end
79
- ## asdf @@#{callback}_names=args.map{|arg| arg.to_s}
80
- # end
81
- #
82
- # # now we run the methods in the callback array for this class
83
- #send :define_method, "run_#{callback}" do
84
- ## def run_#{callback}
85
- # cb_names = self.instance_variable_get(:@#{callback}_names)
86
- # cb_names.each { |name|
87
- # unless eval(name)
88
- # return false
89
- # end
90
- # }
91
- # return true
92
- # end
93
- # }
94
- # end
95
- # end
96
- end
80
+ def test_callbacks
81
+ # these DO NOT work right now, all objects get all callbacks
82
+ # I tried like this, seem to be getting somewhere.
83
+ #
84
+ # class << self;
85
+ # @@callbacks.each do |callback|
86
+ # #we first have to make an initialized array for each of the callbacks, to prevent problems if they are not called
87
+ # puts 'setting callback ' + callback.to_s + ' on ' + self.inspect
88
+ # eval %{
89
+ #
90
+ # # add the methods to the class
91
+ # def #{callback}(*args)
92
+ # args.each do |arg|
93
+ # cb_names = self.instance_variable_get(:@#{callback}_names)
94
+ # cb_names = [] if cb_names.nil?
95
+ # cb_names << arg.to_s if cb_names.index(arg.to_s).nil?
96
+ # self.instance_variable_set(:@#{callback}_names, cb_names)
97
+ # end
98
+ ## asdf @@#{callback}_names=args.map{|arg| arg.to_s}
99
+ # end
100
+ #
101
+ # # now we run the methods in the callback array for this class
102
+ #send :define_method, "run_#{callback}" do
103
+ ## def run_#{callback}
104
+ # cb_names = self.instance_variable_get(:@#{callback}_names)
105
+ # cb_names.each { |name|
106
+ # unless eval(name)
107
+ # return false
108
+ # end
109
+ # }
110
+ # return true
111
+ # end
112
+ # }
113
+ # end
114
+ # end
115
+ end
97
116
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-21 00:00:00 -07:00
12
+ date: 2009-06-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency