pattern_matcher 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ module PatternMatcher
2
+ class Match
3
+
4
+ attr_accessor :name, :regex_match
5
+
6
+ def initialize(hash)
7
+ if hash.is_a?(Hash)
8
+ @name = hash[:name]
9
+ @regex_match = hash[:regex_match]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -2,11 +2,18 @@ module PatternMatcher
2
2
  class Matcher
3
3
 
4
4
  def self.string_to_regex(string)
5
- Regexp.new string if is_valid_regex_string?(string)
5
+ begin
6
+ Regexp.new string if is_valid_regex_string?(string)
7
+ rescue
8
+ end
6
9
  end
7
10
 
8
11
  def self.match_pattern_in_text(pattern, text)
9
- return pattern.match text if is_valid_pattern?(pattern) && is_valid_text?(text)
12
+ return match_regex_in_text(pattern.regex, text) if pattern && is_valid_text?(text)
13
+ end
14
+
15
+ def self.match_regex_in_text(regex, text)
16
+ return regex.match text if is_valid_regex?(regex) && is_valid_text?(text)
10
17
  end
11
18
 
12
19
  def self.initialize_patterns
@@ -20,7 +27,7 @@ module PatternMatcher
20
27
  !string.nil? && !string.empty?
21
28
  end
22
29
 
23
- def self.is_valid_pattern?(pattern)
30
+ def self.is_valid_regex?(pattern)
24
31
  !pattern.nil?
25
32
  end
26
33
 
@@ -1,18 +1,33 @@
1
1
  module PatternMatcher
2
2
  class Pattern
3
3
 
4
- attr_accessor :name, :regex, :description
4
+ attr_accessor :name, :regex_string, :description, :valid_examples
5
+ attr_accessor :regex
5
6
 
6
7
  def initialize(hash)
7
8
  if !hash.nil?
8
9
  @name = hash["name"]
9
- @regex = hash["regex"]
10
+ @regex_string = hash["regex"]
11
+ @regex = Matcher.string_to_regex(@regex_string)
10
12
  @description = hash["description"]
13
+ @valid_examples = hash["valid_examples"] || []
11
14
  end
12
15
  end
13
16
 
17
+ def validate_all_examples
18
+ failures = []
19
+ @valid_examples.each do |example|
20
+ failures << example if !pattern_example_valid? example
21
+ end
22
+ failures
23
+ end
24
+
14
25
  def is_valid?
15
- return !(@regex.nil? || @regex.empty?)
26
+ return !@regex.nil?
27
+ end
28
+
29
+ def pattern_example_valid?(example)
30
+ Matcher.match_regex_in_text(@regex, example)
16
31
  end
17
32
  end
18
33
  end
@@ -1,3 +1,3 @@
1
1
  module PatternMatcher
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -6,6 +6,7 @@ require 'date'
6
6
  require "pattern_matcher/version"
7
7
  require "pattern_matcher/config"
8
8
  require "pattern_matcher/pattern"
9
+ require "pattern_matcher/match"
9
10
  require "pattern_matcher/matcher"
10
11
 
11
12
  module PatternMatcher
@@ -23,5 +24,22 @@ module PatternMatcher
23
24
  !@configuration.nil?
24
25
  end
25
26
 
27
+ def self.match_patterns_to_text(text)
28
+ matches = []
29
+ @patterns.each do |pattern|
30
+ regex_match = Matcher.match_pattern_in_text(pattern, text)
31
+ matches << Match.new({:name => pattern.name, :regex_match => regex_match}) if regex_match
32
+ end
33
+ matches
34
+ end
35
+
36
+ def self.proof_patterns
37
+ pattern_errors = []
38
+ @patterns.each do |pattern|
39
+ pattern_errors << pattern.validate_all_examples if pattern.is_valid?
40
+ end
41
+ pattern_errors
42
+ end
43
+
26
44
  end
27
45
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pattern_matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
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: 2014-04-29 00:00:00.000000000 Z
12
+ date: 2014-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -66,6 +66,7 @@ extensions: []
66
66
  extra_rdoc_files: []
67
67
  files:
68
68
  - lib/pattern_matcher/config.rb
69
+ - lib/pattern_matcher/match.rb
69
70
  - lib/pattern_matcher/matcher.rb
70
71
  - lib/pattern_matcher/pattern.rb
71
72
  - lib/pattern_matcher/version.rb