class Checky::Checker

Attributes

storage[R]

Public Class Methods

new() click to toggle source
# File lib/checky/checker.rb, line 8
def initialize
  @storage = OpenStruct.new(checky_blocks: {}, checky_results: {})
  @self_before_instance_eval = false
end

Public Instance Methods

check(&block) click to toggle source
# File lib/checky/checker.rb, line 13
def check(&block)
  @self_before_instance_eval = eval 'self', block.binding
  instance_eval(&block)
  with_hooks { check_result }
  @storage.checky_final_result
end
method_missing(method, *args, &block) click to toggle source

rubocop:disable Style/MethodMissing

# File lib/checky/checker.rb, line 28
def method_missing(method, *args, &block)
  raise Checky::Exception unless methods.include?("populate_#{method}".to_sym)
  @storage.send("#{method}=", send("populate_#{method}", *args, &block))
  @storage.checky_blocks[method.to_sym] = block if block_given?
rescue Checky::Exception
  @self_before_instance_eval.send method, *args, &block if @self_before_instance_eval.present?
end
respond_to_missing?(method, _include_private = false) click to toggle source

:nocov: :reek: BooleanParameter

# File lib/checky/checker.rb, line 22
def respond_to_missing?(method, _include_private = false)
  methods.include?("run_#{method}".to_sym)
end

Private Instance Methods

active_validators() click to toggle source
# File lib/checky/checker.rb, line 50
def active_validators
  @storage.to_h.keys.reject { |key| key.to_s.start_with?('checky_') }
end
check_result() click to toggle source

rubocop:enable Style/MethodMissing

# File lib/checky/checker.rb, line 39
def check_result
  @storage.checky_final_result = active_validators.all? do |validator_name|
    block = @storage.checky_blocks[validator_name]
    @storage.checky_results[validator_name] = if block.present?
                                                instance_eval(&block)
                                              else
                                                send("check_#{validator_name}")
                                              end
  end
end
fire_hook(hook_name) click to toggle source
# File lib/checky/checker.rb, line 60
def fire_hook(hook_name)
  active_validators.each { |validator_name| send("#{hook_name}_#{validator_name}") }
end
with_hooks() { || ... } click to toggle source
# File lib/checky/checker.rb, line 54
def with_hooks
  fire_hook :before
  yield if block_given?
  fire_hook :after
end