Skip to content
Browse files

Adds basic support for switching timezones via Zonebie.set_random_tim…

…ezone
  • Loading branch information...
1 parent da984da commit b90c4efaf20239e1b38cc67d1de816b41ab88814 @alindeman alindeman committed Mar 5, 2012
View
22 lib/zonebie.rb
@@ -1,5 +1,25 @@
require "zonebie/version"
+require "zonebie/backends"
module Zonebie
- # Your code goes here...
+ class << self
+ def backend
+ @backend ||= Zonebie::Backends::ActiveSupport.new
+ @backend.name
+ end
+
+ def backend=(backend)
+ case backend
+ when :activesupport
+ @backend = Zonebie::Backends::ActiveSupport.new
+ else
+ raise ArgumentError, "Unsupported backend"
+ end
+ end
+
+ def set_random_timezone
+ zones = @backend.zones
+ @backend.zone = zones[rand(zones.length)]
+ end
+ end
end
View
1 lib/zonebie/backends.rb
@@ -0,0 +1 @@
+require "zonebie/backends/active_support"
View
19 lib/zonebie/backends/active_support.rb
@@ -0,0 +1,19 @@
+require "active_support/time"
+
+module Zonebie
+ module Backends
+ class ActiveSupport
+ def name
+ :activesupport
+ end
+
+ def zones
+ ::ActiveSupport::TimeZone.all.map(&:name)
+ end
+
+ def zone=(zone)
+ ::Time.zone = zone
+ end
+ end
+ end
+end
View
18 spec/integrations/activesupport_spec.rb
@@ -0,0 +1,18 @@
+require "spec_helper"
+require "active_support/time"
+
+describe Zonebie do
+ before do
+ Zonebie.backend = :activesupport
+ end
+
+ describe "#set_random_timezone" do
+ it "assigns a random timezone" do
+ ::Time.expects(:zone=).with do |zone|
+ ActiveSupport::TimeZone.all.map(&:name).include? zone
+ end
+
+ Zonebie.set_random_timezone
+ end
+ end
+end
View
27 spec/lib/zonebie/backends/active_support_spec.rb
@@ -0,0 +1,27 @@
+require "spec_helper"
+
+describe Zonebie::Backends::ActiveSupport do
+ describe "#name" do
+ its(:name) { should == :activesupport }
+ end
+
+ describe "#zones" do
+ it "returns a list of zones provided by ActiveSupport" do
+ ::ActiveSupport::TimeZone.stubs(:all).
+ returns([
+ stub(:name => "Eastern Time (US & Canada)"),
+ stub(:name => "Central Time (US & Canada)")
+ ])
+
+ subject.zones.should =~ ["Eastern Time (US & Canada)", "Central Time (US & Canada)"]
+ end
+ end
+
+ describe "#zone=" do
+ it "sets Time.zone provided by ActiveSupport" do
+ ::Time.expects(:zone=).with("Eastern Time (US & Canada)")
+
+ subject.zone = "Eastern Time (US & Canada)"
+ end
+ end
+end
View
19 spec/lib/zonebie_spec.rb
@@ -0,0 +1,19 @@
+require "spec_helper"
+
+describe Zonebie do
+ describe "#backend" do
+ it "defaults to the activesupport backend in the presence of activesupport" do
+ Zonebie.backend.should == :activesupport
+ end
+
+ pending "allows setting the backend to tzinfo"
+
+ pending "defaults to tzinfo in the absense ot activesupport"
+
+ it "does not allow setting the backend to an unsupported value" do
+ expect {
+ Zonebie.backend = :foobar
+ }.to raise_error(ArgumentError)
+ end
+ end
+end
View
7 spec/spec_helper.rb
@@ -1 +1,6 @@
-require File.expand_path("../lib/zonebie", __FILE__)
+require File.expand_path("../lib/zonebie", File.dirname(__FILE__))
+
+require "mocha"
+RSpec.configure do |c|
+ c.mock_with :mocha
+end
View
1 zonebie.gemspec
@@ -18,4 +18,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "rake"
gem.add_development_dependency "rspec", "~>2.8"
gem.add_development_dependency "activesupport", ">=2.3"
+ gem.add_development_dependency "mocha", "~>0.10.0"
end

0 comments on commit b90c4ef

Please sign in to comment.
Something went wrong with that request. Please try again.