lib/duck_puncher.rb in duck_puncher-4.1.0 vs lib/duck_puncher.rb in duck_puncher-4.2.0

- old
+ new

@@ -1,6 +1,5 @@ -# Standard lib require 'pathname' require 'fileutils' require 'logger' require 'set' require 'delegate' @@ -10,83 +9,53 @@ # Our stuff require 'duck_puncher/version' require 'duck_puncher/registration' require 'duck_puncher/decoration' +require 'duck_puncher/utilities' +require 'duck_puncher/ancestral_hash' +require 'duck_puncher/duck' +require 'duck_puncher/ducks' module DuckPuncher - autoload :JSONStorage, 'duck_puncher/json_storage' autoload :GemInstaller, 'duck_puncher/gem_installer' - autoload :Duck, 'duck_puncher/duck' - autoload :Ducks, 'duck_puncher/ducks' + autoload :JSONStorage, 'duck_puncher/json_storage' class << self - include Registration, Decoration + # @description Include additional functionality + # Registration[:register, :deregister] + # Decoration[:decorators, :build_decorator_class, :decorate, :cached_decorators, :undecorate] + # Utilities[:lookup_constant, :redefine_constant] + # AncestralHash[:ancestral_hash] + include Registration, Decoration, Utilities, AncestralHash - attr_accessor :log - alias_method :logger, :log + attr_accessor :logger - def punch!(*classes) - options = classes.last.is_a?(Hash) ? classes.pop : {} + # Backwards compatibility + alias_method :log, :logger + alias_method :log=, :logger= + + def call(*args) + options = args.last.is_a?(Hash) ? args.pop : {} + classes = args.any? ? args : Ducks.list.keys classes.each do |klass| klass = lookup_constant(klass) Ducks[klass].sort.each do |duck| - punches = options[:only] || Ducks::Module.instance_method(:local_methods).bind(duck.mod).call - log.info %Q(#{duck.target}#{" <-- #{punches}" if Array(punches).any?}) + punches = Array(options[:only] || Ducks::Module.instance_method(:local_methods).bind(duck.mod).call) options[:target] = klass + logger.info %Q(#{klass}#{" <-- #{duck.mod.name}#{punches}" if punches.any?}) unless duck.punch(options) - log.error %Q(Failed to punch #{name}) + logger.error %Q(Failed to punch #{name}) end end end nil end - def punch_all! - punch! *Ducks.list.keys - end - - def lookup_constant(const) - return const if Module === const - if const.to_s.respond_to?(:constantize) - const.to_s.constantize - else - const.to_s.split('::').inject(Object) { |k, part| k.const_get(part) } - end - rescue NameError => e - log.error "#{e.class}: #{e.message}" - nil - end - - def redefine_constant(name, const) - if const_defined? name - remove_const name - end - const_set name, const - end - - def ancestral_hash - Hash.new { |me, klass| me[klass.superclass] if klass.respond_to?(:superclass) } - end + # Backwards compatibility + alias punch_all! call + alias punch! call end - - self.log = Logger.new(STDOUT).tap do |config| - config.level = Logger::INFO - config.formatter = proc { |*args| "#{args.first}: #{args.last.to_s}\n" } - end - - log.level = Logger::ERROR - - ducks = [ - [String, Ducks::String], - [Array, Ducks::Array], - [Numeric, Ducks::Numeric], - [Hash, Ducks::Hash], - [Object, Ducks::Object], - [Module, Ducks::Module], - [Method, Ducks::Method, { before: ->(*) { DuckPuncher::GemInstaller.initialize! } }], - ] - ducks << ['ActiveRecord::Base', Ducks::ActiveRecord] if defined? ::ActiveRecord - ducks.each do |duck| - register *duck - end end + +# Everyone likes defaults +require 'duck_puncher/defaults'