appoxy-simple_record 1.1.11 → 1.1.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.markdown +176 -7
  2. data/lib/simple_record.rb +18 -10
  3. metadata +2 -2
data/README.markdown CHANGED
@@ -1,15 +1,184 @@
1
- # simple_record
2
-
3
- http://code.google.com/p/simple-record/
4
-
5
- ## DESCRIPTION:
1
+ # SimpleRecord - ActiveRecord for SimpleDB
6
2
 
7
3
  An ActiveRecord interface for SimpleDB. Can be used as a drop in replacement for ActiveRecord in rails.
8
4
 
9
5
  Special thanks to Garrett Cox for creating Activerecord2sdb which SimpleRecord is based on:
10
6
  http://activrecord2sdb.rubyforge.org/
11
7
 
12
- ## Usage
8
+ ## Getting Started
9
+
10
+ 1. Install gems
11
+
12
+ gem install appoxy-aws uuidtools appoxy-simple_record
13
+
14
+ 2. Create a model
15
+
16
+ require 'simple_record'
17
+
18
+ class MyModel < SimpleRecord::Base
19
+ has_attributes :name
20
+ has_ints :age
21
+ end
22
+
23
+ More about ModelAttributes below.
24
+
25
+ 3. Setup environment
26
+
27
+ AWS_ACCESS_KEY_ID='XXXX'
28
+ AWS_SECRET_ACCESS_KEY='YYYY'
29
+ SimpleRecord.establish_connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
30
+
31
+ 4. Go to town
32
+
33
+ # Store a model object to SimpleDB
34
+ mm = MyModel.new
35
+ mm.name = "Travis"
36
+ mm.age = 32
37
+ mm.save
38
+ id = mm.id
39
+
40
+ # Get an object from SimpleDB
41
+ mm2 = MyModel.find(id)
42
+ puts 'got=' + mm2.name + ' and he/she is ' + mm.age.to_s + ' years old'
43
+ # Or more advanced queries? mms = MyModel?.find(:all, ["age=?", 32], :order=>"name", :limit=>10)
44
+
45
+
46
+ ## Attributes and modifiers for models
47
+
48
+ NOTE: All objects will automatically have :id, :created, :updated attributes.
49
+
50
+ ### has_attributes
51
+
52
+ Add string attributes.
53
+
54
+ class MyModel
55
+ has_attributes :name
56
+ end
57
+
58
+ ### has_ints, has_dates and has_booleans
59
+
60
+ Lets simple_record know that certain attributes defined in has_attributes should be treated as integers, dates or booleans. This is required because SimpleDB only has strings so SimpleRecord needs to know how to convert, pad, offset, etc.
61
+
62
+ class MyModel
63
+ has_attributes :name
64
+ has_ints :age, :height
65
+ has_dates :birthday
66
+ has_booleans :is_nerd
67
+ end
68
+
69
+ ### belongs_to
70
+
71
+ Creates a many-to-one relationship. Can only have one per belongs_to call.
72
+
73
+ class MyModel
74
+ belongs_to :school
75
+ has_attributes :name
76
+ has_ints :age, :height
77
+ has_dates :birthday
78
+ has_booleans :is_nerd
79
+ end
80
+
81
+ Which requires another class called 'School' or you can specify the class explicitly with:
82
+
83
+ belongs_to :school, :class_name => "Institution"
84
+
85
+ ### set_table_name or set_domain_name
86
+
87
+ If you want to use a custom domain for a model object, you can specify it with set_table_name (or set_domain_name).
88
+
89
+ class SomeModel
90
+ set_table_name :different_model
91
+ end
92
+
93
+
94
+ ## Configuration
95
+
96
+ ### Domain Prefix
97
+
98
+ To set a global prefix across all your models, use:
99
+
100
+ SimpleRecord::Base.set_domain_prefix("myprefix_")
101
+
102
+ ### Connection Modes
103
+
104
+ There are 4 different connection modes:
105
+
106
+ * per_request (default) - opens and closes a new connection to simpledb for every simpledb request. Not the best performance, but it's safe and can handle many concurrent requests at the same time (unlike single mode).
107
+ * single - one connection across the entire application, not recommended unless the app is used by a single person.
108
+ * per_thread - a connection is used for every thread in the application. This is good, but the catch is that you have to ensure to close the connection.
109
+ * pool - NOT IMPLEMENTED YET - opens a maximum number of connections and round robins them for any simpledb request.
110
+
111
+ You set the mode when you call establish_connection:
112
+
113
+ SimpleRecord.establish_connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY, :connection_mode=>:per_thread)
114
+
115
+ We recommend per_thread with explicitly closing the connection after each Rails request (not to be mistaken for a SimpleDB request) or pool for rails apps.
116
+
117
+ For rails, be sure to add this to your Application controller if using per_thread mode:
118
+
119
+ after_filter :close_sdb_connection
120
+
121
+ def close_sdb_connection
122
+ SimpleRecord.close_connection
123
+ end
124
+
125
+ ## SimpleRecord on Rails
126
+
127
+ You don't really have to do anything except have your models extends SimpleRecord::Base instead of ActiveRecord::Base, but here are some tips you can use.
128
+
129
+ ### Change Connection Mode
130
+
131
+ Use per_thread connection mode and close the connection after each request.
132
+
133
+ after_filter :close_sdb_connection
134
+
135
+ def close_sdb_connection
136
+ SimpleRecord.close_connection
137
+ end
138
+
139
+ ### Disable ActiveRecord so you don't have to setup another database
140
+
141
+ This is most helpful on windows so Rails doesn't need sqlite or mysql gems/drivers installed which are painful to install on windows. In environment.rb, add 'config.frameworks -= [ :active_record ]', should look something like:
142
+
143
+ Rails::Initializer.run do |config|
144
+ config.frameworks -= [ :active_record ]
145
+ ....
146
+ end
147
+
148
+
149
+ ## Tips and Tricks and Things to Know
150
+
151
+ ### Automagic Stuff
152
+
153
+
154
+ #### Automatic common fields
155
+
156
+ Every object will automatically get the following attributes so you don't need to define them:
157
+
158
+ * id - UUID string
159
+ * created - set when first save
160
+ * updated - set every time you save/update
161
+
162
+
163
+ #### belongs_to foreign keys/IDs are accessible without touching the database
164
+
165
+ If you had the following in your model:
166
+
167
+ belongs_to :something
168
+
169
+ Then in addition to being able to access the something object with:
170
+
171
+ o.something
172
+
173
+ or setting it with:
174
+
175
+ o.something = someo
176
+
177
+ You can also access the ID for something directly with:
178
+
179
+ o.something_id
180
+
181
+ or
13
182
 
