Sha256: 0e79fccf1d15f2bb71d28a3ff51f74610db4f242db724bf9936fee460ae1e963

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module Diagnostics
  class Check < Struct.new(:cls)

    def self.add(cls)
      Diagnostics.checks << check = new(cls)
      check
    end

    def self.find(name)
      Diagnostics.checks.select {|c| c.name == name.to_s }[0]
    end

    def name
      cls.name
    end

    def instance
      @instance ||= cls.new
    end

    def passed?
      @passed_method_result ||= call_method(:passed)
    end

    def warning?
      @warning_method_result ||= call_method(:warning)
    end

    def failed?
      @failed_method_result ||= call_method(:failed) || (
        !passed? if instance.respond_to?(:passed)
      )
    end

    def status
      return :passed  if passed?
      return :warning if warning?
      return :failed  if failed?
      :none
    end

    def data
      @data_method_result ||= if instance.respond_to?(:data)
        instance.data.call(data_group) && data_group
      else
        data_group
      end
    end

  private

    def data_group
      @data_group ||= DataGroup.new
    end

    def call_method(meth)
      instance.respond_to?(meth) && instance.send(meth)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
diagnostics-0.0.4 lib/diagnostics/check.rb