Sha256: e7fd39e9d4685363b600b84f03c139513e2b923146e1819ea53ccdf600adfec0

Contents?: true

Size: 1.28 KB

Versions: 9

Compression:

Stored size: 1.28 KB

Contents

module OSC
  class AddressPattern
    def initialize( pattern )
      @pattern = pattern
      
      generate_regex_from_pattern
    end
    
    def match?( address )
      !!(@re.nil? || @re.match( address ))
    end
    
private 
    def generate_regex_from_pattern
      case @pattern
        when NIL; @re = @pattern
        when Regexp; @re = @pattern
        when String
          
          # i'm unsure what this does
          # @pattern.gsub!(/[.^(|)]/, '\\1')
          
          # handles osc single char wildcard matching
          @pattern.gsub!(/\?/, '[^/]')
          
          # handles osc * - 0 or more matching
          @pattern.gsub!(/\*/, '[^/]*')
          
          # handles [!] matching
          @pattern.gsub!(/\[!/, '[^')
            
          # handles {} matching
          @pattern.gsub!(/\{/, '(')
          @pattern.gsub!(/,/, '|')
          @pattern.gsub!(/\}/, ')')
          
          
          # keeps from matching before the begining of the pattern
          @pattern.gsub!(/\A/, '\A')
          
          # keeps from matching beyond the end, 
          # eg. pattern /hi does not match /hidden
          @pattern.gsub!(/\z/, '\z')

          @re = Regexp.new(@pattern)
        else
          raise ArgumentError, 'invalid pattern'
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
aberant-osc-ruby-0.1.6 lib/osc-ruby/address_pattern.rb
aberant-osc-ruby-0.2.0 lib/osc-ruby/address_pattern.rb
osc-ruby-1.0.0 lib/osc-ruby/address_pattern.rb
osc-ruby-0.7.0 lib/osc-ruby/address_pattern.rb
osc-ruby-0.6.3 lib/osc-ruby/address_pattern.rb
osc-ruby-0.6.2 lib/osc-ruby/address_pattern.rb
osc-ruby-0.6.1 lib/osc-ruby/address_pattern.rb
osc-ruby-0.6.0 lib/osc-ruby/address_pattern.rb
osc-ruby-0.2.0 lib/osc-ruby/address_pattern.rb