Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Incomplete implementation of a flat file storage system.
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
You can’t perform that action at this time.