Permalink
Browse files

Moved OrderedHash to CoreExt and added File.expand_path to ensure spe…

…c_helper.rb is not loaded twice.
  • Loading branch information...
1 parent 614326a commit 41be1be7caf061a3b3cb9e4376140f2c9bf6a0be @josevalim josevalim committed May 23, 2009
@@ -0,0 +1,68 @@
+class Thor #:nodoc:
+ module CoreExt #:nodoc:
+
+ # This class is based on the Ruby 1.9 ordered hashes.
+ # It keeps the semantics and most of the efficiency of normal hashes
+ # while also keeping track of the order in which elements were set.
+ #
+ class OrderedHash #:nodoc:
+ Node = Struct.new(:key, :value, :next, :prev)
+ include Enumerable
+
+ def initialize
+ @hash = {}
+ end
+
+ def initialize_copy(other)
+ @hash = other.instance_variable_get('@hash').clone
+ end
+
+ def [](key)
+ @hash[key] && @hash[key].value
+ end
+
+ def []=(key, value)
+ node = Node.new(key, value)
+
+ if old = @hash[key]
+ if old.prev
+ old.prev.next = old.next
+ else # old is @first and @last
+ @first = @last = nil
+ end
+ end
+
+ if @first.nil?
+ @first = @last = node
+ else
+ node.prev = @last
+ @last.next = node
+ @last = node
+ end
+
+ @hash[key] = node
+ value
+ end
+
+ def each
+ return unless @first
+ yield [@first.key, @first.value]
+ node = @first
+ yield [node.key, node.value] while node = node.next
+ self
+ end
+
+ def values
+ self.map { |k, v| v }
+ end
+
+ def +(other)
+ new = clone
+ other.each do |key, value|
+ new[key] = value unless self[key]
+ end
+ new
+ end
+ end
+ end
+end
View
@@ -1,64 +0,0 @@
-class Thor
- # This class is based on the Ruby 1.9 ordered hashes.
- # It keeps the semantics and most of the efficiency of normal hashes
- # while also keeping track of the order in which elements were set.
- class OrderedHash
- Node = Struct.new(:key, :value, :next, :prev)
- include Enumerable
-
- def initialize
- @hash = {}
- end
-
- def initialize_copy(other)
- @hash = other.instance_variable_get('@hash').clone
- end
-
- def [](key)
- @hash[key] && @hash[key].value
- end
-
- def []=(key, value)
- node = Node.new(key, value)
-
- if old = @hash[key]
- if old.prev
- old.prev.next = old.next
- else # old is @first and @last
- @first = @last = nil
- end
- end
-
- if @first.nil?
- @first = @last = node
- else
- node.prev = @last
- @last.next = node
- @last = node
- end
-
- @hash[key] = node
- value
- end
-
- def each
- return unless @first
- yield [@first.key, @first.value]
- node = @first
- yield [node.key, node.value] while node = node.next
- self
- end
-
- def values
- self.map { |k, v| v }
- end
-
- def +(other)
- new = clone
- other.each do |key, value|
- new[key] = value unless self[key]
- end
- new
- end
- end
-end
View
@@ -1,7 +1,7 @@
-require 'thor/ordered_hash'
+require 'thor/core_ext/ordered_hash'
require 'thor/task'
-class Thor::TaskHash < Thor::OrderedHash
+class Thor::TaskHash < Thor::CoreExt::OrderedHash
def initialize(klass)
super()
@klass = klass
@@ -1,12 +1,12 @@
-require File.dirname(__FILE__) + '/spec_helper'
-require "thor/ordered_hash"
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+require 'thor/core_ext/ordered_hash'
-describe Thor::OrderedHash do
- describe " without any items" do
- before :each do
- @hash = Thor::OrderedHash.new
- end
+describe Thor::CoreExt::OrderedHash do
+ before :each do
+ @hash = Thor::CoreExt::OrderedHash.new
+ end
+ describe "without any items" do
it "returns nil for an undefined key" do
@hash["foo"].must be_nil
end
@@ -20,9 +20,8 @@
end
end
- describe " with several items" do
+ describe "with several items" do
before :each do
- @hash = Thor::OrderedHash.new
@hash[:foo] = "Foo!"
@hash[:bar] = "Bar!"
@hash[:baz] = "Baz!"
@@ -67,7 +66,7 @@
end
it "should append another ordered hash while preserving ordering" do
- other_hash = Thor::OrderedHash.new
+ other_hash = Thor::CoreExt::OrderedHash.new
other_hash[1] = "one"
other_hash[2] = "two"
other_hash[3] = "three"
@@ -76,7 +75,7 @@
end
it "shouldn't overwrite hash keys with matching appended keys" do
- other_hash = Thor::OrderedHash.new
+ other_hash = Thor::CoreExt::OrderedHash.new
other_hash[:bar] = "bar"
(@hash + other_hash)[:bar].must == "Bar!"
end
View
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require "thor/options"
describe Thor::Options do
View
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require "thor"
require "thor/task"
View
@@ -1,9 +1,9 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require "thor"
require "thor/tasks"
# bleh. testing private methods?
-class <<Thor
+class << Thor
public :convert_task_options
end
View
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + "/spec_helper"
+require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
require "thor/runner"
require "rr"
View
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require "thor"
class MyApp < Thor
View
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/spec_helper'
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require "thor/util"
describe Thor::Util do

0 comments on commit 41be1be

Please sign in to comment.