Skip to content

Commit 4707d0d

Browse files
committedAug 15, 2013
Use Enumerable for #map and #to_a in Element
1 parent da4e47f commit 4707d0d

File tree

2 files changed

+2
-34
lines changed

2 files changed

+2
-34
lines changed
 

‎opal/opal-jquery/element.rb

-32
Original file line numberDiff line numberDiff line change
@@ -247,45 +247,13 @@ def offset
247247
Hash.from_native(`#{self}.offset()`)
248248
end
249249

250-
# Yields each element in #{self} collection in turn. The yielded element
251-
# is wrapped as a `DOM` instance.
252-
#
253-
# @example
254-
#
255-
# DOM('.foo').each { |e| puts "The element id: #{e.id}" }
256-
#
257-
# @return returns the receiver
258250
def each
259251
`for (var i = 0, length = #{self}.length; i < length; i++) {`
260252
yield `$(#{self}[i])`
261253
`}`
262254
self
263255
end
264256

265-
# return an opal array mapped with block yielded for any element
266-
#
267-
# @example
268-
#
269-
# list = Document.find('table.players td.surname').map {|el| el.html }
270-
#
271-
# @return an Array
272-
def map
273-
list = []
274-
each {|el| list << yield(el) }
275-
list
276-
end
277-
278-
# return an opal Array of elements
279-
#
280-
# @example
281-
#
282-
# Document.find('table.players td.surname').to_a.last
283-
#
284-
# @return an Array
285-
def to_a
286-
map {|el| el }
287-
end
288-
289257
def first
290258
`#{self}.length ? #{self}.first() : nil`
291259
end

‎spec/element/iterable_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
it "should return a list of class Array" do
3838
Element.find('table.players td.surname').to_a.class.should == Array
3939
end
40-
40+
4141
it "should check first and last element" do
4242
Element.find('table.players td.surname').to_a.first.html == "rossi"
4343
Element.find('table.players td.surname').to_a.last.html == "bianchi"
4444
end
45-
45+
4646
it "should get only element with class surname" do
4747
Element.find('table.players td').to_a.select {|el| el.has_class?('surname') }.
4848
map {|el| el.class }.uniq == ['surname']

0 commit comments

Comments
 (0)
Please sign in to comment.