Sha256: aca834c47ce9d1550a3925abbc0c08946aeb818c8341a655e548cbd8944ae3de

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'pathname'
require 'fileutils'
require 'delegate'
require 'logger'
require 'usable'
require 'duck_puncher/version'

module DuckPuncher
  autoload :JSONStorage, 'duck_puncher/json_storage'
  autoload :GemInstaller, 'duck_puncher/gem_installer'
  autoload :Duck, 'duck_puncher/duck'
  autoload :Ducks, 'duck_puncher/ducks'

  class << self
    attr_accessor :log

    def delegate_class(name)
      @delegations ||= {}
      @delegations[name] ||= Ducks[name].dup.delegated
    end

    # @description Extends functionality to a copy of the specified class
    def punch(*names)
      singular = names.size == 1
      punched_ducks = names.map { |name| Ducks[name].dup.classify }.compact
      punched_ducks = punched_ducks.first if singular
      punched_ducks
    end

    def punch!(*names)
      options = names.last.is_a?(Hash) ? names.pop : {}
      names.each do |name|
        duck = Ducks[name]
        log.warn %Q(Punching#{" #{options[:only]} onto"} #{name})
        unless duck.punch(options)
          log.error %Q(Failed to punch #{name}!)
        end
      end
      nil
    end

    def punch_all!
      log.warn 'Punching all ducks! Watch out!'
      Ducks.list.each &:punch
    end
  end

  # @description Default logger
  # @example Silence logging
  #
  #   `DuckPuncher.log.level = Logger::ERROR`
  #
  self.log = Logger.new(STDOUT).tap do |config|
    config.level = Logger::INFO
    config.formatter = proc { |*args| "#{args.first}: #{args.last.to_s}\n" }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
duck_puncher-2.9.2 lib/duck_puncher.rb