Class: PatternMatcher::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/pattern_matcher/pattern.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/pattern_matcher/pattern.rb', line 4

def description
  @description
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/pattern_matcher/pattern.rb', line 4

def name
  @name
end

#pattern_idObject

Returns the value of attribute pattern_id.



4
5
6
# File 'lib/pattern_matcher/pattern.rb', line 4

def pattern_id
  @pattern_id
end

#regexObject

Returns the value of attribute regex.



5
6
7
# File 'lib/pattern_matcher/pattern.rb', line 5

def regex
  @regex
end

#regex_stringObject

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_examplesObject

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

Returns:

  • (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

Returns:

  • (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_hObject



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_sObject



26
27
28
# File 'lib/pattern_matcher/pattern.rb', line 26

def to_s
  "#{@name} (#{is_valid?}) -- #{@regex_string} -- #{@description}"
end

#validate_all_examplesObject



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