Class: RKit::Decoration::Class

Inherits:
Object
  • Object
show all
Defined in:
lib/r_kit/decoration/class.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(decorated, from: nil, sleeping: nil, &block) ⇒ Class

Returns a new instance of Class.



8
9
10
11
12
13
# 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

Class Method Details

.new(*args, &block) ⇒ Object



3
4
5
# File 'lib/r_kit/decoration/class.rb', line 3

def self.new *args, &block
  super.decorator
end

Instance Method Details

#decoratorObject



16
17
18
19
20
21
22
23
24
25
26
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object

TODO: method module_to_class(superclass)



59
60
61
62
63
64
# 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_superclassObject



29
30
31
32
33
34
35
# File 'lib/r_kit/decoration/class.rb', line 29

def decorator_superclass
  if @decorated <=> Enumerable
    RKit::Decoration::Collection
  else
    RKit::Decoration::Object
  end
end