Sha256: 576d125ae772c5b863cd0d0ff82509411689d17a44b396871247c2de1dacbd76

Contents?: true

Size: 970 Bytes

Versions: 13

Compression:

Stored size: 970 Bytes

Contents

module DuckPuncher
  # @note When updating this file please update comment regarding this module in duck_puncher.rb
  module Decoration
    def decorators
      @decorators ||= ancestral_hash
    end

    def build_decorator_class(*ducks)
      targets = ducks.sort.map(&:target)
      decorator_class = DelegateClass(targets.first)
      DuckPuncher.redefine_constant "#{targets.first.to_s.tr(':', '')}Delegator", decorator_class
      ducks.each { |duck| duck.punch target: decorator_class, method: :prepend }
      decorator_class
    end

    def decorate(context, target)
      cached_decorators[target].inject(context) { |me, (_, decorator)| decorator.new(me) }
    end

    def cached_decorators
      @cached_decorators ||= Hash.new do |me, target|
        me[target] = DuckPuncher.decorators.select { |klass, _| klass >= target }
      end
    end

    def undecorate(obj)
      obj = obj.__getobj__ while obj.respond_to? :__getobj__
      obj
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
duck_puncher-5.0.1 lib/duck_puncher/decoration.rb
duck_puncher-5.0.0 lib/duck_puncher/decoration.rb
duck_puncher-4.5.1 lib/duck_puncher/decoration.rb
duck_puncher-4.5.0 lib/duck_puncher/decoration.rb
duck_puncher-4.4.2 lib/duck_puncher/decoration.rb
duck_puncher-4.4.1 lib/duck_puncher/decoration.rb
duck_puncher-4.4.0 lib/duck_puncher/decoration.rb
duck_puncher-4.3.1 lib/duck_puncher/decoration.rb
duck_puncher-4.3.0 lib/duck_puncher/decoration.rb
duck_puncher-4.2.3 lib/duck_puncher/decoration.rb
duck_puncher-4.2.2 lib/duck_puncher/decoration.rb
duck_puncher-4.2.1 lib/duck_puncher/decoration.rb
duck_puncher-4.2.0 lib/duck_puncher/decoration.rb