Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 109 lines (67 sloc) 2.667 kb
1894b96 @delano Release 0.8.5
authored
1 = Storable - v0.8
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
015d75d @delano Sensitive Fields
authored
28
c17a6e7 @delano Supports to_a
authored
29 == Sensitive Fields
6f6579a @delano Added example to README
authored
30
c17a6e7 @delano Supports to_a
authored
31 require 'storable'
32
33 class Calc < Storable
34 field :three
015d75d @delano Sensitive Fields
authored
35 field :two
36 field :one
c17a6e7 @delano Supports to_a
authored
37 sensitive_fields :three
38 end
39
015d75d @delano Sensitive Fields
authored
40 calc = Calc.new 3, 2, 1
41 calc.to_a # => [3, 2, 1]
42 calc.sensitive!
43 calc.to_a # => [2, 1]
44
c17a6e7 @delano Supports to_a
authored
45
f13def2 @delano ADDED: Storable#call for calling Proc fields via instance_eval
authored
46 == Storing Procs
47
48 Storable can also marshal Proc objects to and from their actual source code.
49
50 require 'storable'
51
52 class Maths < Storable
87e937b @delano Release 0.7.3 (Storable#call, Storable::Anonymous, Range support)
authored
53 field :x => Float
54 field :y => Float
f13def2 @delano ADDED: Storable#call for calling Proc fields via instance_eval
authored
55 field :calculate => Proc
56 end
57
d9552c6 @delano Renamed to_a to to_array to reduce conflicts
authored
58 m1 = Maths.new 2.0, 3.0
59 m1.calculate = Proc.new { @x * @y }
f13def2 @delano ADDED: Storable#call for calling Proc fields via instance_eval
authored
60
d9552c6 @delano Renamed to_a to to_array to reduce conflicts
authored
61 m1.calculate.source # => "{ @x * @y }"
62 m1.call :calculate # => 6.0
f13def2 @delano ADDED: Storable#call for calling Proc fields via instance_eval
authored
63
d9552c6 @delano Renamed to_a to to_array to reduce conflicts
authored
64 dump = m1.to_json
f13def2 @delano ADDED: Storable#call for calling Proc fields via instance_eval
authored
65
d9552c6 @delano Renamed to_a to to_array to reduce conflicts
authored
66 m2 = Maths.from_json dump
67 m2.call :calculate # => 6.0
f13def2 @delano ADDED: Storable#call for calling Proc fields via instance_eval
authored
68
c17a6e7 @delano Supports to_a
authored
69
87e937b @delano Release 0.7.3 (Storable#call, Storable::Anonymous, Range support)
authored
70 Anything is possible when you keep your mind open and you use Ruby.
71
72
a18d78d @delano Extracted from Rudy
authored
73 == Installation
74
75 Via Rubygems, one of:
76
77 $ sudo gem install storable
78 $ sudo gem install delano-storable --source http://gems.github.com/
79
80 or via download:
81 * storable-latest.tar.gz[http://github.com/delano/storable/tarball/latest]
82 * storable-latest.zip[http://github.com/delano/storable/zipball/latest]
83
84
6f6579a @delano Added example to README
authored
85 == Prerequisites
86
f13def2 @delano ADDED: Storable#call for calling Proc fields via instance_eval
authored
87 * Ruby 1.8, Ruby 1.9, or JRuby 1.2+
6f6579a @delano Added example to README
authored
88
89
2bb2351 @delano Updated readme
authored
90 == Credits
91
92 * Delano Mandelbaum (delano@solutious.com)
93 * lib/proc_source.rb is based on http://github.com/imedo/background
94 * OrderedHash implementation by Jan Molic
95
96
93efb51 @delano Added Thanks to README
authored
97 == Thanks
98
930a5dc @delano Release 0.8.7 (proc_source improvements)
authored
99 * Pierre Riteau (priteau[https://github.com/priteau]) for bug fixes.
100 * notro[https://github.com/priteau] for proc_source improvements.
93efb51 @delano Added Thanks to README
authored
101
102
2bb2351 @delano Updated readme
authored
103 == More Info
a18d78d @delano Extracted from Rudy
authored
104
2bb2351 @delano Updated readme
authored
105 * Codes[http://github.com/delano/storable]
a18d78d @delano Extracted from Rudy
authored
106
107 == License
108
109 See: LICENSE.txt
Something went wrong with that request. Please try again.