Class: Crabfarm::BaseReducer

Inherits:
Delegator
  • Object
show all
Includes:
Assertion::Fields
Defined in:
lib/crabfarm/base_reducer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertion::Fields

#field_hash, included, #reset_fields

Methods included from Assertion::Context

#assert

Constructor Details

#initialize(_target, _params) ⇒ BaseReducer

Returns a new instance of BaseReducer.



26
27
28
29
30
31
32
33
34
# File 'lib/crabfarm/base_reducer.rb', line 26

def initialize(_target, _params)
  reset_fields

  @parsed_data = parser.preprocess_parsing_target _target
  @document = parser.parse @parsed_data
  @params = _params

  super @document
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



7
8
9
# File 'lib/crabfarm/base_reducer.rb', line 7

def document
  @document
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/crabfarm/base_reducer.rb', line 7

def params
  @params
end

Class Method Details

.parserObject



13
14
15
# File 'lib/crabfarm/base_reducer.rb', line 13

def self.parser
  @parser ||= Strategies.load(:parser, @parser_name || Crabfarm.config.parser)
end

.snapshot_path(_name = nil) ⇒ Object



17
18
19
20
# File 'lib/crabfarm/base_reducer.rb', line 17

def self.snapshot_path(_name=nil)
  _name = self.to_s.underscore if _name.nil?
  File.join(GlobalState.snapshots_path, _name + '.' + parser.format)
end

.use_parser(_parser_name) ⇒ Object



9
10
11
# File 'lib/crabfarm/base_reducer.rb', line 9

def self.use_parser(_parser_name)
  @parser_name = _parser_name
end

Instance Method Details

#__getobj__Object



59
60
61
# File 'lib/crabfarm/base_reducer.rb', line 59

def __getobj__
  @document
end

#__setobj__(obj) ⇒ Object



63
64
65
# File 'lib/crabfarm/base_reducer.rb', line 63

def __setobj__(obj)
  @document = obj
end

#as_json(_options = nil) ⇒ Object



55
56
57
# File 'lib/crabfarm/base_reducer.rb', line 55

def as_json(_options=nil)
  field_hash
end

#parserObject



22
23
24
# File 'lib/crabfarm/base_reducer.rb', line 22

def parser
  self.class.parser
end

#runObject

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/crabfarm/base_reducer.rb', line 36

def run
  raise NotImplementedError.new
end

#take_snapshot(_name = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/crabfarm/base_reducer.rb', line 40

def take_snapshot(_name=nil)
  file_path = self.class.snapshot_path _name

  dir_path = file_path.split(File::SEPARATOR)[0...-1]
  FileUtils.mkpath dir_path.join(File::SEPARATOR) if dir_path.length > 0

  File.write file_path, @parsed_data
  file_path
end

#take_snapshot_and_fail(_name = nil) ⇒ Object



50
51
52
53
# File 'lib/crabfarm/base_reducer.rb', line 50

def take_snapshot_and_fail(_name=nil)
  file_path = take_snapshot _name
  raise ArgumentError.new "New snapshot for #{self.class.to_s} generated in '#{file_path}'"
end