Sha256: 361359af7d2f5a9ef56ecf4dbf23d7e24157f07b51fd0859c7486e2eb6287450

Contents?: true

Size: 978 Bytes

Versions: 4

Compression:

Stored size: 978 Bytes

Contents

module DuckPuncher
  class Duck
    attr_accessor :name, :options

    def initialize(name, options = {})
      @name = name
      @options = options
    end

    def load_path
      "duck_puncher/ducks/#{name.to_s.gsub(/\B([A-Z])/, '_\1').downcase}"
    end

    def punch(opts = {})
      if options[:if] && !options[:if].call
        DuckPuncher.log.warn %Q(Failed to punch #{name}!)
        return nil
      end
      target = opts.delete(:target) || klass
      options[:before].call(target) if options[:before]
      target.extend Usable
      target.usable DuckPuncher::Ducks.const_get(name), opts
      options[:after].call(target) if options[:after]
      target
    end

    def klass
      @klass ||= (options[:class] || name).to_s.split('::').inject(Kernel) { |k, part| k.const_get part }
    end

    def delegated
      DelegateClass(klass).tap { |k| punch target: k }
    end

    def classify
      Class.new(klass).tap { |k| punch target: k }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
duck_puncher-2.9.3 lib/duck_puncher/duck.rb
duck_puncher-2.9.2 lib/duck_puncher/duck.rb
duck_puncher-2.9.1 lib/duck_puncher/duck.rb
duck_puncher-2.9.0 lib/duck_puncher/duck.rb