Необходимо быть в состоянии для ускорения Вашего script/spec
вызовы путем выполнения script/spec_server
в отдельном окне терминала, затем добавления дополнительного -X
параметр к вызовам спецификации.
Один из способов сделать это так:
require 'singleton'
class Settings
include Singleton
# All instance methods will be added as class methods
def self.method_added(name)
instance_eval %Q{
def #{name}
instance.send '#{name}'
end
}
end
def timeout
# lazy-load timeout from config file, or whatever
end
end
Settings.instance.timeout
Settings.timeout
Если вам нужен более детальный контроль над методами для делегирования вы можете использовать методы делегирования:
require 'singleton'
require 'forwardable'
class Settings
include Singleton
extend SingleForwardable
# More fine grained control on specifying what methods exactly
# to be class methods
def_delegators :instance,:timeout,:foo#, other methods
def timeout
# lazy-load timeout from config file, or whatever
end
def foo
# some other stuff
end
end
Settings.timeout
Settings.foo
С другой стороны, я рекомендую использовать модули, если предполагаемая функциональность ограничена поведением, таким решением будет:
module Settings
extend self
def timeout
# lazy-load timeout from config file, or whatever
end
end
Settings.timeout