Skip to content

Instantly share code, notes, and snippets.

@mweppler
Created September 12, 2012 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mweppler/3709793 to your computer and use it in GitHub Desktop.
Save mweppler/3709793 to your computer and use it in GitHub Desktop.
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