允许我在Rails中使用块语法配置我自己的类

问题描述 投票:0回答:5

我经常看到表单的代码

RSpec.configure do |config|
  config.include ApiHelper

  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  config.include FactoryGirl::Syntax::Methods

end

Rails中是否有一个功能可以让我做类似的事情?我希望能够在config / initializers / my_class.rb中使用类似的语法配置我自己的库

MyClass.configure do |config|
  # allow configuration here
end
ruby-on-rails-3 configuration
5个回答
1
投票

我不知道rails是否提供了帮助,但我为这个问题编写了我自己的小解决方案,我在几个宝石中使用:https://github.com/phoet/confiture

它让你定义配置:

module Your
  class Configuration
    include Confiture::Configuration

    confiture_allowed_keys(:secret, :key)
    confiture_defaults(secret: 'SECRET_STUFF', key: 'EVEN_MOAR_SECRET')
  end
end

并有一个简单的api来进行配置:

Your::Configuration.configure do |config|
  config.secret = 'your-secret'
  config.key    = 'your-key'
end

除此之外,还有很多其他的配置工具,如configatron或simpleconfig。


2
投票

Rails中没有什么特别之处 - 它是简单的Ruby代码。以下是它的完成方式:

class MyClass
  def self.configure(&block)
    a_hash = { :car => "Red" }
    puts "Hello"
    yield a_hash
    puts "There"
  end    
end


MyClass.configure do |config|
  puts "In Block"
  puts config[:car]
end

输出:

Hello
In Block
Red
There

我正在产生哈希,但你可以产生你想要的任何对象。


2
投票

在启动服务器时,Rails将加载config/initializers目录中的所有Ruby文件。

如果要为自己的自定义可配置类使用相同的样式,则只需实现接受块并将配置对象传递给该块的configure类方法。例如

class MyClassConfiguration
  # configuration attributes
end

class MyClass
  def self.configure
    yield configuration if block_given?
  end

  def self.configuration
    @config ||= MyClassConfiguration.new
  end
end

使用phoet的宝石会更容易。

值得一看的是,如果你好奇,RSpec会如何做到这一点:

RSpec.configure方法在https://github.com/rspec/rspec-core/blob/master/lib/rspec/core.rb

Configuration类在https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/configuration.rb中实现


1
投票

通过块设置的全局配置。

module VkRobioAPI
  module Configuration

  OPTION_NAMES = [
      :client_id,
      :redirect_uri,
      :display,
      :response_type
    ]

    attr_accessor *OPTION_NAMES

    def configure 
      yield self if block_given?
      self
    end

  end
end


module VkRobioAPI
  extend VkRobioAPI::Configuration

  class << self
    def result
     puts VkRobioAPI.client_id 
    end
  end
end

例:

  VkRobioAPI.configure do |config|
    config.client_id       = '3427211'
    config.redirect_uri   = 'bEWLUZrNLxff1oQpEa6M'
    config.response_type = 'http://localhost:3000/oauth/callback'
    config.display = 'token'
  end

结果:

VkRobioAPI.result
    #3427211
    #=> nil

0
投票

如果您正在使用rails,您可以将ActiveSupport::Configurable包含在您的课程中,然后离开。在rails之外,你必须将activesuport gem添加到你的Gemfile或gemspec,然后调用require 'active_support/configurable'

class MyClass
  include ActiveSupport::Configurable
end

用法

随着块

MyClass.configure do |config|
  config.key = "something"
  config.key2 = "something else"
end

或内联,无阻塞

MyClass.config.key = "something"
MyClass.config.key2 = "something else"

或者像哈希

MyClass.config[:key] = "somehting"
MyClass.config[:key2] = "something else"

注意:键可以是您选择的任何字符。

© www.soinside.com 2019 - 2024. All rights reserved.