Module: SpitterMethods

Included in:
Spitter
Defined in:
lib/rakumarket/spitter.rb

Overview

DSL for defining data extraction rules from an abstract document object

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



3
4
5
# File 'lib/rakumarket/spitter.rb', line 3

def self.extended(base)
  base.send(:include, InstanceMethods) if base.is_a? Class
end

Instance Method Details

#inspect_optionsObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rakumarket/spitter.rb', line 28

def inspect_options
  options = {}
  @rules.each do |k,v|
    options[k] = if v[1].respond_to?(:inspect_options)
      v[2] ? [v[1].inspect_options] : v[1].inspect_options
    else
      v[1].respond_to?(:values) ? v[1].values.keys.join('|') : ""
    end
  end
  options
end

#parameter(*args, &block) ⇒ Object

Declare a singular spitting rule



8
9
10
11
12
# File 'lib/rakumarket/spitter.rb', line 8

def parameter(*args, &block)
  key, name, delegate = parse_rule_declaration(*args, &block)
  rules[key] = [name, delegate]
  key
end

#parameters(*args, &block) ⇒ Object

Declare a plural spitting rule



15
16
17
18
# File 'lib/rakumarket/spitter.rb', line 15

def parameters(*args, &block)
  key = parameter(*args, &block)
  rules[key] << true
end

#parse(params) ⇒ Object

Process data by creating a new instance



26
# File 'lib/rakumarket/spitter.rb', line 26

def parse(params) new(params).parse end

#rulesObject

Parsing rules declared with ‘parameter` or `parameters`



21
22
23
# File 'lib/rakumarket/spitter.rb', line 21

def rules
  @rules ||= {}
end