Skip to content

Commit 6a02d10

Browse files
committed
Merge pull request #32 from activeadmin/30-flatten-infinite-recursion
[30] Prevent infinite recursion
2 parents dbc6110 + 9146b34 commit 6a02d10

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

Rakefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,14 @@ require 'rspec/core/rake_task'
55
RSpec::Core::RakeTask.new(:spec)
66

77
task :default => :spec
8+
9+
task :console do
10+
require 'irb'
11+
require 'irb/completion'
12+
13+
require 'pry'
14+
require 'arbre'
15+
16+
ARGV.clear
17+
IRB.start
18+
end

lib/arbre/element.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'arbre/element/builder_methods'
2+
require 'arbre/element/proxy'
23
require 'arbre/element_collection'
34

45
module Arbre
@@ -147,11 +148,11 @@ def +(element)
147148
else
148149
element = Arbre::HTML::TextNode.from_string(element)
149150
end
150-
ElementCollection.new([self]) + element
151+
to_ary + element
151152
end
152153

153154
def to_ary
154-
ElementCollection.new [self]
155+
ElementCollection.new [Proxy.new(self)]
155156
end
156157
alias_method :to_a, :to_ary
157158

lib/arbre/element/proxy.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Arbre
2+
class Element
3+
class Proxy < BasicObject
4+
undef_method :==
5+
undef_method :equal?
6+
7+
def initialize(element)
8+
@element = element
9+
end
10+
11+
def respond_to?(method, include_all = false)
12+
if method.to_s == 'to_ary'
13+
false
14+
else
15+
super || @element.respond_to?(method, include_all)
16+
end
17+
end
18+
19+
def method_missing(method, *args, &block)
20+
if method.to_s == 'to_ary'
21+
super
22+
else
23+
@element.__send__ method, *args, &block
24+
end
25+
end
26+
end
27+
end
28+
end

spec/arbre/unit/element_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ def helper_method
6565

6666
end
6767

68+
it "to_a.flatten should not infinitely recurse" do
69+
Timeout.timeout(1) do
70+
element.to_a.flatten
71+
end
72+
end
73+
6874
describe "adding a child" do
6975

7076
let(:child){ Arbre::Element.new }

0 commit comments

Comments
 (0)