1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Back to Ruby 1.8.6 compatibility.

Apparently 1.8.6's Array#flatten doesn't take an arg. Annoying.

Closes gh-140
This commit is contained in:
Nathan Weizenbaum 2010-04-26 14:54:47 -07:00
parent dbccadba45
commit cc6f2ed399
4 changed files with 41 additions and 7 deletions

View file

@ -189,7 +189,7 @@ module Haml
# # [2, 4, 5]]
def paths(arrs)
arrs.inject([[]]) do |paths, arr|
arr.map {|e| paths.map {|path| path + [e]}}.flatten(1)
flatten(arr.map {|e| paths.map {|path| path + [e]}}, 1)
end
end
@ -370,6 +370,14 @@ module Haml
Haml::Util::RUBY_VERSION[0] == 1 && Haml::Util::RUBY_VERSION[1] < 9
end
# Whether or not this is running under Ruby 1.8.6 or lower.
# Note that lower versions are not officially supported.
#
# @return [Boolean]
def ruby1_8_6?
ruby1_8? && Haml::Util::RUBY_VERSION[2] < 7
end
# Checks that the encoding of a string is valid in Ruby 1.9
# and cleans up potential encoding gotchas like the UTF-8 BOM.
# If it's not, yields an error string describing the invalid character
@ -458,6 +466,17 @@ MSG
ruby1_8? ? c[0] : c.ord
end
# Flattens the first `n` nested arrays in a cross-version manner.
#
# @param arr [Array] The array to flatten
# @param n [Fixnum] The number of levels to flatten
# @return [Array] The flattened array
def flatten(arr, n)
return arr.flatten(n) unless ruby1_8_6?
return arr if n == 0
arr.inject([]) {|res, e| e.is_a?(Array) ? res.concat(flatten(e, n - 1)) : res << e}
end
## Static Method Stuff
# The context in which the ERB for \{#def\_static\_method} will be run.

View file

@ -78,7 +78,7 @@ module Haml
[index, subenum]
end
end
res.flatten!(1)
res = Haml::Util.flatten(res, 1)
res.compact!
res.uniq!
res.sort!

View file

@ -77,10 +77,11 @@ module Sass
# These correspond to a {CommaSequence}'s {CommaSequence#members members array}.
# @see CommaSequence#do_extend
def do_extend(extends, supers = [])
Haml::Util.paths(members.map do |sseq_or_op|
paths = Haml::Util.paths(members.map do |sseq_or_op|
next [[sseq_or_op]] unless sseq_or_op.is_a?(SimpleSequence)
[[sseq_or_op], *sseq_or_op.do_extend(extends, supers).map {|seq| seq.members}]
end).map {|path| weave(path)}.flatten(1).map {|p| Sequence.new(p)}
end)
Haml::Util.flatten(paths.map {|path| weave(path)}, 1).map {|p| Sequence.new(p)}
end
# @see Simple#to_a
@ -134,9 +135,9 @@ module Sass
while !current.empty? && last_current.first.is_a?(String) || current.last.is_a?(String)
last_current.unshift(current.pop)
end
befores = befores.map do |before|
subweave(before, current).map {|seqs| seqs + last_current}
end.flatten(1)
befores = Haml::Util.flatten(befores.map do |before|
subweave(before, current).map {|seqs| seqs + last_current}
end, 1)
return befores if afters.empty?
end
end

View file

@ -144,6 +144,20 @@ class UtilTest < Test::Unit::TestCase
assert_equal(98, ord("bar"))
end
def test_flatten
assert_equal([1, 2, 3], flatten([1, 2, 3], 0))
assert_equal([1, 2, 3], flatten([1, 2, 3], 1))
assert_equal([1, 2, 3], flatten([1, 2, 3], 2))
assert_equal([[1, 2], 3], flatten([[1, 2], 3], 0))
assert_equal([1, 2, 3], flatten([[1, 2], 3], 1))
assert_equal([1, 2, 3], flatten([[1, 2], 3], 2))
assert_equal([[[1], 2], [3], 4], flatten([[[1], 2], [3], 4], 0))
assert_equal([[1], 2, 3, 4], flatten([[[1], 2], [3], 4], 1))
assert_equal([1, 2, 3, 4], flatten([[[1], 2], [3], 4], 2))
end
def test_caller_info
assert_equal(["/tmp/foo.rb", 12, "fizzle"], caller_info("/tmp/foo.rb:12: in `fizzle'"))
assert_equal(["/tmp/foo.rb", 12, nil], caller_info("/tmp/foo.rb:12"))