Class: Marley::Plugins::Plugin

Inherits:
Object
  • Object
show all
Extended by:
Utils::ClassAttrs
Defined in:
lib/marley/plugin.rb

Instance Method Summary collapse

Methods included from Utils::ClassAttrs

class_attr, lazy_class_attrs

Constructor Details

#initialize(opts = {}) ⇒ Plugin

Returns a new instance of Plugin.



10
11
12
# File 'lib/marley/plugin.rb', line 10

def initialize(opts={})
  config(opts)
end

Instance Method Details

#apply(*klasses) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/marley/plugin.rb', line 13

def apply(*klasses)
  plugin=self.class
  klasses.flatten.each do |klass|
    resource=klass.class==String ? MR.const_get(klass) : klass
    @opts[:required_plugins].to_a.each do |p|
      Marley.plugin(p).apply(klass)
    end
    #below 4 lines vs 2 is for 1.8-1.9 and is fucking awful!
    plugin.constants.include?('ClassMethods') && resource.extend(plugin.const_get('ClassMethods'))
    plugin.constants.include?(:ClassMethods) && resource.extend(plugin.const_get(:ClassMethods))
    plugin.constants.include?('InstanceMethods') && resource.send(:include, plugin.const_get('InstanceMethods'))
    plugin.constants.include?(:InstanceMethods) && resource.send(:include, plugin.const_get(:InstanceMethods))
    if @opts[:lazy_class_attrs] || @opts[:class_attrs] 
      resource.extend Marley::Utils::ClassAttrs
    end
    if lazy_attrs=@opts[:lazy_class_attrs]
      resource.lazy_class_attrs(lazy_attrs[0],lazy_attrs[1..-1])
    end
    @opts[:class_attrs].to_a.each do |att|
      resource.class_attr(*att)
    end
  end
  nil
end

#config(opts) ⇒ Object



37
38
39
# File 'lib/marley/plugin.rb', line 37

def config(opts)
  @opts=(@opts || self.class.default_opts || {}).merge(opts)
end