Skip to content

Instantly share code, notes, and snippets.

@Tei
Created October 5, 2010 13:14
Show Gist options
  • Save Tei/611527 to your computer and use it in GitHub Desktop.
Save Tei/611527 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require "rubygems"
require "yaml"
class Model
def initialize(file)
@file = file
@_attr = YAML::load_file(@file) || {}
end
def save
File.open(@file, 'w') do |out|
YAML::dump(@_attr, out)
end
end
private
def self.vattr(*keys)
keys.each do |key|
define_method(key) { @_attr[key] }
define_method("#{key}=") { |val| @_attr[key] = val }
end
end
end
class User < Model
vattr :id, :login
end
user = User.new('user.yaml')
user.login = 'demo'
user.id = 1
user.save
demo = User.new('user.yaml')
puts demo.login
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment