Sha256: ce17a4594609baba29db86d585f62442834088b67ed0222be5d3c9fc8cc56b13

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

# encoding: utf-8

module Ryodo
  class RuleSet

    def initialize
      @tree = {}
      build!
    end

    def build!
      Ryodo::SuffixList.list.each do |line|

        line.each.with_index do |node_name, idx|

          stopOK = node_name == line.last
          exception = node_name[0] == "!"
          node_name = node_name[1..-1] if exception
          children = {}

          node = Ryodo::Rule.new(exception, stopOK, children)

          if idx > 0
            end_idx = idx - 1
            parent = select_rule(line[0..end_idx])
            parent.children[node_name] = node unless parent.children[node_name]

          else
            @tree[node_name] = node unless @tree[node_name]
          end

        end

      end
    end

    def select_rule(rule_path)
      path = rule_path.dup

      if current = path.pop

        if path.empty?
          @tree[current]

        else
          rule = select_rule(path)
          rule.children[current] if rule
        end

      end
    end

    def match(path)
      suffix, domain, match = [], [], nil

      until match || path.empty?
        match = select_rule(path) || select_rule(path.dup.fill("*",-1))
        match = nil if match && !match.is_suffix?
        domain.unshift path.pop
        suffix = path
      end

      suffix.push(domain.shift) if match && !match.exception

      # only if match has no children with domain and domain is present
      if match && !match.children.keys.include?(domain[0]) && domain[0]
        [ suffix, [domain.shift], domain ]
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ryodo-0.2.2 lib/ryodo/rule_set.rb
ryodo-0.2.0 lib/ryodo/rule_set.rb
ryodo-0.1.0.1 lib/ryodo/rule_set.rb
ryodo-0.1.0 lib/ryodo/rule_set.rb