public
Fork of ndemonner/stone
Description: Plug-and-play data persistence created for small Ruby web applications.
Homepage: http://stone.rubyforge.org
Clone URL: git://github.com/juretta/stone.git
stone / website / index.html
100644 411 lines (279 sloc) 18.329 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <link rel="stylesheet" href="stylesheets/screen.css" type="text/css" media="screen" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>
      Stone: Dead-Simple Data Persistence
  </title>
  <script src="javascripts/rounded_corners_lite.inc.js" type="text/javascript"></script>
<style>
 
</style>
  <script type="text/javascript">
    window.onload = function() {
      settings = {
          tl: { radius: 10 },
          tr: { radius: 10 },
          bl: { radius: 10 },
          br: { radius: 10 },
          antiAlias: true,
          autoPad: true,
          validTags: ["div"]
      }
      var versionBox = new curvyCorners(settings, document.getElementById("version"));
      versionBox.applyCornersToAll();
    }
  </script>
</head>
<body>
<div id="main">
 
    <h1>Stone: Dead-Simple Data Persistence</h1>
    <div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/stone"; return false'>
      <p>Get Version</p>
      <a href="http://rubyforge.org/projects/stone" class="numbers">0.1.2</a>
    </div>
    <h1>&#x2192; &#8216;stone&#8217;</h1>
 
 
  <p><img src="images/stone.png" alt="" /></p>
 
 
  <h2>Sections</h2>
 
 
  <p><a href="#what">What</a></p>
 
 
  <p><a href="#installing">Installing</a></p>
 
 
<a href="#running">Getting Up and Running</a>
  <ul>
  <li><a href="#rails">Rails</a></li>
    <li><a href="#merb">Merb</a></li>
    <li><a href="#everywhere">Everywhere Else</a></li>
  </ul>
 
 
<a href="#usage">Usage</a>
  <ul>
  <li><a href="#models">Models</a></li>
    <li><a href="#gppd">Get, Post, Put, and Delete</a></li>
  </ul>
 
 
  <p><a href="#word">A Word About Finding Stuff</a></p>
 
 
  <p><a href="#license">License</a></p>
 
 
  <p><a href="#contrib">Contributing</a></p>
 
 
<a href="#contact">Contact</a>
<br /><br />
  <h2 id="what">What</h2>
 
 
  <p>For small applications, a database can be overkill for storing your data in a consistent and
organized manner. Therefore, Stone was built to provide plug-and-play data persistence for
any application or framework. It is fast, and it is easy&#8230; therefore it is good.</p>
 
 
  <p>You can check out an application that uses Merb+Stone <a href="http://theprestigio.us">here.</a></p>
 
 
  <p>The source for that blog is <a href="http://github.com/ndemonner/written_in_stone/tree/master">here.</a></p>
 
 
  <h2 id="installing">Installing</h2>
 
 
<pre>sudo gem install stone</pre>
 
  <h2 id="running">Up and Running</h2>
 
 
  <h3 id="rails">Rails</h3>
 
 
  <ul>
  <li>Create a new file in <code>config/initializers</code> called <code>stone.rb</code></li>
  </ul>
 
 
  <ul>
  <li>Add the following code to your new file:
