Class: Herpes::Module

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/herpes/module.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *aliases, &block) ⇒ Module

Returns a new instance of Module.



34
35
36
37
38
39
40
41
42
43
# File 'lib/herpes/module.rb', line 34

def initialize (name, *aliases, &block)
	@name    = name
	@aliases = aliases

	@before   = []
	@matchers = []
	@after    = []

	instance_eval &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



45
46
47
48
49
# File 'lib/herpes/module.rb', line 45

def method_missing (id, *args, &block)
	return owner.__send__ id, *args, &block if owner and owner.respond_to?(id)

	super
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



30
31
32
# File 'lib/herpes/module.rb', line 30

def aliases
  @aliases
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/herpes/module.rb', line 30

def name
  @name
end

#ownerObject

Returns the value of attribute owner.



30
31
32
# File 'lib/herpes/module.rb', line 30

def owner
  @owner
end

Class Method Details

.[](name) ⇒ Object



18
19
20
21
22
# File 'lib/herpes/module.rb', line 18

def self.[] (name)
	return name if name.is_a?(Module)

	Module.all.find { |mod| mod =~ name }
end

.allObject



14
15
16
# File 'lib/herpes/module.rb', line 14

def self.all
	@modules ||= []
end

.define(name, *aliases, &block) ⇒ Object



24
25
26
# File 'lib/herpes/module.rb', line 24

def self.define (name, *aliases, &block)
	Module.all << new(name, *aliases, &block)
end

Instance Method Details

#=~(other) ⇒ Object



143
144
145
146
147
# File 'lib/herpes/module.rb', line 143

def =~ (other)
	return true if self == other

	name.to_s.downcase == other.to_s.downcase || aliases.any? { |a| a.to_s.downcase == other.to_s.downcase }
end

#after(*args, &block) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/herpes/module.rb', line 119

def after (*args, &block)
	if owner = self.owner
		owner.from name do
			owner.after *args do |*args|
				owner.instance_exec *args, &block
			end
		end
	else
		@after << Struct.new(:arguments, :block).new(args, block)
	end
end

#before(*args, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/herpes/module.rb', line 95

def before (*args, &block)
	if owner = self.owner
		owner.from name do
			owner.before *args do |*args|
				owner.instance_exec *args, &block
			end
		end
	else
		@before << Struct.new(:arguments, :block).new(args, block)
	end
end

#default(&block) ⇒ Object



73
74
75
# File 'lib/herpes/module.rb', line 73

def default (&block)
	block ? @default : @default = block
end

#dispatch(event = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/herpes/module.rb', line 131

def dispatch (event = nil, &block)
	if block && !event
		event = Event.new(&block)
	end

	raise ArgumentError, 'you did not pass an Event' unless event.is_a?(Event)

	event.generated_by self

	owner.dispatch(event)
end

#inspectObject



149
150
151
# File 'lib/herpes/module.rb', line 149

def inspect
	"#<#{self.class.name}(#{name}#{" [#{aliases.join ', '}]" unless aliases.empty?})>"
end

#on(*args, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/herpes/module.rb', line 107

def on (*args, &block)
	if owner = self.owner
		owner.from name do
			owner.on *args do |*args|
				owner.instance_exec *args, &block
			end
		end
	else
		@matchers << Struct.new(:arguments, :block).new(args, block)
	end
end

#use(owner, &block) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/herpes/module.rb', line 87

def use (owner, &block)
	with {
		self.owner = owner

		instance_eval &block
	}
end

#with(&block) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/herpes/module.rb', line 77

def with (&block)
	block = default unless block

	if !block
		raise ArgumentError, 'no block passed and a default is not present'
	end

	clone.tap { |o| o.instance_eval &block }
end