Class: Class

Inherits:
Object show all
Defined in:
lib/dolzenko/acts_as.rb

Overview

My attempt at abstracting ‘acts_as_*` pattern. Need to get rid of `InstanceMethods` class to make it year 2010 compatible.

Instance Method Summary collapse

Instance Method Details

#acts_as(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dolzenko/acts_as.rb', line 4

def acts_as(*args)
  modules_with_options = []
  for arg in args
    if arg.is_a?(Module)
      modules_with_options << [arg]
    elsif arg.is_a?(Hash)
      raise ArgumentError, "Options without module" unless modules_with_options[-1][0].is_a?(Module)
      modules_with_options[-1][1] = arg
    end
  end
  
  klass = self
  for mod, options in modules_with_options
    klass.send(:instance_exec, options, &mod::ClassContextProc) if defined?(mod::ClassContextProc)
    klass.send(:include, mod::InstanceMethods) if defined?(mod::InstanceMethods)
    klass.extend(mod::ClassMethods) if defined?(mod::ClassMethods)
  end  
end