Skip to content

Commit

Permalink
Moved OrderedHash to CoreExt and added File.expand_path to ensure spe…
Browse files Browse the repository at this point in the history
…c_helper.rb is not loaded twice.
  • Loading branch information
josevalim committed May 23, 2009
1 parent 614326a commit 41be1be
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 84 deletions.
68 changes: 68 additions & 0 deletions lib/thor/core_ext/ordered_hash.rb
@@ -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
64 changes: 0 additions & 64 deletions lib/thor/ordered_hash.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/thor/task_hash.rb
@@ -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
Expand Down
21 changes: 10 additions & 11 deletions spec/ordered_hash_spec.rb → spec/core_ext/ordered_hash_spec.rb
@@ -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
Expand All @@ -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!"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/options_spec.rb
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/task_spec.rb
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/spec_helper'
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require "thor"
require "thor/task"

Expand Down
4 changes: 2 additions & 2 deletions spec/tasks_spec.rb
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/thor_runner_spec.rb
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + "/spec_helper"
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
require "thor/runner"
require "rr"

Expand Down
2 changes: 1 addition & 1 deletion spec/thor_spec.rb
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/spec_helper'
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require "thor"

class MyApp < Thor
Expand Down
2 changes: 1 addition & 1 deletion spec/util_spec.rb
@@ -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
Expand Down

0 comments on commit 41be1be

Please sign in to comment.