Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/delano/storable
Marshal Ruby classes into and out of multiple formats (yaml, json, csv, tsv)
https://github.com/delano/storable
Last synced: 16 days ago
JSON representation
Marshal Ruby classes into and out of multiple formats (yaml, json, csv, tsv)
- Host: GitHub
- URL: https://github.com/delano/storable
- Owner: delano
- Created: 2009-05-07T10:26:28.000Z (over 15 years ago)
- Default Branch: main
- Last Pushed: 2024-04-04T23:45:32.000Z (9 months ago)
- Last Synced: 2024-12-10T04:05:50.496Z (26 days ago)
- Language: Ruby
- Homepage: http://delano.github.com/storable
- Size: 324 KB
- Stars: 31
- Watchers: 5
- Forks: 7
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Storable - v0.9-RC1
Marshal Ruby classes in to and out of multiple formats (yaml, json, csv, tsv).
## Example
require 'storable'
class Machine < Storable
field :environment # Define field names for Machine. The
field :role # default type is String, but you can
field :position => Integer # specify a type using a hash syntax.
endmac1 = Machine.new # Instances of Machine have accessors
mac1.environment = "stage" # just like regular attributes.
mac1.role = "app"
mac1.position = 1puts "# YAML", mac1.to_yaml # Note: the field order is maintained
puts "# CSV", mac1.to_csv # => stage,app,1
puts "# JSON", mac1.to_json # Note: field order not maintained.mac2 = Machine.from_yaml(mac1.to_yaml)
puts mac2.environment # => "stage"
puts mac2.position.class # => Fixnum## Sensitive Fields
require 'storable'
class Calc < Storable
field :three
field :two
field :one
sensitive_fields :three
endcalc = Calc.new 3, 2, 1
calc.to_a # => [3, 2, 1]
calc.sensitive!
calc.to_a # => [2, 1]## Storing Procs
Storable can also marshal Proc objects to and from their actual source code.
require 'storable'
class Maths < Storable
field :x => Float
field :y => Float
field :calculate => Proc
endm1 = Maths.new 2.0, 3.0
m1.calculate = Proc.new { @x * @y }m1.calculate.source # => "{ @x * @y }"
m1.call :calculate # => 6.0dump = m1.to_json
m2 = Maths.from_json dump
m2.call :calculate # => 6.0Anything is possible when you keep your mind open and you use Ruby.
## Installation
Via Rubygems, one of:
$ sudo gem install storable
$ sudo gem install delano-storable --source http://gems.github.com/or via download:
* storable-latest.tar.gz[http://github.com/delano/storable/tarball/latest]
* storable-latest.zip[http://github.com/delano/storable/zipball/latest]## Prerequisites
* Ruby <=2.7, >=1.9, possibly JRuby
## Credits
* Delano Mandelbaum ([email protected])
* lib/proc_source.rb is based on http://github.com/imedo/background
* OrderedHash implementation by Jan Molic## Thanks
* Pierre Riteau (priteau[https://github.com/priteau]) for bug fixes.
* notro[https://github.com/priteau] for proc_source improvements.## More Info
* GitHub[http://github.com/delano/storable]
## License
See: LICENSE.txt