Class: Foxy::Collection

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Monads
Defined in:
lib/foxy/collection.rb

Instance Method Summary collapse

Methods included from Monads

#and_then, #dangerously, #many, #mapy, #normally, #optionaly, #safy, #then

Instance Method Details

#attr(name) ⇒ Object



7
8
9
# File 'lib/foxy/collection.rb', line 7

def attr(name)
  each_with_object([]) { |node, acc| acc << node.attr(name) if node }
end

#clean(*args) ⇒ Object



45
46
47
# File 'lib/foxy/collection.rb', line 45

def clean(*args)
  mapy.clean(*args)
end

#css(query) ⇒ Object



21
22
23
# File 'lib/foxy/collection.rb', line 21

def css(query)
  query.split(/\s+/).inject(self) { |memo, q| memo.search(css: q) }
end

#flat_mapObject



37
38
39
# File 'lib/foxy/collection.rb', line 37

def flat_map
  self.class.new(super)
end

#joinedtextsObject



29
30
31
# File 'lib/foxy/collection.rb', line 29

def joinedtexts
  each_with_object([]) { |node, acc| acc << node.joinedtexts if node }
end

#mapObject



33
34
35
# File 'lib/foxy/collection.rb', line 33

def map
  self.class.new(super)
end

#rebuildObject



41
42
43
# File 'lib/foxy/collection.rb', line 41

def rebuild
  map(&:to_s).__getobj__.join
end

#search(**kws) ⇒ Object



11
12
13
14
15
16
17
18
19
# 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

#textsObject



25
26
27
# File 'lib/foxy/collection.rb', line 25

def texts
  map(&:texts).__getobj__
end