module DuckPuncher::Decoration
@note When updating this file please update comment regarding this module in duck_puncher.rb
Public Instance Methods
build_decorator_class(*ducks)
click to toggle source
# File lib/duck_puncher/decoration.rb, line 8 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
cached_decorators()
click to toggle source
# File lib/duck_puncher/decoration.rb, line 20 def cached_decorators @cached_decorators ||= Hash.new do |me, target| me[target] = DuckPuncher.decorators.select { |klass, _| klass >= target } end end
decorate(context, target)
click to toggle source
# File lib/duck_puncher/decoration.rb, line 16 def decorate(context, target) cached_decorators[target].inject(context) { |me, (_, decorator)| decorator.new(me) } end
decorators()
click to toggle source
# File lib/duck_puncher/decoration.rb, line 4 def decorators @decorators ||= ancestral_hash end
undecorate(obj)
click to toggle source
# File lib/duck_puncher/decoration.rb, line 26 def undecorate(obj) obj = obj.__getobj__ while obj.respond_to? :__getobj__ obj end