Class: WebCrawler::Parsers::Mapper::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/web_crawler/parsers/mapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(selector, binding, options, &block) ⇒ Map

Returns a new instance of Map.



20
21
22
23
24
# File 'lib/web_crawler/parsers/mapper.rb', line 20

def initialize(selector, binding, options, &block)
  @selector, @options = selector, self.class.default_options.merge(options)
  @binding = binding
  @block   = block
end

Instance Method Details

#call(context) ⇒ Object



52
53
54
55
56
# File 'lib/web_crawler/parsers/mapper.rb', line 52

def call(context)
  value = context.search(element)
  value = value.send(*on) if on.present?
  filter.call(value)
end

#elementObject



30
31
32
# File 'lib/web_crawler/parsers/mapper.rb', line 30

def element
  @selector
end

#filterObject



48
49
50
# File 'lib/web_crawler/parsers/mapper.rb', line 48

def filter
  @filter ||= Filter.new(@options[:filter], @binding)
end

#in(context) ⇒ Object



34
35
36
37
38
# File 'lib/web_crawler/parsers/mapper.rb', line 34

def in(context)
  context.search(element).each do |el|
    yield el, @block.call
  end
end

#onObject



40
41
42
# File 'lib/web_crawler/parsers/mapper.rb', line 40

def on
  @options[:on]
end

#to(context) ⇒ Object



44
45
46
# File 'lib/web_crawler/parsers/mapper.rb', line 44

def to(context)
  @options[:to].respond_to?(:call) ? @options[:to].call(context) : @options[:to]
end

#with_block?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/web_crawler/parsers/mapper.rb', line 26

def with_block?
  @block.present?
end