Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 91 lines (54 sloc) 2.287 kb
a465c45 @delano Release 0.7.1
authored
1 = Storable - v0.7
a18d78d @delano Extracted from Rudy
authored
2
d74243f @delano Updated README
authored
3 Marshal Ruby classes into and out of multiple formats (yaml, json, csv, tsv)
a18d78d @delano Extracted from Rudy
authored
4
6f6579a @delano Added example to README
authored
5 == Example
6
7 require 'storable'
8
9 class Machine < Storable
10 field :environment # Define field names for Machine. The
11 field :role # default type is String, but you can
12 field :position => Integer # specify a type using a hash syntax.
13 end
14
15 mac1 = Machine.new # Instances of Machine have accessors
16 mac1.environment = "stage" # just like regular attributes.
17 mac1.role = "app"
18 mac1.position = 1
19
20 puts "# YAML", mac1.to_yaml # Note: the field order is maintained
21 puts "# CSV", mac1.to_csv # => stage,app,1
22 puts "# JSON", mac1.to_json # Note: field order not maintained.
23
24 mac2 = Machine.from_yaml(mac1.to_yaml)
25 puts mac2.environment # => "stage"
26 puts mac2.position.class # => Fixnum
27
28
f13def2 @delano ADDED: Storable#call for calling Proc fields via instance_eval
authored
29 == Storing Procs
30
31 Storable can also marshal Proc objects to and from their actual source code.
32
33 require 'storable'
34
35 class Maths < Storable
87e937b @delano Release 0.7.3 (Storable#call, Storable::Anonymous, Range support)
authored
36 field :x => Float
37 field :y => Float
f13def2 @delano ADDED: Storable#call for calling Proc fields via instance_eval
authored
38 field :calculate => Proc
39 end
40
41 mat1 = Maths.new 2.0, 3.0
42 mat1.calculate = Proc.new { @x * @y }
43
44 mat1.calculate.source # => "{ @x * @y }"
45 mat1.call :calculate # => 6.0
46
47 dump = mat1.to_json
48
49 mat2 = Maths.from_json dump
50 mat2.call :calculate # => 6.0
51
52
87e937b @delano Release 0.7.3 (Storable#call, Storable::Anonymous, Range support)
authored
53 Anything is possible when you keep your mind open and you use Ruby.
54
55
a18d78d @delano Extracted from Rudy
authored
56 == Installation
57
58 Via Rubygems, one of:
59
60 $ sudo gem install storable
61 $ sudo gem install delano-storable --source http://gems.github.com/
62
63 or via download:
64 * storable-latest.tar.gz[http://github.com/delano/storable/tarball/latest]
65 * storable-latest.zip[http://github.com/delano/storable/zipball/latest]
66
67
6f6579a @delano Added example to README
authored
68 == Prerequisites
69
f13def2 @delano ADDED: Storable#call for calling Proc fields via instance_eval
authored
70 * Ruby 1.8, Ruby 1.9, or JRuby 1.2+
6f6579a @delano Added example to README
authored
71
72
2bb2351 @delano Updated readme
authored
73 == Credits
74
75 * Delano Mandelbaum (delano@solutious.com)
76 * lib/proc_source.rb is based on http://github.com/imedo/background
77 * OrderedHash implementation by Jan Molic
78
79
93efb51 @delano Added Thanks to README
authored
80 == Thanks
81
82 * Pierre Riteau (priteau[http://github.com/priteau]) for bug fixes.
83
84
2bb2351 @delano Updated readme
authored
85 == More Info
a18d78d @delano Extracted from Rudy
authored
86
2bb2351 @delano Updated readme
authored
87 * Codes[http://github.com/delano/storable]
a18d78d @delano Extracted from Rudy
authored
88
89 == License
90
91 See: LICENSE.txt
Something went wrong with that request. Please try again.