<pre class='syntax'>
  <span class="ident">require</span> <span class="punct">'</span><span class="string">stone</span><span class="punct">'</span>
  
  <span class="constant">Stone</span><span class="punct">.</span><span class="ident">start</span><span class="punct">(</span><span class="constant">Dir</span><span class="punct">.</span><span class="ident">pwd</span><span class="punct">,</span> <span class="constant">Dir</span><span class="punct">.</span><span class="ident">glob</span><span class="punct">(</span><span class="constant">File</span><span class="punct">.</span><span class="ident">join</span><span class="punct">(</span><span class="constant">Dir</span><span class="punct">.</span><span class="ident">pwd</span><span class="punct">,&quot;</span><span class="string">app/models/*</span><span class="punct">&quot;)),</span> <span class="symbol">:rails</span><span class="punct">)</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Open your <code>environment.rb</code> file and look for the following lines:
<pre class='syntax'>
  <span class="comment"># Skip frameworks you're not going to use (only works if using vendor/rails).</span>
  <span class="comment"># To use Rails without a database, you must remove the Active Record framework</span>
  <span class="comment"># config.frameworks -= [ :active_record, :active_resource, :action_mailer ]</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Uncomment the last of those lines, and leave only active_record,
so that it looks like this:
<pre class='syntax'>
  <span class="comment"># Skip frameworks you're not going to use (only works if using vendor/rails).</span>
  <span class="comment"># To use Rails without a database, you must remove the Active Record framework</span>
  <span class="ident">config</span><span class="punct">.</span><span class="ident">frameworks</span> <span class="punct">-=</span> <span class="punct">[</span> <span class="symbol">:active_record</span> <span class="punct">]</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Create some models! We&#8217;ll use an Author as an example:
<pre class='syntax'>
  stone-gen model Author name:string
</pre></li>
  </ul>
 
 
  <h3 id="merb">Merb</h3>
 
 
  <ul>
  <li>Add the following code to your <code>init.rb</code> file:
<pre class='syntax'>
  <span class="ident">dependency</span> <span class="punct">'</span><span class="string">stone</span><span class="punct">'</span>
  
  <span class="comment"># add this in the Merb::BootLoader.after_app_loads block</span>
  <span class="comment"># at the bottom of init.rb</span>
  <span class="constant">Stone</span><span class="punct">.</span><span class="ident">start</span><span class="punct">(</span><span class="constant">Dir</span><span class="punct">.</span><span class="ident">pwd</span><span class="punct">,</span> <span class="constant">Dir</span><span class="punct">.</span><span class="ident">glob</span><span class="punct">(</span><span class="constant">File</span><span class="punct">.</span><span class="ident">join</span><span class="punct">(</span><span class="constant">Dir</span><span class="punct">.</span><span class="ident">pwd</span><span class="punct">,&quot;</span><span class="string">app/models/*</span><span class="punct">&quot;)),</span> <span class="symbol">:merb</span><span class="punct">)</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Create some models! We&#8217;ll use an Author as an example:
<pre class='syntax'>
  stone-gen model Author name:string
</pre></li>
  </ul>
 
 
  <h3 id="everywhere">Everywhere Else</h3>
 
 
  <p>Stone was designed to provide easy data storage management for <strong>any</strong>
app, not just web ones.</p>
 
 
  <ul>
  <li>Wherever you load your app, add the following:
<pre class='syntax'>
  <span class="ident">require</span> <span class="punct">'</span><span class="string">stone</span><span class="punct">'</span>
  
  <span class="comment"># replace path/to/models/ with wherever your models/resources may be</span>
  <span class="constant">Stone</span><span class="punct">.</span><span class="ident">start</span><span class="punct">(</span><span class="constant">Dir</span><span class="punct">.</span><span class="ident">pwd</span><span class="punct">,</span> <span class="constant">Dir</span><span class="punct">.</span><span class="ident">glob</span><span class="punct">(</span><span class="constant">File</span><span class="punct">.</span><span class="ident">join</span><span class="punct">(</span><span class="constant">Dir</span><span class="punct">.</span><span class="ident">pwd</span><span class="punct">,&quot;</span><span class="string">path/to/models/*</span><span class="punct">&quot;)))</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Create some models! See <a href="#models">Models</a>.</li>
  </ul>
 
 
  <p>That&#8217;s it! Remember, there are no databases to install with Stone. Instead,
your data gets persisted to files within <code>myapp/datastore/</code>.</p>
 
 
  <h2 id="usage">Usage</h2>
 
 
  <h3 id="models">Model Files</h3>
 
 
  <p>Models are really easy to create. Here is an example:
<pre class='syntax'>
  <span class="keyword">class </span><span class="class">Person</span>
    <span class="ident">include</span> <span class="constant">Stone</span><span class="punct">::</span><span class="constant">Resource</span> <span class="comment"># gives you access to persistence methods</span>
    
    <span class="comment"># defines an attribute called &quot;name&quot; of type String</span>
    <span class="ident">field</span> <span class="symbol">:name</span><span class="punct">,</span> <span class="constant">String</span>
    
    <span class="comment"># defines an attribute called &quot;email&quot; of type String. </span>
    <span class="comment"># Since :unique =&gt; true is set, &quot;email&quot; must be unique.</span>
    <span class="ident">field</span> <span class="symbol">:email</span><span class="punct">,</span> <span class="constant">String</span><span class="punct">,</span> <span class="symbol">:unique</span> <span class="punct">=&gt;</span> <span class="constant">true</span>
    
    <span class="comment"># Notifies Stone of a relationship between People and Jewels.</span>
    <span class="comment"># This allows for @person.jewels retrieval.</span>
    <span class="ident">has_many</span> <span class="symbol">:jewels</span>
    
    <span class="comment"># both name and email must be present or errors will be added</span>
    <span class="ident">validates_presence_of</span> <span class="symbol">:name</span><span class="punct">,</span> <span class="symbol">:email</span>
  <span class="keyword">end</span>
    
</pre></p>
 
 
  <p>For more validation methods, see <a href="http://validatable.rubyforge.org/classes/Validatable/Macros.html">here</a>.</p>
 
 
  <h3 id="gppd">Get, Post, Put, and Delete</h3>
 
 
  <ul>
  <li>Getting
<pre class='syntax'>
  <span class="constant">Author</span><span class="punct">.</span><span class="ident">get</span><span class="punct">(</span><span class="ident">some_id</span><span class="punct">)</span> <span class="comment"># gets Author with some_id</span>
  <span class="constant">Author</span><span class="punct">[</span><span class="ident">some_id</span><span class="punct">]</span> <span class="comment"># same as get()</span>
  
  <span class="comment"># brings back first author whose name is &quot;Nick DeMonner&quot;</span>
  <span class="constant">Author</span><span class="punct">.</span><span class="ident">first</span><span class="punct">(</span><span class="symbol">:name</span> <span class="punct">=&gt;</span> <span class="punct">&quot;</span><span class="string">Nick DeMonner</span><span class="punct">&quot;)</span>
  
  <span class="comment"># brings back first author whose name contains &quot;Nick&quot;</span>
  <span class="constant">Author</span><span class="punct">.</span><span class="ident">first</span><span class="punct">(</span><span class="symbol">:name</span><span class="punct">.</span><span class="ident">includes</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">Nick</span><span class="punct">')</span>
  
  <span class="comment"># brings back all authors whose email contains &quot;gmail.com&quot;, and who </span>
  <span class="comment"># were created before today</span>
  <span class="constant">Author</span><span class="punct">.</span><span class="ident">all</span><span class="punct">(</span><span class="symbol">:email</span><span class="punct">.</span><span class="ident">includes</span> <span class="punct">=&gt;</span> <span class="punct">&quot;</span><span class="string">gmail.com</span><span class="punct">&quot;,</span> <span class="symbol">:created_at</span><span class="punct">.</span><span class="ident">lt</span> <span class="punct">=&gt;</span> <span class="constant">DateTime</span><span class="punct">.</span><span class="ident">now</span><span class="punct">)</span>
  
  <span class="comment"># brings back all Authors created before today, and orders them descending</span>
  <span class="comment"># from most recent to least</span>
  <span class="constant">Author</span><span class="punct">.</span><span class="ident">all</span><span class="punct">(</span><span class="symbol">:created_at</span><span class="punct">.</span><span class="ident">lt</span> <span class="punct">=&gt;</span> <span class="constant">DateTime</span><span class="punct">.</span><span class="ident">now</span><span class="punct">,</span> <span class="symbol">:order</span> <span class="punct">=&gt;</span> <span class="punct">{</span><span class="symbol">:created_at</span> <span class="punct">=&gt;</span> <span class="symbol">:desc</span><span class="punct">})</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Posting
<pre class='syntax'>
  <span class="ident">author</span> <span class="punct">=</span> <span class="constant">Author</span><span class="punct">.</span><span class="ident">new</span>
  <span class="ident">author</span><span class="punct">.</span><span class="ident">name</span> <span class="punct">=</span> <span class="punct">&quot;</span><span class="string">Nick DeMonner</span><span class="punct">&quot;</span>
  <span class="ident">author</span><span class="punct">.</span><span class="ident">save</span> <span class="comment"># calls Author.post after validations</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Putting
<pre class='syntax'>
  <span class="ident">author</span> <span class="punct">=</span> <span class="constant">Author</span><span class="punct">[</span><span class="ident">some_id</span><span class="punct">]</span> <span class="comment"># gets Author with id of some_id</span>
  
  <span class="comment"># updates name and calls Resource.save (which in turn calls Resource.put </span>
  <span class="comment"># because this resource is being updated, not created)</span>
  <span class="ident">author</span><span class="punct">.</span><span class="ident">update_attributes</span><span class="punct">(</span><span class="symbol">:name</span> <span class="punct">=&gt;</span> <span class="punct">&quot;</span><span class="string">Bob Bobberson</span><span class="punct">&quot;)</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Deleting
<pre class='syntax'>
  <span class="constant">Author</span><span class="punct">.</span><span class="ident">delete</span><span class="punct">(</span><span class="ident">some_id</span><span class="punct">)</span> <span class="comment"># deletes Author at some_id</span>
</pre></li>
  </ul>
 
 
  <h3 id="word">A Word About Finding Stuff</h3>
 
 
  <p>Stone uses a series of unique methods (à la DataMapper) to finding objects.
You may use the following methods when searching:</p>
 
 
  <ul>
  <li>Matches all objects whose <code>field</code> is greater
than <code>thing</code>
<pre class='syntax'>
  <span class="symbol">:field</span><span class="punct">.</span><span class="ident">gt</span> <span class="punct">=&gt;</span> <span class="ident">thing</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Matches all objects whose <code>field</code> is greater than
or equal to <code>thing</code>
<pre class='syntax'>
  <span class="symbol">:field</span><span class="punct">.</span><span class="ident">gte</span> <span class="punct">=&gt;</span> <span class="ident">thing</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Matches all objects whose <code>field</code> is less than <code>thing</code>
<pre class='syntax'>
  <span class="symbol">:field</span><span class="punct">.</span><span class="ident">lt</span> <span class="punct">=&gt;</span> <span class="ident">thing</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Matches all objects whose <code>field</code> is less than or
equal to <code>thing</code>
<pre class='syntax'>
  <span class="symbol">:field</span><span class="punct">.</span><span class="ident">lte</span> <span class="punct">=&gt;</span> <span class="ident">thing</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Matches all objects whose <code>field</code> includes <code>thing</code>
<pre class='syntax'>
  <span class="symbol">:field</span><span class="punct">.</span><span class="ident">includes</span> <span class="punct">=&gt;</span> <span class="ident">thing</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Matches all objects whose <code>field</code> is equal to <code>thing</code>
<pre class='syntax'>
  <span class="symbol">:field</span><span class="punct">.</span><span class="ident">equals</span> <span class="punct">=&gt;</span> <span class="ident">thing</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Matches all objects whose <code>field</code> Regexp matches <code>thing</code>
<pre class='syntax'>
  <span class="symbol">:field</span><span class="punct">.</span><span class="ident">matches</span> <span class="punct">=&gt;</span> <span class="ident">regexp</span>
</pre></li>
  </ul>
 
 
  <ul>
  <li>Matches all objects whose <code>field</code> does not equal <code>thing</code>
<pre class='syntax'>
  <span class="symbol">:field</span><span class="punct">.</span><span class="ident">not</span> <span class="punct">=&gt;</span> <span class="ident">thing</span>
</pre></li>
  </ul>
 
 
  <h2 id="license">License</h2>
 
 
  <p>This code is free to use under the terms of the <span class="caps">MIT</span> license.</p>
 
 
  <h2 id="contrib">Contributing</h2>
 
 
  <p><span class="caps">PLEASE</span>. Stone is extremely immature at this point&#8212;&#8220;stable&#8221; is
the watchword for the moment.</p>
 
 
  <ul>
  <li>You can grab the source using Git:
<pre class='syntax'>
  git clone git://github.com/ndemonner/stone.git
</pre></li>
  </ul>
 
 
  <p>The code is pretty well specced out, so make sure you don&#8217;t break anything
by running <code>rake ok</code> every once in a while.</p>
 
 
  <p>Please add specs for any new functionality you might add, and hit me up to
commit. =D</p>
 
 
  <h2 id="contact">Contact</h2>
 
 
  <p>Comments are welcome. Join me in the <span class="caps">IRC</span> channel #stone on freenode.</p>
    <p class="coda">
      <a href="http://theprestigio.us">Nick DeMonner</a>, 17th April 2008<br>
      Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
    </p>
</div>
 
<!-- insert site tracking codes here, like Google Urchin -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-2831521-3");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</body>
</html>