Sha256: 821ca70745663336a9e9075dbf7f10a3695f459f9f80f169fc9f2a2d8e577ede

Contents?: true

Size: 963 Bytes

Versions: 1

Compression:

Stored size: 963 Bytes

Contents

module DuckPuncher
  class Duck
    attr_accessor :name, :options

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

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

    def punch(target = nil)
      if options[:if] && !options[:if].call
        DuckPuncher.log.warn %Q(Failed to punch #{name}!)
        return nil
      end
      options[:before].call if options[:before]
      (target || klass).send :include, DuckPuncher::Ducks.const_get(name)
      options[:after].call if options[:after]
      @punched = true
    end

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

    def punched?
      @punched
    end

    def delegated
      DelegateClass(klass).tap &method(:punch)
    end

    def classify
      Class.new(klass).tap &method(:punch)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
duck_puncher-2.7.0 lib/duck_puncher/duck.rb