Class: Filtri

Inherits:
Object
  • Object
show all
Defined in:
lib/filtri.rb,
lib/filtri/version.rb

Overview

Filtri DSL

Author:

Constant Summary collapse

RULES =
[:rule, :meta]
VERSION =

Filtri version (semantic versioning)

"0.1.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFiltri

Returns a new instance of Filtri.



20
21
22
23
24
25
# File 'lib/filtri.rb', line 20

def initialize
  @rules      = []
  @meta_rules = []
  @passes     = 1
  @meta_applied = false
end

Instance Attribute Details

#meta_rulesObject (readonly)

The meta rules



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

def meta_rules
  @meta_rules
end

#rulesObject (readonly)

The rules



11
12
13
# File 'lib/filtri.rb', line 11

def rules
  @rules
end

Class Method Details

.from_str(rule_str) ⇒ Filtri

Factory, init with rule-set from a string

The input string is expected to contain rules and comments, one per line, separated by a line break. The expected format of a line is ‘operation <space> argument <eol>’. Empty lines and lines starting with a ‘#’ are ignored. Whitespace at the beginning of a line is trimmed.

Parameters:

  • rule_str (String)

Returns:

  • (Filtri)

    A new Filtri object with the rules parsed from the provided string(s).

Raises:

  • (FiltriInitError)

    if an error occurs when initialising the rules from the provided strings



158
159
160
161
162
163
164
# File 'lib/filtri.rb', line 158

def self.from_str(rule_str)

  inst = Filtri.new
  inst.add_rule_str rule_str
  inst

end

.load(file_name) ⇒ Filtri

Factory, Init by loading rules from a file

Parameters:

  • file_name (String)

Returns:

  • (Filtri)

    A new Filtri object with the rules contained in the file

Raises:

  • (IOError, SystemCallError)

    If an error occurs when opening the file

  • (FiltriInitError)

    If an error occurs when parsing the rules in the file



182
183
184
185
186
187
# File 'lib/filtri.rb', line 182

def self.load(file_name)

  data = IO.read(file_name)
  Filtri.from_str(data)

end

.valid_rulesObject



190
191
192
# File 'lib/filtri.rb', line 190

def self.valid_rules
  Filtri::RULES
end

Instance Method Details

#add_rule(rule_set, rule_hash) ⇒ Object

Add a rule to the current rule-set

Parameters:

  • rule_set (Array<Hash{Regexp => String},Hash{String => String}>)
  • rule_hash (Hash{Regexp => String}, Hash{String => String})


43
44
45
46
47
48
49
# File 'lib/filtri.rb', line 43

def add_rule(rule_set, rule_hash)

  rule_hash.each_key do |k|
    rule_set << { from: k, to: rule_hash[k] }
  end

end

#add_rule_str(rule_str) ⇒ Object

The input string is expected to contain rules and comments, one per line, separated by a line break. The expected format of a line is “operation <space> argument <eol>”. Empty lines and lines starting with a ‘#’ are ignored. Whitespace at the beginning of a line is trimmed.

Parameters:

  • rule_str (String)

Raises:

  • (FiltriInitError)

    if an error occurs when initialising the rules from the provided strings



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/filtri.rb', line 118

def add_rule_str(rule_str)

  rule_str.strip.lines do |l|

    op_str = l.strip.partition " "
    if op_str[0].length > 0
      op     = op_str[0]
      op_arg = op_str[2]

      if Filtri::RULES.include? op.to_sym
        # parse arg string
        begin
          arg_hash = op_arg.to_h
        rescue Parslet::ParseFailed => err
          raise FiltriInitError, "Invalid rule format: '#{op_arg}' (#{err.message})"
        end
        # add rule
        self.send(op.to_sym,arg_hash)
      else
        raise FiltriInitError, "Unknown rule: #{op}" unless ( op == "#" || l[0] == "#" )
      end

    end
  end


end

#apply(in_str) ⇒ String

Apply filtering rules to the provided string

Parameters:

  • in_str (String)

Returns:

  • (String)

    the resulting string



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/filtri.rb', line 91

def apply(in_str)

  @passes.times do

    unless @meta_rules.empty?
      unless @meta_applied
        @rules = rewrite(@rules, @meta_rules)
        @meta_applied = true
      end
    end

    @rules.each do |rule|
      in_str = in_str.gsub(rule[:from], rule[:to])
    end

  end
  in_str
end

#do_rewrite(val, rule) ⇒ Object

Parameters:

  • val (Regexp, String)
  • rule (Hash{Regexp => String}, Hash{String => String})


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/filtri.rb', line 54

def do_rewrite(val, rule)
  case val
    when Regexp
      val_str = PP.singleline_pp(val, "")
      val_str.gsub!(rule[:from], rule[:to])
      val_str.to_regexp
    when String
      val.gsub(rule[:from], rule[:to])
    else
      val
  end

end

#load(file_name) ⇒ Object

Load rules from a file

Parameters:

  • file_name (String)

Raises:

  • (IOError, SystemCallError)

    If an error occurs when opening the file

  • (FiltriInitError)

    If an error occurs when parsing the rules in the file



170
171
172
173
174
175
# File 'lib/filtri.rb', line 170

def load(file_name)

  data = IO.read(file_name)
  add_rule_str(data)

end

#meta(rule_hash) ⇒ Object

Add a meta rule

Parameters:

  • rule_hash (Hash{Regexp=>String})


35
36
37
# File 'lib/filtri.rb', line 35

def meta(rule_hash)
  add_rule(@meta_rules, rule_hash)
end

#rewrite(in_hash, rules) ⇒ Object

Rewrite a hash with a set of rules

Parameters:

  • in_hash (Hash{Regexp => String}, Hash{String => String})
  • rules (Hash{Regexp => String}, Hash{String => String})


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/filtri.rb', line 72

def rewrite(in_hash, rules)
  out_hash = []
  in_hash.each do |v|
    f = v[:from]
    t = v[:to]

    rules.each do |r|
      f = do_rewrite(f, r)
      t = do_rewrite(t, r)
    end
    out_hash << { from: f, to: t }
  end
  out_hash
end

#rule(rule_hash) ⇒ Object

Add a filtering rule

Parameters:

  • rule_hash (Hash{Regexp => String}, Hash{String => String})


29
30
31
# File 'lib/filtri.rb', line 29

def rule(rule_hash)
  add_rule(@rules, rule_hash)
end