Sha256: 8e0b83e2c42702a68e571db888fec927a5fa210b4e393b1ed464c482a04bc8c8

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require "hook_me_up/version"

module HookMeUp
	module ClassMethods
		def hook_me_up(*args)
			hooks = args.last.is_a?(::Hash) ? args.pop : {}

			args.each do |method|
				original_method = self.instance_method(method)

				self.send(:define_method, method) do |*a|
					options = a.last
					if options.is_a?(::Hash) && options[:no_hook] == true && a.pop	
						result = original_method.bind(self).call(*a)
					elsif options.is_a?(::Hash) && options[:no_before_hook] == true && a.pop
						result = original_method.bind(self).call(*a)
						if hooks[:after]
							if hooks[:after].is_a? Proc
								hooks[:after].call(self, *a, result)
							else
								self.send(hooks[:after], *a, result)
							end
						end
					elsif options.is_a?(::Hash) && options[:no_after_hook] == true && a.pop
						if hooks[:before]
							if hooks[:before].is_a? Proc
								hooks[:before].call(self, *a)
							else
								self.send(hooks[:before], *a)
							end
						end

						result = original_method.bind(self).call(*a)
					else
						if hooks[:before]
							if hooks[:before].is_a? Proc
								hooks[:before].call(self, *a)
							else
								self.send(hooks[:before], *a)
							end
						end

						result = original_method.bind(self).call(*a)

						if hooks[:after]
							if hooks[:after].is_a? Proc
								hooks[:after].call(self, *a, result)
							else
								self.send(hooks[:after], *a, result)
							end
						end
					end

					result
				end
			end
		end
	end

	module InstanceMethods
		
	end
	
	def self.included(receiver)
		receiver.extend         ClassMethods
		receiver.send :include, InstanceMethods
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hook_me_up-0.0.2 lib/hook_me_up.rb