Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails初始化器中配置库_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何在Rails初始化器中配置库

Ruby on rails 如何在Rails初始化器中配置库,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在编写一个库(包装器),它将与AWS运动交互。我希望该库可以像其他库一样进行配置 我也在调查并试图了解他们是如何做到这一点的。不过,我认为我并不完全理解。我认为他们正在使用中间件来处理这个问题,但不确定如何为我的用例配置它 我想要一个文件../initializers/firehouse.rb,内容可以如下所示: Firehose.configure do |config| config.stream_name = ENV['AWS_KINESIS_FIREHOSE_STREAM']

我正在编写一个库(包装器),它将与AWS运动交互。我希望该库可以像其他库一样进行配置

我也在调查并试图了解他们是如何做到这一点的。不过,我认为我并不完全理解。我认为他们正在使用中间件来处理这个问题,但不确定如何为我的用例配置它

我想要一个文件
../initializers/firehouse.rb
,内容可以如下所示:

Firehose.configure do |config|
  config.stream_name = ENV['AWS_KINESIS_FIREHOSE_STREAM']
  config.region = ENV['AWS_KINESIS_FIREHOSE_REGION']
  #.. more config here
end
以前有人这样做过吗

我认为他们正在使用中间件来处理这个问题

不,没有中间件。这是一颗普通的红宝石

下面是最小/基本实现的外观

class Configuration
  attr_accessor :host, :port
end

class MyService
  attr_reader :configuration

  def initialize
    @configuration = Configuration.new
  end

  def configure(&block)
    block.call(configuration)
  end
end

service = MyService.new

service.configuration # => #<Configuration:0x007fefa9084530>
service.configuration.host # => nil


service.configure do |config|
  config.host = 'http://example.com'
  config.port = 8080
end

service.configuration # => #<Configuration:0x007fefa9084530 @host="http://example.com", @port=8080>
service.configuration.host # => "http://example.com"
类配置
属性访问器:主机,:端口
结束
类MyService
属性读取器:配置
def初始化
@configuration=configuration.new
结束
def配置(&block)
块调用(配置)
结束
结束
service=MyService.new
service.configuration#=>#
service.configuration.host#=>nil
service.configure do | config|
config.host=http://example.com'
config.port=8080
结束
service.configuration#=>#
service.configuration.host#=>”http://example.com"
正如你所看到的,这里没有什么复杂的事情。只是传递物体而已

我认为他们正在使用中间件来处理这个问题

不,没有中间件。这是一颗普通的红宝石

下面是最小/基本实现的外观

class Configuration
  attr_accessor :host, :port
end

class MyService
  attr_reader :configuration

  def initialize
    @configuration = Configuration.new
  end

  def configure(&block)
    block.call(configuration)
  end
end

service = MyService.new

service.configuration # => #<Configuration:0x007fefa9084530>
service.configuration.host # => nil


service.configure do |config|
  config.host = 'http://example.com'
  config.port = 8080
end

service.configuration # => #<Configuration:0x007fefa9084530 @host="http://example.com", @port=8080>
service.configuration.host # => "http://example.com"
类配置
属性访问器:主机,:端口
结束
类MyService
属性读取器:配置
def初始化
@configuration=configuration.new
结束
def配置(&block)
块调用(配置)
结束
结束
service=MyService.new
service.configuration#=>#
service.configuration.host#=>nil
service.configure do | config|
config.host=http://example.com'
config.port=8080
结束
service.configuration#=>#
service.configuration.host#=>”http://example.com"

正如你所看到的,这里没有什么复杂的事情。只是传递对象而已。

哦,我明白了,我几乎没有疑问这个
服务
对象在代码中的其他地方如何可用?@aks:通常,你在类本身(而不是它的实例)上有这个
configure
方法,它自然在任何地方都可用。这是一个小的调整,我留给你。哦,我明白了,我几乎没有疑问这个
服务
对象如何在代码中的任何其他地方可用?@aks:通常,你在类本身(而不是它的实例)上有这个
配置
方法,它自然在任何地方都可用。这是一个小的调整,我留给你。