Class: QueryGuard::Core::Context
- Inherits:
-
Object
- Object
- QueryGuard::Core::Context
- Defined in:
- lib/query_guard/core/context.rb
Overview
Holds the state of a single request or analysis session. Replaces implicit Thread.current usage for better testability and clarity.
Instance Attribute Summary collapse
-
#findings ⇒ Object
readonly
Returns the value of attribute findings.
-
#queries ⇒ Object
readonly
Returns the value of attribute queries.
Instance Method Summary collapse
-
#add_finding(finding) ⇒ Object
Add a finding to the context.
-
#add_query(sql:, duration_ms:, name: nil, started_at: nil, finished_at: nil) ⇒ Object
Add a captured query to the context.
- #clear ⇒ Object
-
#create_finding(analyzer_name:, rule_name:, severity: :warn, message:, metadata: {}, query: nil) ⇒ Object
Convenience: create and add a finding.
-
#finding_count ⇒ Object
Finding counts.
- #findings_by_severity(severity) ⇒ Object
-
#initialize ⇒ Context
constructor
A new instance of Context.
-
#query_count ⇒ Object
Query counts for easy checking.
- #to_h ⇒ Object
- #total_duration_ms ⇒ Object
Constructor Details
#initialize ⇒ Context
Returns a new instance of Context.
10 11 12 13 |
# File 'lib/query_guard/core/context.rb', line 10 def initialize @queries = [] @findings = [] end |
Instance Attribute Details
#findings ⇒ Object (readonly)
Returns the value of attribute findings.
8 9 10 |
# File 'lib/query_guard/core/context.rb', line 8 def findings @findings end |
#queries ⇒ Object (readonly)
Returns the value of attribute queries.
8 9 10 |
# File 'lib/query_guard/core/context.rb', line 8 def queries @queries end |
Instance Method Details
#add_finding(finding) ⇒ Object
Add a finding to the context
29 30 31 32 |
# File 'lib/query_guard/core/context.rb', line 29 def add_finding(finding) raise ArgumentError, "Must be a Finding" unless finding.is_a?(Finding) @findings << finding end |
#add_query(sql:, duration_ms:, name: nil, started_at: nil, finished_at: nil) ⇒ Object
Add a captured query to the context
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/query_guard/core/context.rb', line 16 def add_query(sql:, duration_ms:, name: nil, started_at: nil, finished_at: nil) query = Query.new( sql: sql, duration_ms: duration_ms, name: name, started_at: started_at, finished_at: finished_at ) @queries << query query end |
#clear ⇒ Object
66 67 68 69 |
# File 'lib/query_guard/core/context.rb', line 66 def clear @queries.clear @findings.clear end |
#create_finding(analyzer_name:, rule_name:, severity: :warn, message:, metadata: {}, query: nil) ⇒ Object
Convenience: create and add a finding
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/query_guard/core/context.rb', line 35 def create_finding(analyzer_name:, rule_name:, severity: :warn, message:, metadata: {}, query: nil) finding = Finding.new( analyzer_name: analyzer_name, rule_name: rule_name, severity: severity, message: , metadata: , query: query ) add_finding(finding) finding end |
#finding_count ⇒ Object
Finding counts
58 59 60 |
# File 'lib/query_guard/core/context.rb', line 58 def finding_count @findings.length end |
#findings_by_severity(severity) ⇒ Object
62 63 64 |
# File 'lib/query_guard/core/context.rb', line 62 def findings_by_severity(severity) @findings.select { |f| f.severity == severity.to_sym } end |
#query_count ⇒ Object
Query counts for easy checking
49 50 51 |
# File 'lib/query_guard/core/context.rb', line 49 def query_count @queries.length end |
#to_h ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/query_guard/core/context.rb', line 71 def to_h { query_count: query_count, total_duration_ms: total_duration_ms, findings: findings.map(&:to_h) } end |
#total_duration_ms ⇒ Object
53 54 55 |
# File 'lib/query_guard/core/context.rb', line 53 def total_duration_ms @queries.sum { |q| q.duration_ms } end |