Class: PatternMatcher::Pattern
- Inherits:
-
Object
- Object
- PatternMatcher::Pattern
- Defined in:
- lib/pattern_matcher/pattern.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#pattern_id ⇒ Object
Returns the value of attribute pattern_id.
-
#regex ⇒ Object
Returns the value of attribute regex.
-
#regex_string ⇒ Object
Returns the value of attribute regex_string.
-
#valid_examples ⇒ Object
Returns the value of attribute valid_examples.
Instance Method Summary collapse
-
#initialize(hash) ⇒ Pattern
constructor
A new instance of Pattern.
- #is_valid? ⇒ Boolean
- #pattern_example_valid?(example) ⇒ Boolean
- #to_h ⇒ Object
- #to_s ⇒ Object
- #validate_all_examples ⇒ Object
Constructor Details
#initialize(hash) ⇒ Pattern
Returns a new instance of Pattern.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/pattern_matcher/pattern.rb', line 7 def initialize(hash) if hash.is_a?(Hash) @pattern_id = hash[:pattern_id] @name = hash["name"] @regex_string = hash["regex"] @regex = Matcher.string_to_regex(@regex_string) @description = hash["description"] @valid_examples = hash["valid_examples"] || [] end end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
4 5 6 |
# File 'lib/pattern_matcher/pattern.rb', line 4 def description @description end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/pattern_matcher/pattern.rb', line 4 def name @name end |
#pattern_id ⇒ Object
Returns the value of attribute pattern_id.
4 5 6 |
# File 'lib/pattern_matcher/pattern.rb', line 4 def pattern_id @pattern_id end |
#regex ⇒ Object
Returns the value of attribute regex.
5 6 7 |
# File 'lib/pattern_matcher/pattern.rb', line 5 def regex @regex end |
#regex_string ⇒ Object
Returns the value of attribute regex_string.
4 5 6 |
# File 'lib/pattern_matcher/pattern.rb', line 4 def regex_string @regex_string end |
#valid_examples ⇒ Object
Returns the value of attribute valid_examples.
4 5 6 |
# File 'lib/pattern_matcher/pattern.rb', line 4 def valid_examples @valid_examples end |
Instance Method Details
#is_valid? ⇒ Boolean
40 41 42 |
# File 'lib/pattern_matcher/pattern.rb', line 40 def is_valid? return !@regex.nil? && !@pattern_id.nil? end |
#pattern_example_valid?(example) ⇒ Boolean
44 45 46 |
# File 'lib/pattern_matcher/pattern.rb', line 44 def pattern_example_valid?(example) Matcher.match_regex_in_text(@regex, example) end |
#to_h ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/pattern_matcher/pattern.rb', line 30 def to_h { :name => @name, :is_valid => is_valid?, :regex_string => @regex_string, :description => @description, :valid_examples => @valid_examples } end |
#to_s ⇒ Object
26 27 28 |
# File 'lib/pattern_matcher/pattern.rb', line 26 def to_s "#{@name} (#{is_valid?}) -- #{@regex_string} -- #{@description}" end |
#validate_all_examples ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/pattern_matcher/pattern.rb', line 18 def validate_all_examples failures = [] @valid_examples.each do |example| failures << example if !pattern_example_valid? example end failures end |