~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to lib/yaml/store.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
require 'yaml'
5
5
require 'pstore'
6
6
 
 
7
# YAML::Store provides the same functionality as PStore, except it uses YAML
 
8
# to dump objects instead of Marshal.
 
9
#
 
10
# == Example
 
11
#
 
12
#   require 'yaml/store'
 
13
#
 
14
#   Person = Struct.new :first_name, :last_name
 
15
#
 
16
#   people = [Person.new("Bob", "Smith"), Person.new("Mary", "Johnson")]
 
17
#
 
18
#   store = YAML::Store.new "test.store"
 
19
#
 
20
#   store.transaction do
 
21
#     store["people"] = people
 
22
#     store["greeting"] = { "hello" => "world" }
 
23
#   end
 
24
#
 
25
# After running the above code, the contents of "test.store" will be:
 
26
#
 
27
#   ---
 
28
#   people:
 
29
#   - !ruby/struct:Person
 
30
#     first_name: Bob
 
31
#     last_name: Smith
 
32
#   - !ruby/struct:Person
 
33
#     first_name: Mary
 
34
#     last_name: Johnson
 
35
#   greeting:
 
36
#     hello: world
 
37
 
7
38
class YAML::Store < PStore
 
39
 
 
40
  # :call-seq:
 
41
  #   initialize( file_name, yaml_opts = {} )
 
42
  #
 
43
  # Creates a new YAML::Store object, which will store data in +file_name+.
 
44
  # If the file does not already exist, it will be created.
 
45
  #
 
46
  #
 
47
  # Options passed in through +yaml_opts+ will be used when converting the
 
48
  # store to YAML via Hash#to_yaml().
8
49
  def initialize( *o )
9
50
    @opt = {}
10
51
    if String === o.first
15
56
    end
16
57
  end
17
58
 
 
59
  # :stopdoc:
 
60
 
18
61
  def dump(table)
19
62
    @table.to_yaml(@opt)
20
63
  end