Class: Querly::Rule
- Inherits:
-
Object
- Object
- Querly::Rule
- Defined in:
- lib/querly/rule.rb
Defined Under Namespace
Classes: InvalidRuleHashError, PatternSyntaxError
Instance Attribute Summary collapse
-
#after_examples ⇒ Object
readonly
Returns the value of attribute after_examples.
-
#before_examples ⇒ Object
readonly
Returns the value of attribute before_examples.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#justifications ⇒ Object
readonly
Returns the value of attribute justifications.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(id:, messages:, patterns:, sources:, tags:, before_examples:, after_examples:, justifications:) ⇒ Rule
constructor
A new instance of Rule.
- #match?(identifier: nil, tags: nil) ⇒ Boolean
Constructor Details
#initialize(id:, messages:, patterns:, sources:, tags:, before_examples:, after_examples:, justifications:) ⇒ Rule
Returns a new instance of Rule.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/querly/rule.rb', line 13 def initialize(id:, messages:, patterns:, sources:, tags:, before_examples:, after_examples:, justifications:) @id = id @patterns = patterns @sources = sources = @justifications = justifications @before_examples = before_examples @after_examples = after_examples = end |
Instance Attribute Details
#after_examples ⇒ Object (readonly)
Returns the value of attribute after_examples.
10 11 12 |
# File 'lib/querly/rule.rb', line 10 def after_examples @after_examples end |
#before_examples ⇒ Object (readonly)
Returns the value of attribute before_examples.
9 10 11 |
# File 'lib/querly/rule.rb', line 9 def before_examples @before_examples end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/querly/rule.rb', line 3 def id @id end |
#justifications ⇒ Object (readonly)
Returns the value of attribute justifications.
8 9 10 |
# File 'lib/querly/rule.rb', line 8 def justifications @justifications end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
5 6 7 |
# File 'lib/querly/rule.rb', line 5 def end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
4 5 6 |
# File 'lib/querly/rule.rb', line 4 def patterns @patterns end |
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
7 8 9 |
# File 'lib/querly/rule.rb', line 7 def sources @sources end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
11 12 13 |
# File 'lib/querly/rule.rb', line 11 def end |
Class Method Details
.load(hash) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/querly/rule.rb', line 43 def self.load(hash) id = hash["id"] raise InvalidRuleHashError, "id is missing" unless id srcs = Array(hash["pattern"]) raise InvalidRuleHashError, "pattern is missing" if srcs.empty? patterns = srcs.map.with_index do |src, index| begin Pattern::Parser.parse(src) rescue Racc::ParseError => exn raise PatternSyntaxError, "Pattern syntax error: rule=#{hash["id"]}, index=#{index}, pattern=#{Rainbow(src.split("\n").first).blue}: #{exn}" end end = Array(hash["message"]) raise InvalidRuleHashError, "message is missing" if .empty? = Set.new(Array(hash["tags"])) before_examples = Array(hash["before"]) after_examples = Array(hash["after"]) justifications = Array(hash["justification"]) Rule.new(id: id, messages: , patterns: patterns, sources: srcs, tags: , before_examples: before_examples, after_examples: after_examples, justifications: justifications) end |
Instance Method Details
#match?(identifier: nil, tags: nil) ⇒ Boolean
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/querly/rule.rb', line 24 def match?(identifier: nil, tags: nil) if identifier unless id == identifier || id.start_with?(identifier + ".") return false end end if unless .subset?(self.) return false end end true end |