Class: Class

Inherits:
Object show all
Defined in:
lib/wrap.rb,
lib/wrap.rb,
lib/event.rb,
lib/system.rb

Instance Method Summary collapse

Instance Method Details

#cache_method(method_name, cache_name = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/system.rb', line 13

def cache_method(method_name, cache_name = nil)
  cache_attr_name = cache_name
  cache_attr_name = "#{method_name.to_s}_cache" if cache_name.blank?

  class_eval(<<-EOS, __FILE__, __LINE__)
    def #{method_name.to_s}_with_cache_support(*args)
      @#{cache_attr_name} ||= {}
      @#{cache_attr_name}[args] ||= #{method_name.to_s}_without_cache_support( *args )
    end

    alias_method_chain :#{method_name.to_s}, :cache_support
  EOS

end

#subscribe(target, options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/event.rb', line 85

def subscribe( target, options )
  return if options[:for].blank?
  
  unless self.respond_to?( :new_with_subscribe_support )
    code_to_eval = <<-EOC
      class << self
        def register_subscribe(target, options)
          es = @@event_subscribes ||= {}
          est = es[options[:for]] ||= [] 
          est << target
        end
        
        def new_with_subscribe_support(*constructor_arguments)
          instance = new_without_subscribe_support(*constructor_arguments)
          @@event_subscribes.each_key do |event_name|
            ::MPT::Event.subscribe( event_name, :owner => instance ) do |*args|
              instance.send( "trigger_event", event_name, *args )
            end
          end
          instance
        end
        
        alias_method_chain :new, :subscribe_support
      end
      
      private
      def trigger_event(name, *args)
        @@event_subscribes[name].each do |method_name|
          self.send( method_name, *args )
        end
      end
    EOC
    
    self.class_eval( code_to_eval, __FILE__, __LINE__ )
  end
  
  self.register_subscribe( target, options )
  
end

#wrap_it(target, options = { :using => :run, :scope => :before }, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/wrap.rb', line 55

def wrap_it(target, options = { :using => :run, :scope => :before }, &block)
  return if options[:by].nil? && block.nil?
  
  targets = target
  targets = [target] unless target.instance_of?( Array )
  targets.each do |tgt|
    wrap_it_single(tgt, options, &block)
  end      
end

#wrap_with(target, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/wrap.rb', line 65

def wrap_with(target, options = {})
  return if options.blank?
  
  targets = target
  targets = [target] unless target.instance_of?( Array )
  targets.each do |tgt|
    wrap_with_single(tgt, options)
  end
end

#wrappable(accessor_name, wrap_name) ⇒ Object Also known as: wrapable



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/wrap.rb', line 129

def wrappable(accessor_name, wrap_name)
  acc_name = accessor_name.to_s
	code = <<-EOS
	  def #{acc_name}_with_wrap_support
       MPT::Wrap.get( "#{wrap_name}", 
         Proc.new do 
           if self.respond_to?( :"#{acc_name}_without_wrap_support" )
             self.send :"#{acc_name}_without_wrap_support"
           end  
         end)
	  end

     if self.public_instance_methods.include?( "#{acc_name}" )
	    alias_method_chain :"#{acc_name}", :wrap_support
	  else
	    alias :"#{acc_name}" :"#{acc_name}_with_wrap_support"
    end
	EOS
	
	class_eval(code, __FILE__, __LINE__)
end