Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwynne committed Sep 5, 2015
1 parent 7b58d5f commit 26151c8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
37 changes: 35 additions & 2 deletions lib/cucumber/events/bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,49 @@ def parse_event_id(event_id)
when Class
return event_id
when String
return Object.const_get(event_id)
constantize(event_id)
else
Object.const_get("#{@default_namespace}::#{camel_case(event_id)}")
constantize("#{@default_namespace}::#{camel_case(event_id)}")
end
end

def camel_case(underscored_name)
underscored_name.to_s.split("_").map { |word| word.upcase[0] + word[1..-1] }.join
end

# Thanks ActiveSupport
# (Only needed to support Ruby 1.9.3 and JRuby)
def constantize(camel_cased_word)
names = camel_cased_word.split('::')

# Trigger a built-in NameError exception including the ill-formed constant in the message.
Object.const_get(camel_cased_word) if names.empty?

# Remove the first blank element in case of '::ClassName' notation.
names.shift if names.size > 1 && names.first.empty?

names.inject(Object) do |constant, name|
if constant == Object
constant.const_get(name)
else
candidate = constant.const_get(name)
next candidate if constant.const_defined?(name, false)
next candidate unless Object.const_defined?(name)

# Go down the ancestors to check if it is owned directly. The check
# stops when we reach Object or the end of ancestors tree.
constant = constant.ancestors.inject do |const, ancestor|
break const if ancestor == Object
break ancestor if ancestor.const_defined?(name, false)
const
end

# owner is in Object, so raise
constant.const_get(name, false)
end
end
end

end
end
end
10 changes: 5 additions & 5 deletions spec/cucumber/events/bus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

module Cucumber
module Events
describe Bus do
class TestEvent
end
class TestEvent
end

class AnotherTestEvent
end
class AnotherTestEvent
end

describe Bus do
let(:bus) { Bus.new(Cucumber::Events) }
let(:test_event) { TestEvent.new }
let(:another_test_event) { AnotherTestEvent.new }
Expand Down

0 comments on commit 26151c8

Please sign in to comment.