Class: Butler::Plugin::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/butler/plugin/mapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, meth, language, expression) ⇒ Mapper

Returns a new instance of Mapper.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/butler/plugin/mapper.rb', line 25

def initialize(plugin, meth, language, expression)
	@plugin        = plugin
	@method        = meth
	@language      = language
	@expression    = expression
	@trigger       = expression[/^\w+/].downcase
	@captures      = []
	@authorization = "plugin/#{@plugin.base}".freeze
	@name          = "mapper:#{@language}:#{@expression}".freeze
	@hash          = @name.hash
	
	tiles = expression.strip.scan(/(?:^|\s)[:*]\w+(?:\[[^\]]+\])?|\S+/).map { |word|
		word.strip!
		case word
			when /:(\w+)(.*)/
				@captures << [$1.to_sym, $2]
				'(\S+)'
			when /\*(\w+)(.*)/
				@captures << [$1.to_sym, $2]
				'(\S+(?:\s+\S+)*?)'
			else
				Regexp.escape(word)
		end
	}
	@regexp      = Regexp.new('^'+tiles.join('\s+')+'$')
end

Instance Attribute Details

#authorizationObject (readonly)

Returns the value of attribute authorization.



17
18
19
# File 'lib/butler/plugin/mapper.rb', line 17

def authorization
  @authorization
end

#hashObject (readonly)

Returns the value of attribute hash.



18
19
20
# File 'lib/butler/plugin/mapper.rb', line 18

def hash
  @hash
end

#languageObject (readonly)

Returns the value of attribute language.



19
20
21
# File 'lib/butler/plugin/mapper.rb', line 19

def language
  @language
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/butler/plugin/mapper.rb', line 20

def name
  @name
end

#pluginObject (readonly)

Returns the value of attribute plugin.



21
22
23
# File 'lib/butler/plugin/mapper.rb', line 21

def plugin
  @plugin
end

#triggerObject (readonly)

Returns the value of attribute trigger.



22
23
24
# File 'lib/butler/plugin/mapper.rb', line 22

def trigger
  @trigger
end

Instance Method Details

#<=>(other) ⇒ Object



72
73
74
# File 'lib/butler/plugin/mapper.rb', line 72

def <=>(other)
	-10 <=> other.priority
end

#abort_invocations?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/butler/plugin/mapper.rb', line 76

def abort_invocations?
	true
end

#call(message, params) ⇒ Object



64
65
66
# File 'lib/butler/plugin/mapper.rb', line 64

def call(message, params)
	@plugin.new(message).send(@method, params)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/butler/plugin/mapper.rb', line 80

def eql?(other)
	other.kind_of?(Mapper) && @name.eql?(other.name)
end

#invoked_by?(message) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/butler/plugin/mapper.rb', line 52

def invoked_by?(message)
	if match = @regexp.match(message.post_arguments[0]) then
		params = {}
		@captures.zip(match.captures) { |(name, valid), value|
			params[name] = value
		}
		[OpenStruct.new(params)]
	else
		nil
	end
end

#priorityObject



68
69
70
# File 'lib/butler/plugin/mapper.rb', line 68

def priority
	-10
end