Class: Paperless::Rule
- Inherits:
-
Object
- Object
- Paperless::Rule
- Defined in:
- lib/paperless/rule.rb
Instance Attribute Summary collapse
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#destination ⇒ Object
readonly
Returns the value of attribute destination.
-
#matched ⇒ Object
readonly
Returns the value of attribute matched.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#initialize(options) ⇒ Rule
constructor
A new instance of Rule.
- #match(file, text) ⇒ Object
- #match_text(text) ⇒ Object
- #set_date(date, format) ⇒ Object
- #sub_var(attribute, value) ⇒ Object
- #sub_vars(value) ⇒ Object
Constructor Details
#initialize(options) ⇒ Rule
Returns a new instance of Rule.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/paperless/rule.rb', line 5 def initialize() # @todo: Add logic to validate rule has proper settings @scope = ['scope'].split @condition = ['condition'] @destination = ['destination'] @service = ['service'] @title = ['title'] @date = ['date'] @description = ['description'] @tags = ['tags'].nil? ? Array.new : ['tags'].split @date_stamp = DateTime.now @filename = '' @date_default_format = '%Y-%m-%d' @matched = false # Automatically replace spaces with \s*. OCR tends to be wonky with spaces. @condition = @condition.to_s.strip.gsub(/\s/,'\s*') end |
Instance Attribute Details
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
25 26 27 |
# File 'lib/paperless/rule.rb', line 25 def condition @condition end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
25 26 27 |
# File 'lib/paperless/rule.rb', line 25 def date @date end |
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
25 26 27 |
# File 'lib/paperless/rule.rb', line 25 def destination @destination end |
#matched ⇒ Object (readonly)
Returns the value of attribute matched.
25 26 27 |
# File 'lib/paperless/rule.rb', line 25 def matched @matched end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
25 26 27 |
# File 'lib/paperless/rule.rb', line 25 def service @service end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
25 26 27 |
# File 'lib/paperless/rule.rb', line 25 def @tags end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
25 26 27 |
# File 'lib/paperless/rule.rb', line 25 def title @title end |
Instance Method Details
#match(file, text) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/paperless/rule.rb', line 32 def match(file,text) return @matched if @matched file_ext = File.extname(file) @filename = File.basename(file, file_ext) if @condition == Paperless::DATE_VAR @date = date @matched = true else @scope.each do |type| case type when 'content' then self.match_text(text) when 'filename' then self.match_text(file) else puts "WARNING: Unknown scope defined - #{type}" end end end return @matched end |
#match_text(text) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/paperless/rule.rb', line 56 def match_text(text) match = text.match(/#{@condition}/i) {|md| md[0]} unless match.nil? puts "Rule Matched: #{@description}" self.sub_vars(match.gsub(/\s*$/,'')) # chomp! was not working... had to do this @matched = true end end |
#set_date(date, format) ⇒ Object
27 28 29 30 |
# File 'lib/paperless/rule.rb', line 27 def set_date(date,format) @date_stamp = date @date_default_format = format end |
#sub_var(attribute, value) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/paperless/rule.rb', line 65 def sub_var(attribute, value) unless attribute.nil? attribute.gsub!(/#{Paperless::FILENAME_VAR}/, @filename) attribute.gsub!(/#{Paperless::MATCH_VAR}/, value) attribute.gsub!(/#{Paperless::DATE_VAR}/, @date_stamp.strftime(@date_default_format)) # Custom date formats set at the rule level custom_date_regex = "#{Paperless::DATE_VAR.chop}=([a-zA-Z\%\-]+)>" if match = attribute.match(/#{custom_date_regex}/) attribute.gsub!(/#{custom_date_regex}/, @date_stamp.strftime(match[1])) end end attribute end |
#sub_vars(value) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/paperless/rule.rb', line 80 def sub_vars(value) # Need to do substituation for supported variables @destination = self.sub_var(@destination,value) @title = self.sub_var(@title,value) @date = self.sub_var(@date,value) @tags.collect! {|tag| tag = self.sub_var(tag,value) } end |