Class: Querly::Rule
- Inherits:
-
Object
- Object
- Querly::Rule
- Defined in:
- lib/querly/rule.rb
Defined Under Namespace
Classes: Example, 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.
-
#examples ⇒ Object
readonly
Returns the value of attribute 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:, examples:) ⇒ 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:, examples:) ⇒ Rule
Returns a new instance of Rule.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/querly/rule.rb', line 28 def initialize(id:, messages:, patterns:, sources:, tags:, before_examples:, after_examples:, justifications:, examples:) @id = id @patterns = patterns @sources = sources @messages = @justifications = justifications @before_examples = before_examples @after_examples = after_examples @tags = @examples = examples end |
Instance Attribute Details
#after_examples ⇒ Object (readonly)
Returns the value of attribute after_examples.
24 25 26 |
# File 'lib/querly/rule.rb', line 24 def after_examples @after_examples end |
#before_examples ⇒ Object (readonly)
Returns the value of attribute before_examples.
23 24 25 |
# File 'lib/querly/rule.rb', line 23 def before_examples @before_examples end |
#examples ⇒ Object (readonly)
Returns the value of attribute examples.
25 26 27 |
# File 'lib/querly/rule.rb', line 25 def examples @examples end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
17 18 19 |
# File 'lib/querly/rule.rb', line 17 def id @id end |
#justifications ⇒ Object (readonly)
Returns the value of attribute justifications.
22 23 24 |
# File 'lib/querly/rule.rb', line 22 def justifications @justifications end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
19 20 21 |
# File 'lib/querly/rule.rb', line 19 def @messages end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
18 19 20 |
# File 'lib/querly/rule.rb', line 18 def patterns @patterns end |
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
21 22 23 |
# File 'lib/querly/rule.rb', line 21 def sources @sources end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
26 27 28 |
# File 'lib/querly/rule.rb', line 26 def @tags end |
Class Method Details
.load(hash) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/querly/rule.rb', line 59 def self.load(hash) id = hash["id"] raise InvalidRuleHashError, "id is missing" unless id srcs = case hash["pattern"] when Array hash["pattern"] when nil [] else [hash["pattern"]] end raise InvalidRuleHashError, "pattern is missing" if srcs.empty? patterns = srcs.map.with_index do |src, index| case src when String subject = src where = {} when Hash subject = src['subject'] where = Hash[src['where'].map {|k,v| [k.to_sym, translate_where(v)] }] end begin Pattern::Parser.parse(subject, where: where) rescue Racc::ParseError => exn raise PatternSyntaxError, "Pattern syntax error: rule=#{hash["id"]}, index=#{index}, pattern=#{Rainbow(subject.split("\n").first).blue}, where=#{where.inspect}: #{exn}" end end = Array(hash["message"]) raise InvalidRuleHashError, "message is missing" if .empty? = Set.new(Array(hash["tags"])) examples = [hash["examples"]].compact.flatten.map do |example| raise(InvalidRuleHashError, "Example should have at least before or after, #{example.inspect}") unless example.key?("before") || example.key?("after") Example.new(before: example["before"], after: example["after"]) end 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, examples: examples) end |
.translate_where(value) ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/querly/rule.rb', line 113 def self.translate_where(value) Array(value).map do |v| case v when /\A\/(.*)\/\Z/ Regexp.new($1) else v end end end |
Instance Method Details
#match?(identifier: nil, tags: nil) ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/querly/rule.rb', line 40 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 |