14
- See http://code.google.com/p/simple-record/ for documentation.
183
+ o.something_id = x
15
184
 
data/lib/simple_record.rb CHANGED
@@ -186,8 +186,11 @@ module SimpleRecord
186
186
 
187
187
 
188
188
  attr_accessor :errors
189
- @@domain_prefix = ''
190
- @domain_name_for_class = nil
189
+
190
+ @domain_prefix = ''
191
+ class << self; attr_accessor :domain_prefix; end
192
+
193
+ #@domain_name_for_class = nil
191
194
 
192
195
  @@cache_store = nil
193
196
  # Set the cache to use
@@ -200,7 +203,8 @@ module SimpleRecord
200
203
 
201
204
  # If you want a domain prefix for all your models, set it here.
202
205
  def self.set_domain_prefix(prefix)
203
- @@domain_prefix = prefix
206
+ #puts 'set_domain_prefix=' + prefix
207
+ self.domain_prefix = prefix
204
208
  end
205
209
 
206
210
  # Same as set_table_name
@@ -211,25 +215,29 @@ module SimpleRecord
211
215
  # Sets the domain name for this class
212
216
  def self.set_domain_name(table_name)
213
217
  # puts 'setting domain name for class ' + self.inspect + '=' + table_name
214
- @domain_name_for_class = table_name
218
+ #@domain_name_for_class = table_name
215
219
  super
216
220
  end
217
221
 
218
- def self.get_domain_name
222
+ =begin
223
+ def self.get_domain_name
219
224
  # puts 'returning domain_name=' + @domain_name_for_class.to_s
220
- return @domain_name_for_class
225
+ #return @domain_name_for_class
226
+ return self.domain
221
227
  end
222
228
 
229
+ =end
223
230
 
224
231
  def domain
225
232
  super # super.domain
226
233
  end
227
234
 
228
235
  def self.domain
229
- return self.get_domain_name unless self.get_domain_name.nil?
236
+ #return self.get_domain_name unless self.get_domain_name.nil?
230
237
  d = super
231
- domain_name_for_class = @@domain_prefix + d.to_s
232
- self.set_domain_name(domain_name_for_class)
238
+ #puts 'in self.domain, d=' + d.to_s + ' domain_prefix=' + SimpleRecord::Base.domain_prefix.to_s
239
+ domain_name_for_class = SimpleRecord::Base.domain_prefix + d.to_s
240
+ #self.set_domain_name(domain_name_for_class)
233
241
  domain_name_for_class
234
242
  end
235
243
 
@@ -916,7 +924,7 @@ module SimpleRecord
916
924
  end
917
925
 
918
926
  def self.table_name
919
- return @@domain_prefix + self.class.name.tableize
927
+ return domain
920
928
  end
921
929
 
922
930
  def changed
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appoxy-simple_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.11
4
+ version: 1.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-08-04 00:00:00 -07:00
13
+ date: 2009-08-18 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16