Class: Querly::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/querly/rule.rb

Defined Under Namespace

Classes: InvalidRuleHashError, PatternSyntaxError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
  @messages = messages
  @justifications = justifications
  @before_examples = before_examples
  @after_examples = after_examples
  @tags = tags
end

Instance Attribute Details

#after_examplesObject (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_examplesObject (readonly)

Returns the value of attribute before_examples.



9
10
11
# File 'lib/querly/rule.rb', line 9

def before_examples
  @before_examples
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/querly/rule.rb', line 3

def id
  @id
end

#justificationsObject (readonly)

Returns the value of attribute justifications.



8
9
10
# File 'lib/querly/rule.rb', line 8

def justifications
  @justifications
end

#messagesObject (readonly)

Returns the value of attribute messages.



5
6
7
# File 'lib/querly/rule.rb', line 5

def messages
  @messages
end

#patternsObject (readonly)

Returns the value of attribute patterns.



4
5
6
# File 'lib/querly/rule.rb', line 4

def patterns
  @patterns
end

#sourcesObject (readonly)

Returns the value of attribute sources.



7
8
9
# File 'lib/querly/rule.rb', line 7

def sources
  @sources
end

#tagsObject (readonly)

Returns the value of attribute tags.



11
12
13
# File 'lib/querly/rule.rb', line 11

def tags
  @tags
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

  messages = Array(hash["message"])
  raise InvalidRuleHashError, "message is missing" if messages.empty?

  tags = Set.new(Array(hash["tags"]))
  before_examples = Array(hash["before"])
  after_examples = Array(hash["after"])
  justifications = Array(hash["justification"])

  Rule.new(id: id,
           messages: messages,
           patterns: patterns,
           sources: srcs,
           tags: tags,
           before_examples: before_examples,
           after_examples: after_examples,
           justifications: justifications)
end

Instance Method Details

#match?(identifier: nil, tags: nil) ⇒ Boolean

Returns:

  • (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 tags
    unless tags.subset?(self.tags)
      return false
    end
  end

  true
end