Module: Facy::Command
- Included in:
- Facy
- Defined in:
- lib/facy/command.rb
Instance Method Summary collapse
- #command(pattern, options = {}, &block) ⇒ Object
- #commands ⇒ Object
- #execute(text) ⇒ Object
- #match_single_command(text) ⇒ Object
- #match_target_command(text) ⇒ Object
Instance Method Details
#command(pattern, options = {}, &block) ⇒ Object
7 8 9 |
# File 'lib/facy/command.rb', line 7 def command(pattern, ={}, &block) commands << {pattern: pattern, block: block} end |
#commands ⇒ Object
3 4 5 |
# File 'lib/facy/command.rb', line 3 def commands @commands ||= [] end |
#execute(text) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/facy/command.rb', line 11 def execute(text) text.strip! rule, target = match_single_command(text) || match_target_command(text) commands.each do |c| if rule.to_s == c[:pattern].to_s.split(":").first c[:block].call(target) return end end rescue Exception => e error e log(:error, e.) log(:error, e.backtrace) end |
#match_single_command(text) ⇒ Object
32 33 34 35 |
# File 'lib/facy/command.rb', line 32 def match_single_command(text) text =~ /^:(\S*)$/ return $1 end |
#match_target_command(text) ⇒ Object
26 27 28 29 30 |
# File 'lib/facy/command.rb', line 26 def match_target_command(text) text =~ /^:(\S*) (.+)$/ return [$1, $2] if $1 && $2 return nil end |