Class: Paperclip::Document::Processors::Counter

Inherits:
Paperclip::Document::Processor show all
Defined in:
lib/paperclip/document/processors/counter.rb

Overview

This processor extract the OCR text of the file

Instance Attribute Summary collapse

Attributes inherited from Paperclip::Document::Processor

#instance, #tmp_dir

Instance Method Summary collapse

Methods inherited from Paperclip::Document::Processor

#basename, #file_path

Constructor Details

#initialize(file, options = {}, attachment = nil) ⇒ Counter

Returns a new instance of Counter.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/paperclip/document/processors/counter.rb', line 8

def initialize(file, options = {}, attachment = nil)
  super(file, options, attachment)
  if @options[:pages_count_column].nil? && pages_count_column?
    @options[:pages_count_column] = default_pages_count_column
  end
  @pages_count_column = @options[:pages_count_column]

  unless @pages_count_column
    raise Paperclip::Error, 'No pages count column given'
  end
end

Instance Attribute Details

#pages_count_columnObject

Returns the value of attribute pages_count_column.



6
7
8
# File 'lib/paperclip/document/processors/counter.rb', line 6

def pages_count_column
  @pages_count_column
end

Instance Method Details

#default_pages_count_columnObject

Returns the name of the default pages count column



39
40
41
# File 'lib/paperclip/document/processors/counter.rb', line 39

def default_pages_count_column
  @attachment.name.to_s + '_pages_count'
end

#makeObject

Extract the pages count of all the document



21
22
23
24
25
26
27
28
# File 'lib/paperclip/document/processors/counter.rb', line 21

def make
  count = Docsplit.extract_length(file_path.to_s)

  instance[pages_count_column] = count
  instance.run_callbacks(:save) { false }

  File.open(file.path)
end

#pages_count_column?Boolean

Check if a pages count column is present

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/paperclip/document/processors/counter.rb', line 31

def pages_count_column?
  expected_column = default_pages_count_column
  @attachment.instance.class.columns.detect do |column|
    column.name.to_s == expected_column
  end
end