Commit 70835d51 authored by Marcel Hoppe's avatar Marcel Hoppe

more flexible enabling and disabling

parent aabf1086
......@@ -14,6 +14,14 @@ module ActiveBunny
@@fanouts = {}
@@queues = {}
def self.run?
file = File.join(Rails.root, 'config', 'rabbitmq.yml')
return false unless File.exists?(file)
self.load_config(file)
self.config[:enabled]
end
def self.load_config(config_file)
self.config = YAMLSource.new(config_file).load
end
......@@ -24,6 +32,7 @@ module ActiveBunny
def self.config= (config)
@@config = {
enabled: false,
logger: Rails.logger,
auth_mechanism: "plain",
username: "guest",
......
module ActiveBunny
module ActiveRecord
module ActiveModel
module Hooks
def create_model_hooks(except:[], only:nil)
return unless File.exists?(File.join(Rails.root, 'config', 'rabbitmq.yml'))
return unless ActiveBunny.run?
hooks = (only || [:after_create, :after_update, :after_destroy]) - except
caller_class = self
model_name = self.send(:class_name)
......
require_relative 'basics'
require_relative 'active_record/hooks'
require_relative 'active_model/hooks'
module ActiveBunny
class Publisher
extend ActiveSupport::DescendantsTracker
extend ::ActiveBunny::Basics
extend ::ActiveBunny::ActiveRecord::Hooks
extend ::ActiveBunny::ActiveModel::Hooks
def publish(obj, method=nil)
method ||= caller_locations(1,1)[0].label
......
......@@ -11,13 +11,13 @@ module Rails
config.active_bunny = ::ActiveBunny
initializer "activebunny.load-config" do
if File.exists?(File.join(Rails.root, 'config', 'rabbitmq.yml'))
if config.active_bunny.run?
config.active_bunny.load_config("#{Rails.root}/config/rabbitmq.yml")
end
end
initializer "activebunny.file_watcher" do |app|
if File.exists?(File.join(Rails.root, 'config', 'rabbitmq.yml'))
if config.active_bunny.run?
app.reloaders << ActiveSupport::FileUpdateChecker.new([], { "app/subscribers" => [".rb"], "app/publishers" => [".rb"] }) do
puts "Active Bunny reloading..."
end
......@@ -25,7 +25,7 @@ module Rails
end
initializer "activebunny.connect_to_rabbitmq" do |app|
if File.exists?(File.join(Rails.root, 'config', 'rabbitmq.yml'))
if config.active_bunny.run?
config.active_bunny.connection
end
end
......@@ -41,7 +41,7 @@ module Rails
end
def self.load_stuff
if File.exists?(File.join(Rails.root, 'config', 'rabbitmq.yml'))
if config.active_bunny.run?
Dir.glob(File.join(Rails.root, 'app', 'publishers', '**', '*.rb'), &method(:require_dependency))
Dir.glob(File.join(Rails.root, 'app', 'subscribers', '**', '*.rb'), &method(:require_dependency))
end
......
require_relative 'basics'
require_relative 'active_record/hooks'
require_relative 'active_model/hooks'
module ActiveBunny
class Subscriber
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment