Skip to content

Commit

Permalink
Dont pollute global namespace with env checks
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed May 19, 2013
1 parent 962c004 commit f40b906
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 43 deletions.
36 changes: 3 additions & 33 deletions lib/bson.rb
@@ -1,41 +1,11 @@
# encoding: utf-8

# Determine if we are using JRuby or not.
#
# @example Are we running with JRuby?
# jruby?
#
# @return [ true, false ] If JRuby is our vm.
#
# @since 2.0.0
def jruby?
defined?(JRUBY_VERSION)
end

# Does the Ruby runtime we are using support ordered hashes?
#
# @example Does the runtime support ordered hashes?
# ordered_hash_support?
#
# @return [ true, false ] If the runtime has ordered hashes.
#
# @since 2.0.0
def ordered_hash_support?
jruby? || RUBY_VERSION > "1.9.1"
end

# Are we running in a ruby runtime that is version 1.8.x?
#
# @since 2.0.0
def ruby_18?
RUBY_VERSION < "1.9"
end
require "bson/environment"

# In the case where we don't have encoding, we need to monkey
# patch string to ignore the encoding directives.
#
# @since 2.0.0
if ruby_18?
if BSON::Environment.ruby_18?

class String

Expand Down Expand Up @@ -113,7 +83,7 @@ module BSON
#
# @since 2.0.0
begin
if jruby?
if BSON::Environment.jruby?
require "bson-ruby.jar"
org.bson.NativeService.new.basicLoad(JRuby.runtime)
else
Expand Down
2 changes: 1 addition & 1 deletion lib/bson/document.rb
Expand Up @@ -44,7 +44,7 @@ def [](key)
# not, then we need to import our custom BSON::Document implementation.
#
# @since 2.0.0
unless ordered_hash_support?
unless Environment.retaining_hash_order?

# Message for argument error when providing bad arguments to [].
#
Expand Down
47 changes: 47 additions & 0 deletions lib/bson/environment.rb
@@ -0,0 +1,47 @@
# encoding: utf-8
module BSON

# Provides static helper methods around determining what environment is
# running without polluting the global namespace.
#
# @since 2.0.0
module Environment
extend self

# Determine if we are using JRuby or not.
#
# @example Are we running with JRuby?
# Environment.jruby?
#
# @return [ true, false ] If JRuby is our vm.
#
# @since 2.0.0
def jruby?
defined?(JRUBY_VERSION)
end

# Does the Ruby runtime we are using support ordered hashes?
#
# @example Does the runtime support ordered hashes?
# Environment.retaining_hash_order?
#
# @return [ true, false ] If the runtime has ordered hashes.
#
# @since 2.0.0
def retaining_hash_order?
jruby? || RUBY_VERSION > "1.9.1"
end

# Are we running in a ruby runtime that is version 1.8.x?
#
# @example Is the ruby runtime in 1.8 mode?
# Environment.ruby_18?
#
# @return [ true, false ] If we are running in 1.8.
#
# @since 2.0.0
def ruby_18?
RUBY_VERSION < "1.9"
end
end
end
2 changes: 1 addition & 1 deletion lib/bson/int32.rb
Expand Up @@ -17,7 +17,7 @@ class Int32
# Constant for the int 32 pack directive.
#
# @since 2.0.0
PACK = ruby_18? ? "l".freeze : "l<".freeze
PACK = Environment.ruby_18? ? "l".freeze : "l<".freeze

# Deserialize an Integer from BSON.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/bson/int64.rb
Expand Up @@ -17,7 +17,7 @@ class Int64
# Constant for the int 64 pack directive.
#
# @since 2.0.0
PACK = ruby_18? ? "q".freeze : "q<".freeze
PACK = Environment.ruby_18? ? "q".freeze : "q<".freeze

# Deserialize an Integer from BSON.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/bson/object_id.rb
Expand Up @@ -285,7 +285,7 @@ def generate(time, counter = 0)

private

if jruby?
if Environment.jruby?
def process_id
"#{Process.pid}#{Thread.current.object_id}".hash % 0xFFFF
end
Expand Down
4 changes: 2 additions & 2 deletions spec/bson/document_spec.rb
Expand Up @@ -10,7 +10,7 @@
{}
end
let(:enum_class) do
ruby_18? ? Enumerable::Enumerator : Enumerator
BSON::Environment.ruby_18? ? Enumerable::Enumerator : Enumerator
end

before do
Expand Down Expand Up @@ -544,7 +544,7 @@
end
end

if ordered_hash_support?
if BSON::Environment.retaining_hash_order?

context "when provided hashes" do

Expand Down
2 changes: 1 addition & 1 deletion spec/bson/object_id_spec.rb
Expand Up @@ -380,7 +380,7 @@
expect(object_id.to_s).to eq(expected)
end

unless ruby_18?
unless BSON::Environment.ruby_18?

it "returns the string in UTF-8" do
expect(object_id.to_s.encoding).to eq(Encoding.find(BSON::UTF8))
Expand Down
2 changes: 1 addition & 1 deletion spec/bson/registry_spec.rb
Expand Up @@ -22,7 +22,7 @@

context "when the type has no corresponding class" do

unless ruby_18?
unless BSON::Environment.ruby_18?

it "raises an error" do
expect {
Expand Down
4 changes: 2 additions & 2 deletions spec/bson/string_spec.rb
Expand Up @@ -96,7 +96,7 @@
it_behaves_like "a binary encoded string"
end

unless ruby_18?
unless BSON::Environment.ruby_18?

context "when the string contains non utf-8 characters" do

Expand Down Expand Up @@ -200,7 +200,7 @@
it_behaves_like "a binary encoded string"
end

unless ruby_18?
unless BSON::Environment.ruby_18?

context "when the string contains non utf-8 characters" do

Expand Down

0 comments on commit f40b906

Please sign in to comment.