Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved OrderedHash to CoreExt and added File.expand_path to ensure spe…
…c_helper.rb is not loaded twice.
- Loading branch information
Showing
10 changed files
with
87 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters