Class: Rearview::MetricsValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- Rearview::MetricsValidator
- Defined in:
- lib/rearview/metrics_validator.rb
Instance Attribute Summary collapse
-
#message ⇒ Object
Returns the value of attribute message.
Instance Method Summary collapse
- #cache ⇒ Object
- #cache? ⇒ Boolean
- #client ⇒ Object
- #metric_valid?(metric) ⇒ Boolean
- #target_parser ⇒ Object
- #validate_each(record, attribute, value) ⇒ Object
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
3 4 5 |
# File 'lib/rearview/metrics_validator.rb', line 3 def @message end |
Instance Method Details
#cache ⇒ Object
42 43 44 |
# File 'lib/rearview/metrics_validator.rb', line 42 def cache @cache ||= {} end |
#cache? ⇒ Boolean
39 40 41 |
# File 'lib/rearview/metrics_validator.rb', line 39 def cache? [:cache] end |
#client ⇒ Object
45 46 47 |
# File 'lib/rearview/metrics_validator.rb', line 45 def client @client ||= Graphite::Client.new(Rearview.config.graphite_connection) end |
#metric_valid?(metric) ⇒ Boolean
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rearview/metrics_validator.rb', line 14 def metric_valid?(metric) valid = false metric_key = nil target_parser.parse(metric) if target_parser.error? = "contains an unparseable metric: #{metric} (#{target_parser.error})" else if target_parser.tree.comment? valid = true else metric_key = target_parser.tree.metric if metric_key.present? if cache? unless cache.has_key?(metric_key) cache[metric_key] = client.metric_exists?(metric_key) end valid = cache[metric_key] else valid = client.metric_exists?(metric_key) end end end end valid end |
#target_parser ⇒ Object
48 49 50 |
# File 'lib/rearview/metrics_validator.rb', line 48 def target_parser @target_parser ||= Graphite::TargetParser.new end |
#validate_each(record, attribute, value) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/rearview/metrics_validator.rb', line 4 def validate_each(record, attribute, value) if value.present? && value.respond_to?(:each) value.each do |metric| unless metric_valid?(metric) record.errors.add(attribute,( || "contains an invalid metric: #{metric}")) = nil end end end end |