Sha256: 87cd14bfa3beeb4fa28c77e3324ae27692ee5c0cb58f65fb027861c844881713

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'singleton'

module GitHooker
  class RegistrationError < StandardError; end
  class Hook
    include Singleton

    def self.method_missing(*args, &block)
      return super unless self.instance.respond_to? args.first
      self.instance.send(*args, &block)
    end

    def initialize
      @sections = []
      @section  = nil
    end

    def register(options = {}, &block)
      raise ArgumentError, "Missing required block to #register" unless block_given?

      @phase = (options.delete(:phase) || :any).to_s.to_sym
      unless GitHooker::VALID_PHASES.include? phase
        raise ArgumentError, "Phase must be one of #{GitHooker::VALID_PHASES.join(', ')}"
      end

      instance_eval(&block) if Repo.match_phase(@phase)
      self
    end

    def run
      @sections.collect { |section| section.run }.all?
    end

    # DSL methods

    def section(name)
      @sections << (@section = Section.new(name))
      self
    end

    def sections
      @sections
    end

    def stop_on_error(value)
      raise RegistrationError, "#stop_on_error called before section defined" unless @section
      @section.stop_on_error = value
    end

    def perform(title, options = {}, &block)
      raise RegistrationError, "#perform called before section defined" unless @section
      raise ArgumentError, "Missing required block to #perform" unless block_given?
      @section << Action.new(title, options.delete(:phase) || @phase, &block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
githooker-0.2.10 lib/githooker/hook.rb