class Foxy::Collection
Public Instance Methods
attr(name)
click to toggle source
# File lib/foxy/collection.rb, line 7 def attr(name) each_with_object([]) { |node, acc| acc << node.attr(name) if node } end
clean(*args)
click to toggle source
# File lib/foxy/collection.rb, line 45 def clean(*args) mapy.clean(*args) end
css(query)
click to toggle source
# File lib/foxy/collection.rb, line 21 def css(query) query.split(/\s+/).inject(self) { |memo, q| memo.search(css: q) } end
flat_map()
click to toggle source
Calls superclass method
# File lib/foxy/collection.rb, line 37 def flat_map self.class.new(super) end
joinedtexts()
click to toggle source
# File lib/foxy/collection.rb, line 29 def joinedtexts each_with_object([]) { |node, acc| acc << node.joinedtexts if node } end
map()
click to toggle source
Calls superclass method
# File lib/foxy/collection.rb, line 33 def map self.class.new(super) end
rebuild()
click to toggle source
# File lib/foxy/collection.rb, line 41 def rebuild map(&:to_s).__getobj__.join end
search(**kws)
click to toggle source
# File lib/foxy/collection.rb, line 11 def search(**kws) return search(parse_css(kws[:css])) if kws[:css] filters = kws.delete(:filters) Collection.new(flat_map { |node| node.search(**kws) }).tap do |r| return filters.inject(r) { |_memo, filter| r.public_send(filter) } if filters end end
texts()
click to toggle source
# File lib/foxy/collection.rb, line 25 def texts map(&:texts).__getobj__ end
Private Instance Methods
parse_css(css)
click to toggle source
assert Foxy::Html.new
.parse_css(“tag#id”) == {tagname: “tag”, id: “id”} assert Foxy::Html.new
.parse_css(“#id”) == {id: “id”} assert Foxy::Html.new
.parse_css(“tag”) == {tagname: “tag”} assert Foxy::Html.new
.parse_css(“tag.cls”) == {tagname: “tag”, cls: [“cls”]} assert Foxy::Html.new
.parse_css(“.class”) == {cls: [“class”]} assert Foxy::Html.new
.parse_css(“.class.class”) == {cls: [“class”, “class”]} assert Foxy::Html.new
.parse_css(“.cls.class”) == {cls: [“cls”, “class”]}
# File lib/foxy/collection.rb, line 58 def parse_css(css) token = "([^:#\.\s]+)" css .scan(/#{token}|##{token}|\.#{token}|:#{token}/) .each_with_object({}) do |(tagname, id, cls, filter), memo| next memo[:tagname] = tagname if tagname next memo[:id] = id if id memo.fetch(:filters) { memo[:filters] = [] } << filter if filter memo.fetch(:cls) { memo[:cls] = [] } << cls if cls end end