이것은 내가 사용하는 것입니다. 다른 곳에서 아이디어를 얻었지만 그 구현은 제 것이 었습니다.
class AppConfig
# Loads a YAML configuration file from RAILS_ROOT/config/. The default file
# it looks for is 'application.yml', although if this doesn't match your
# application, you can pass in an alternative value as an argument
# to AppConfig.load.
# After the file has been loaded, any inline ERB is evaluated and unserialized
# into a hash. For each key-value pair in the hash, class getter and setter methods
# are defined i.e., AppConfig.key => "value"
# This allows you to store your application configuration information e.g., API keys and
# authentication credentials in a convenient manner, external to your application source
#
# application.yml example
#
# :defaults: &defaults
# :app_name: Platform
# :app_domain: dev.example.com
# :admin_email: [email protected]
# :development:
# <<: *defaults
# :test:
# <<: *defaults
# :production:
# <<: *defaults
# :app_domain: example.com
#
# For example will result in AppConfig.app_domain => "dev.example.com"
# when Rails.env == "development"
#
class << self
def load(file='application.yml')
configuration_file = File.join Rails.root, 'config', file
File.open(configuration_file) do |configuration|
configuration = ERB.new(configuration.read).result
configuration = YAML.load(configuration)[Rails.env.to_sym]
configuration.each do |key, value|
cattr_accessor key
send "#{key}=", value
end
end if File.exists? configuration_file
end
end
end
AppConfig.load
이 config/initializers/app_config.rb
을 만들고 그것으로 위의 코드를 붙여 넣습니다 광산의 생산 프로젝트에서 가져온. 나는 이것을 보석으로 만들 예정이다. 다른 사람들이 유용하다고 생각합니다.
편집 : 웹 기반 인터페이스를 통해 앱을 실행하면서 구성을 편집하고 싶습니다. getter 및 setter 메서드가 각 특성에 대해 정의되므로이 메서드를 사용하여이 작업을 수행 할 수 있습니다. 컨트롤러에서
:
def update
params[:configuration].each { |k,v| AppConfig.send "#{k}=", v }
…
end
내가 모델을 찾을 수없는 여기에 적합한 솔루션이다. DB 설정에 대해 잊어 버리십시오. 앱 구성을 제어하는 무언가를 인스턴스화 할 수 있다는 생각은 이치에 맞지 않습니다.당신이 그것을 구현하는 방법은 무엇입니까? 각 튜플에 대한 인스턴스! 싱글 톤 클래스 여야합니다.
어떤 technoligies를 사용하고 계십니까? 일부 언어/프레임 워크는이 문제에 대한 솔루션을 구축했습니다. – NDM
프레임 워크로 레일에 루비를 사용합니다. – sarunw