class RKit::Decoration::Class

Public Class Methods

new(*args, &block) click to toggle source
Calls superclass method Object::new
# File lib/r_kit/decoration/class.rb, line 3
def self.new *args, &block
  super.decorator
end
new(decorated, from: nil, sleeping: nil, &block) click to toggle source
# File lib/r_kit/decoration/class.rb, line 8
def initialize decorated, from: nil, sleeping: nil, &block
  @decorated = decorated
  @from = from
  @sleeping = sleeping
  @block = block || proc{}
end

Public Instance Methods

decorator() click to toggle source
# File lib/r_kit/decoration/class.rb, line 16
def decorator
  decorator_from(@from)
    .tap{ |decorator| decorator.instance_variable_set "@decorated_class", @decorated }
    .tap{ |decorator|
      unless decorator.decorated_class.method_defined? :"#{ decorator.decorated_class.demodulize.underscore }"
        decorator.class_eval{ alias :"#{ decorated_class.demodulize.underscore }" :__getobj__ }
      end
    }
    .tap{ |decorator| decorator.send :include, @sleeping }
    .tap{ |decorator| decorator.class_eval &@block }
end
decorator_from_class(base) click to toggle source
# File lib/r_kit/decoration/class.rb, line 66
def decorator_from_class base
  if base <=> RKit::Decoration::Base
    base
  else
    # TODO: method class_to_module
    # TODO: method class.add_ancestor(superclass)
    decorator_superclass = self.decorator_superclass

    base.tap do |base|
      base.send :include, Module.new{ include refine(decorator_superclass){} }
      base.extend Module.new{ include refine(decorator_superclass.singleton_class){} }
      base.instance_variable_set "@decorated_class", @decorated
      base.class_eval{ alias :"#{ decorated_class.demodulize.underscore }" :__getobj__ }
    end
  end
end
decorator_from_module(mod) click to toggle source

TODO: method module_to_class(superclass)

# File lib/r_kit/decoration/class.rb, line 59
def decorator_from_module mod
  mod
    .namespace
    .const_replace mod.demodulize,
      Class.new(decorator_superclass){ include mod }
end
decorator_from_symbol(name)
decorator_superclass() click to toggle source
# File lib/r_kit/decoration/class.rb, line 29
def decorator_superclass
  if @decorated <=> Enumerable
    RKit::Decoration::Collection
  else
    RKit::Decoration::Object
  end
end

Protected Instance Methods

decorator_from(from) click to toggle source
# File lib/r_kit/decoration/class.rb, line 39
          def decorator_from from
  send "decorator_from_#{ from.class.underscore }", from
end
decorator_from_nil_class(*_) click to toggle source
# File lib/r_kit/decoration/class.rb, line 44
          def decorator_from_nil_class *_
  decorator_from "#{ @decorated.demodulize }Decorator"
end
decorator_from_string(name) click to toggle source
# File lib/r_kit/decoration/class.rb, line 48
          def decorator_from_string name
  decorator_from(
    @decorated
      .namespace
      .const_get name, default: Class.new(decorator_superclass)
  )
end
Also aliased as: decorator_from_symbol