Skip to content

Commit

Permalink
Merge pull request #62 from remind101/default-properties
Browse files Browse the repository at this point in the history
Merge in global properties.
  • Loading branch information
michaelklishin committed Dec 24, 2013
2 parents 46e9de2 + 498aefb commit c8e748a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/hutch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def self.logger
Hutch::Logging.logger
end

def self.global_properties=(properties)
@global_properties = properties
end

def self.global_properties
@global_properties ||= {}
end

def self.connect(config = Hutch::Config)
unless connected?
@broker = Hutch::Broker.new(config)
Expand Down
5 changes: 5 additions & 0 deletions lib/hutch/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def publish(routing_key, message, properties = {})
logger.info("publishing message '#{message.inspect}' to #{routing_key}")
@exchange.publish(payload, {persistent: true}.
merge(properties).
merge(global_properties).
merge(non_overridable_properties))
end

Expand All @@ -187,6 +188,10 @@ def work_pool_threads
def generate_id
SecureRandom.uuid
end

def global_properties
Hutch.global_properties.respond_to?(:call) ? Hutch.global_properties.call : Hutch.global_properties
end
end
end

24 changes: 24 additions & 0 deletions spec/hutch/broker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,30 @@
broker.exchange.should_receive(:publish).once
broker.publish('test.key', 'message', {expiration: "2000", persistent: false})
end

context 'when there are global properties' do
context 'as a hash' do
before do
Hutch.stub global_properties: { app_id: 'app' }
end

it 'merges the properties' do
broker.exchange.should_receive(:publish).with('"message"', hash_including(app_id: 'app'))
broker.publish('test.key', 'message')
end
end

context 'as a callable object' do
before do
Hutch.stub global_properties: proc { { app_id: 'app' } }
end

it 'calls the proc and merges the properties' do
broker.exchange.should_receive(:publish).with('"message"', hash_including(app_id: 'app'))
broker.publish('test.key', 'message')
end
end
end
end

context 'without a valid connection' do
Expand Down

0 comments on commit c8e748a

Please sign in to comment.