Class: Querly::Rule

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

Defined Under Namespace

Classes: Example, 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:, 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 = messages
  @justifications = justifications
  @before_examples = before_examples
  @after_examples = after_examples
  @tags = tags
  @examples = examples
end

Instance Attribute Details

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

Returns the value of attribute before_examples.



23
24
25
# File 'lib/querly/rule.rb', line 23

def before_examples
  @before_examples
end

#examplesObject (readonly)

Returns the value of attribute examples.



25
26
27
# File 'lib/querly/rule.rb', line 25

def examples
  @examples
end

#idObject (readonly)

Returns the value of attribute id.



17
18
19
# File 'lib/querly/rule.rb', line 17

def id
  @id
end

#justificationsObject (readonly)

Returns the value of attribute justifications.



22
23
24
# File 'lib/querly/rule.rb', line 22

def justifications
  @justifications
end

#messagesObject (readonly)

Returns the value of attribute messages.



19
20
21
# File 'lib/querly/rule.rb', line 19

def messages
  @messages
end

#patternsObject (readonly)

Returns the value of attribute patterns.



18
19
20
# File 'lib/querly/rule.rb', line 18

def patterns
  @patterns
end

#sourcesObject (readonly)

Returns the value of attribute sources.



21
22
23
# File 'lib/querly/rule.rb', line 21

def sources
  @sources
end

#tagsObject (readonly)

Returns the value of attribute tags.



26
27
28
# File 'lib/querly/rule.rb', line 26

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

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

  tags = 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: messages,
           patterns: patterns,
           sources: srcs,
           tags: 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

Returns:

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

  true
end