0.0
No release in over 3 years
Low commit activity in last 3 years
Normally you can only read from DATA but with data-writer you can also write to it. This allows you to easily persist data in a source file.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 0
 Project Readme

Build Status

data-writer

Normally you can only read from DATA but with data-writer you can also write to it. This allows you to easily persist data in a source file.

###Installation

gem install data-writer

###Usage

Here is a simple example program that keeps track of how many times it has been executed and stores this as a YAML file in DATA.

require 'data-writer'
require 'yaml'

store = YAML.load(DATA.read)
puts "run = #{store['run']}"
store["run"] += 1

DATAWriter.file("w+") do |w|
  w.write(store.to_yaml)
end

__END__
---
run: 1

Each time this program is run it will increment run by 1 and persist the result in the YAML hash.

###API

DATAWriter.file(mode_string, opt = {})

A factory method that makes file objects which can write to data. mode_string and opt are the same as for File.new. If this method is called with a block it behaves as File.open and if it is called without a block it returns a File object as in File.new.

Example:

# append to DATA
require 'data-writer'

appender = DATAWriter.file("a")
appender.write " my dear Watson"
appender.close

DATA.read # => "Elementary my dear Watson"

__END__
Elementary

If this method is called and DATA is not defined then it will raise a DATANotFoundError exception. The file objects returned by this method have their #rewind method changed so that it seeks back to the start of DATA, and not back to the start of the file.