Created
September 12, 2012 20:46
-
-
Save mweppler/3709793 to your computer and use it in GitHub Desktop.
Incomplete implementation of a flat file storage system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FlatFileProperties | |
def properties(*attributes) | |
@file_properties = attributes | |
end | |
def file_properties | |
@file_properties | |
end | |
end | |
module FlatFile | |
def from_file(file_name) | |
properties = [] | |
File.open(file_name, 'r') do |file| | |
tmp_property = "" | |
while line = file.gets | |
if line.match /^--END_PROPERTY--$/ | |
properties << tmp_property | |
tmp_property = "" | |
next | |
end | |
tmp_property << line | |
end | |
end | |
self.class.file_properties.each_with_index do |property, index| | |
self.instance_variable_set("@#{property}", properties[index]) | |
end | |
end | |
def to_file(file_name) | |
file = File.open(file_name, 'w') | |
self.class.file_properties.each do |property| | |
file.write "#{send(property)}\n--END_PROPERTY--\n" | |
end | |
file.close | |
end | |
def to_s | |
self.class.file_properties.each do |property| | |
puts "[#{property.to_s.upcase}] #{send(property)}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment