为你做了点什么。
这是它的工作原理。
s = Settings.new(SettingsFileManager.new(some_file))
s.ip # returns whats in the file.
s.ip = 10.0.0.1 # overrides the new value.
我还没有写出它与文件交互的方式,所以你必须写一个合适的 SettingsFileManager。
如果有的话,它应该为您提供良好的入门基础。但我写它主要是为了测试我对 ruby 的了解。因为它使用了一些半棘手的东西。
也通过一些测试将它托管在github上。
class Settings
attr_accessor :settings
def initialize(file_writer)
@file_writer = file_writer
read_settings_to_new_hash
end
def read_settings_to_new_hash
@settings = fabricate_hash
@settings.replace(@file_writer.read_keys)
end
def fabricate_hash
InterceptedHash.new( &hash_saved_proc )
end
def hash_saved_proc
Proc.new do |key, value|
@file_writer.write_key(key, value)
end
end
def method_missing(m, *args, &block)
if @settings.has_key?(m) || args[0]
if args[0]
@settings[m.to_s.sub("=","").to_sym] = args[0]
else
@settings[m]
end
else
raise "unknown setting"
end
end
end
# Not implemented. This should continue the logic to interact with whatever you want.
class SettingsFileManager
def initialize(file)
@file = file
end
# write the key and its value somewhere.
def write_key(key, value)
puts 'write_key', key, value
end
# should return hash of keys.
def read_keys
puts 'read_keys'
Hash.new
end
end
class InterceptedHash < Hash
def initialize(&block)
super
@block = block
end
def store(key, value)
super(key, value)
@block.call(key, value)
end
def []=(key, value)
store(key, value)
end
end