Class: Filtri
- Inherits:
-
Object
- Object
- Filtri
- Defined in:
- lib/filtri.rb,
lib/filtri/version.rb
Overview
Filtri DSL
Constant Summary collapse
- RULES =
[:rule, :meta]
- VERSION =
Filtri version (semantic versioning)
"0.1.2"
Instance Attribute Summary collapse
-
#meta_rules ⇒ Object
readonly
The meta rules.
-
#rules ⇒ Object
readonly
The rules.
Class Method Summary collapse
-
.from_str(rule_str) ⇒ Filtri
Factory, init with rule-set from a string.
-
.load(file_name) ⇒ Filtri
Factory, Init by loading rules from a file.
- .valid_rules ⇒ Object
Instance Method Summary collapse
-
#add_rule(rule_set, rule_hash) ⇒ Object
Add a rule to the current rule-set.
-
#add_rule_str(rule_str) ⇒ Object
The input string is expected to contain rules and comments, one per line, separated by a line break.
-
#apply(in_str) ⇒ String
Apply filtering rules to the provided string.
- #do_rewrite(val, rule) ⇒ Object
-
#initialize ⇒ Filtri
constructor
A new instance of Filtri.
-
#load(file_name) ⇒ Object
Load rules from a file.
-
#meta(rule_hash) ⇒ Object
Add a meta rule.
-
#rewrite(in_hash, rules) ⇒ Object
Rewrite a hash with a set of rules.
-
#rule(rule_hash) ⇒ Object
Add a filtering rule.
Constructor Details
#initialize ⇒ Filtri
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_rules ⇒ Object (readonly)
The meta rules
13 14 15 |
# File 'lib/filtri.rb', line 13 def @meta_rules end |
#rules ⇒ Object (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.
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 |
Instance Method Details
#add_rule(rule_set, rule_hash) ⇒ Object
Add a rule to the current rule-set
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.
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.})" 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
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
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
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
35 36 37 |
# File 'lib/filtri.rb', line 35 def (rule_hash) add_rule(@meta_rules, rule_hash) end |
#rewrite(in_hash, rules) ⇒ Object
Rewrite a hash with a set of rules
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
29 30 31 |
# File 'lib/filtri.rb', line 29 def rule(rule_hash) add_rule(@rules, rule_hash) end |