sieve-parser 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,6 +19,7 @@ module Sieve
19
19
  @type = (params[:type]) ? params[:name] : "if"
20
20
  @name = params[:name]
21
21
  @join = params[:join]
22
+ @disabled = params[:disabled]
22
23
  @conditions = (params[:conditions]) ? params[:conditions] : []
23
24
  @actions = (params[:actions]) ? params[:actions] : []
24
25
  parse unless @text.nil?
@@ -61,15 +62,17 @@ module Sieve
61
62
  #@return [string] text of filter
62
63
  def to_s
63
64
  text = "# #{name}\n"
64
- text += ((disabled?) ? "false #" : "") + "#{@type}"
65
+ text += "#{@type}" + ((disabled?) ? " false #" : "")
65
66
  if conditions.count > 1
66
67
  text += " #{@join} (" + conditions.join(", ") + ")"
67
68
  else
68
69
  text += " " + conditions[0].to_s
69
70
  end
70
- text += "\n{\n\t"
71
- text += actions.join("\n\t")
71
+ text += "\n{\n"
72
+ text += "\t" + actions.join("\n\t") if actions.count > 0
72
73
  text += "\n}\n"
74
+ rescue => e
75
+ puts e.to_s
73
76
  end
74
77
 
75
78
  # Is disabled or not? Return the status of filter
@@ -85,24 +88,28 @@ module Sieve
85
88
  private
86
89
  # Parse conditions, call the parse_common or parse_vacation
87
90
  def parse
88
- #regex_rules_params = "(^#.*)\nif([\s\w\:\"\.\;\(\)\,\-]+)\{([\@\<>=a-zA-Z0-9\s\[\]\_\:\"\.\;\(\)\,\-\/]+)\}$"
89
- #regex_rules_params2 = "(^#.*)\n(\S+)(.+)\n\{\n([\s\S]*)\}"
90
- parts = @text.scan(/(^#.*)\n(\S+)\s(.+)\n\{\n([\s\S]*;)\n\}/)[0]
91
- parse_name(parts[0])
92
- @type = parts[1]
93
-
94
- self.disable! if parts[2] =~ /.*false #/
95
- #if the join is true, dont have conditions...
96
- if parts[2] =~ /true/
97
- @conditions << Condition.new(type:"true")
98
- elsif parts[2] =~ /(anyof|allof)/
99
- @join = parts[2][/^\S+/]
100
- @conditions.concat(Condition.parse_all( parts[2].scan(/\(([\S\s]+)\)/)[0][0] ))
101
- else
102
- @conditions << Condition.new(text:parts[2])
91
+ begin
92
+ #regex_rules_params = "(^#.*)\nif([\s\w\:\"\.\;\(\)\,\-]+)\{([\@\<>=a-zA-Z0-9\s\[\]\_\:\"\.\;\(\)\,\-\/]+)\}$"
93
+ #regex_rules_params2 = "(^#.*)\n(\S+)(.+)\n\{\n([\s\S]*)\}"
94
+ parts = @text.scan(/(^#.*)\n(\S+)\s(.+)\n\{\n([\s\S]*)\n\}/)[0]
95
+ parse_name(parts[0])
96
+ @type = parts[1]
97
+
98
+ self.disable! if parts[2] =~ /.*false #/
99
+ #if the join is true, dont have conditions...
100
+ if parts[2] =~ /true/
101
+ @conditions << Condition.new(type:"true")
102
+ elsif parts[2] =~ /(anyof|allof)/
103
+ @join = parts[2][/^\S+/]
104
+ @conditions.concat(Condition.parse_all( parts[2].scan(/\(([\S\s]+)\)/)[0][0] ))
105
+ else
106
+ @conditions << Condition.new(text:parts[2])
107
+ end
108
+
109
+ @actions.concat(Action.parse_all(parts[3]))
110
+ rescue => e
111
+ puts e.to_s + " - text: #{@text}"
103
112
  end
104
-
105
- @actions.concat(Action.parse_all(parts[3]))
106
113
  end
107
114
 
108
115
 
@@ -11,7 +11,13 @@
11
11
  # @author Thiago Coutinho<thiago.coutinho@locaweb.com.br>
12
12
  # @note This code folow de "THE BEER-WARE LICENSE"
13
13
 
14
+ class SieveErrors
15
+ class FilterNotFound < StandardError;end
16
+ class InvalidParam < StandardError;end
17
+ end
18
+
14
19
  module Sieve
20
+
15
21
  class FilterSet
16
22
  attr_accessor :text_sieve, :auto_require
17
23
 
@@ -22,7 +28,7 @@ module Sieve
22
28
  @text_sieve = text_sieve
23
29
  @requires = []
24
30
  @filters = []
25
- @auto_require = true;
31
+ @auto_require = true
26
32
  parse unless @text_sieve.nil?
27
33
  end
28
34
 
@@ -44,7 +50,7 @@ module Sieve
44
50
  # @return [Sieve::Filter]
45
51
  def filter_index_by_name(name)
46
52
  key = @filters.index{|f| f.name==name}
47
- raise Exception.new("Filter not found") unless key
53
+ raise SieveErrors::FilterNotFound unless key
48
54
  key
49
55
  end
50
56
 
@@ -83,8 +89,26 @@ module Sieve
83
89
  # Return a text of filterset
84
90
  #@return [string] text of filterset
85
91
  def to_s
92
+ text = ""
86
93
  text = "require [\"#{requires.join('","')}\"];\n" if @requires.count > 0
87
- text += filters.join("")
94
+ text += filters.join("") if filters.count > 0
95
+ text
96
+ end
97
+
98
+ # Return a array of filters by select with all given params
99
+ #@param [{:name=>String, :disabled => Boolean, :text => String}]
100
+ #@note The :text only have text to search
101
+ #@return [Array<Sieve::Filter>]
102
+ def where
103
+ entries =[]
104
+ @filters.each do |filter|
105
+ cond = false
106
+ cond = filter.name == args[:name] unless args[:name].nil?
107
+ cond = filter.disabled? == args[:disabled] unless args[:disabled].nil?
108
+ cond = filter.to_s =~ /#{args[:text]}/ unless args[:text].nil?
109
+ entries << filter if cond
110
+ end
111
+ entries
88
112
  end
89
113
 
90
114
  private
@@ -95,7 +119,7 @@ module Sieve
95
119
  @requires.concat(r[0].split('","'))
96
120
  end
97
121
 
98
- @text_sieve.scan(/(^#.*\nif[\s\w\:\"\.\;\(\)\,\-]*\n\{[a-zA-Z0-9\s\@\<>=\:\[\]\_\"\.\;\(\)\,\-\/]*\n\}$)/).each do |f|
122
+ @text_sieve.scan(/(^#.*\nif[\s\w\:\"\.\;\(\)\,\-]*\n\{[a-zA-Z0-9\s\@\<>=\:\[\]\_\"\.\;\(\)\&\,\-\/]*\n\}$)/).each do |f|
99
123
  @filters << Sieve::Filter.new(text:f[0])
100
124
  end
101
125
 
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  spec = Gem::Specification.new do |s|
4
4
  s.name = 'sieve-parser'
5
- s.version = '0.0.4'
5
+ s.version = '0.0.5'
6
6
  s.summary = 'A Ruby library for sieve parser'
7
7
  s.description = <<-EOF
8
8
  sieve-parser is a pure-ruby implementation for parsing and
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sieve-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-01 00:00:00.000000000Z
12
+ date: 2012-10-09 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: ! " sieve-parser is a pure-ruby implementation for parsing and \n
15
15
  \ manipulate the sieve scripts.\n"