Permalink
Browse files

Practicing Ruby v4i6.

  • Loading branch information...
1 parent 7863b05 commit 0c79e2806383259088c62f32a80287b1d428e1b2 @bjhess committed Aug 14, 2012
Showing with 34 additions and 0 deletions.
  1. +34 −0 practicing_ruby/v04i06-persisting_relations.md
@@ -0,0 +1,34 @@
+### [Persisting Relations](http://practicingruby.com/articles/shared/kzosgsxjjbar)
+
+There was a lot of stuff in here, simultaneously too much and too little information. So my main takeaway was the existence of [`PStore`](http://ruby-doc.org/stdlib-1.9.3/libdoc/pstore/rdoc/PStore.html), Ruby's built-in persistence mechanism. The example uses `YAML::Store`:
+
+```ruby
+require 'yaml/store'
+store = YAML::Store.new 'quotes.yml'
+
+# quotes are author + text structures
+Quote = Struct.new :author, :text
+
+store.transaction do # a read/write transaction...
+ store['db'] ||= []
+ store['db'] << Quote.new('Charlie Gibbs',
+ 'A database is a black hole into which you put your data.')
+ store['db'] << Quote.new('Will Jessop',
+ 'MySQL is truly the PHP of the database world.')
+end # ...is atomically committed here
+```
+
+`quotes.yml`:
+
+```yaml
+---
+db:
+- !ruby/struct:Quote
+ author: Charlie Gibbs
+ text: A database is a black hole into which you put your data.
+- !ruby/struct:Quote
+ author: Will Jessop
+ text: MySQL is truly the PHP of the database world.
+```
+
+An interesting option for small, especially local, apps.

0 comments on commit 0c79e28

Please sign in to comment.