aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/server/configuration.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2015-07-05 22:34:23 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2015-07-05 22:34:23 +0200
commitb8b50e6b043a5b1900922629edf250ccb6006085 (patch)
tree2e8f191b45e9eaa89b7a1de45888685fc8b66907 /lib/action_cable/server/configuration.rb
parent44e7cc324df1189531b60de1c6353289c8205a97 (diff)
downloadrails-b8b50e6b043a5b1900922629edf250ccb6006085.tar.gz
rails-b8b50e6b043a5b1900922629edf250ccb6006085.tar.bz2
rails-b8b50e6b043a5b1900922629edf250ccb6006085.zip
Extract Server configuration into a Configuration object
Diffstat (limited to 'lib/action_cable/server/configuration.rb')
-rw-r--r--lib/action_cable/server/configuration.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/action_cable/server/configuration.rb b/lib/action_cable/server/configuration.rb
new file mode 100644
index 0000000000..c5783f30e0
--- /dev/null
+++ b/lib/action_cable/server/configuration.rb
@@ -0,0 +1,51 @@
+module ActionCable
+ module Server
+ class Configuration
+ attr_accessor :logger, :log_tags
+ attr_accessor :connection_class, :worker_pool_size
+ attr_accessor :redis_path, :channels_path
+
+ def initialize
+ @logger = Rails.logger
+ @log_tags = []
+
+ @connection_class = ApplicationCable::Connection
+ @worker_pool_size = 100
+
+ @redis_path = Rails.root.join('config/redis/cable.yml')
+ @channels_path = Rails.root.join('app/channels')
+ end
+
+ def channel_paths
+ @channels ||= Dir["#{channels_path}/**/*_channel.rb"]
+ end
+
+ def channel_class_names
+ @channel_class_names ||= channel_paths.collect do |channel_path|
+ Pathname.new(channel_path).basename.to_s.split('.').first.camelize
+ end
+ end
+
+ def redis
+ @redis ||= config_for(redis_path).with_indifferent_access
+ end
+
+ private
+ # FIXME: Extract this from Rails::Application in a way it can be used here.
+ def config_for(path)
+ if path.exist?
+ require "yaml"
+ require "erb"
+ (YAML.load(ERB.new(path.read).result) || {})[Rails.env] || {}
+ else
+ raise "Could not load configuration. No such file - #{path}"
+ end
+ rescue Psych::SyntaxError => e
+ raise "YAML syntax error occurred while parsing #{path}. " \
+ "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
+ "Error: #{e.message}"
+ end
+ end
+ end
+end
+