Sha256: 3c65b11ee638725a6526cb0b99d076f36c05b3463e029342bdc53333b7e464c7

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

require 'pathname'
require 'fileutils'
require 'delegate'
require 'logger'
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)
      names.each do |name|
        duck = Ducks[name]
        if duck.punched?
          log.info %Q(Already punched #{name})
        else
          log.warn %Q(Punching #{name} ducky)
          unless duck.punch
            log.error %Q(Failed to punch #{name}!)
          end
        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

def punch(name, val)
  DuckPuncher.delegate_class(name).new val
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
duck_puncher-2.6.0 lib/duck_puncher.rb
duck_puncher-2.5.1 lib/duck_puncher.rb