Class: MentionSystem::MentionProcessor
- Inherits:
-
Object
- Object
- MentionSystem::MentionProcessor
- Defined in:
- lib/mention_system/mention_processor.rb
Overview
MentionProcessor class
This class defines mention processor behavior in mention system
Instance Method Summary collapse
-
#add_after_callback(callback) ⇒ Object
Adds an after callback.
-
#add_before_callback(callback) ⇒ Object
Adds a before callback.
-
#extract_handles_from_mentioner(mentioner) ⇒ Array
Extract handles from mentioner.
-
#extract_mentioner_content(mentioner) ⇒ String
Extracts the mentioner content.
-
#find_mentionees_by_handles(*handles) ⇒ Array
Finds mentionees by handles.
-
#handle_regexp ⇒ Regexp
private
Retrieves the handle regexp.
-
#initialize ⇒ MentionProcessor
constructor
Constructor of the MentionProcessor class.
-
#mention_prefix ⇒ String
private
Returns the mention prefix.
-
#process_after_callbacks(mentioner, mentionee) ⇒ Object
private
Process after callbacks.
-
#process_before_callbacks(mentioner, mentionee) ⇒ Object
private
Process before callbacks.
-
#process_mentions(mentioner) ⇒ Object
Process mentions.
Constructor Details
#initialize ⇒ MentionProcessor
Constructor of the MentionProcessor class
16 17 18 |
# File 'lib/mention_system/mention_processor.rb', line 16 def initialize @callbacks = { after: [], before: [] } end |
Instance Method Details
#add_after_callback(callback) ⇒ Object
Adds an after callback
25 26 27 |
# File 'lib/mention_system/mention_processor.rb', line 25 def add_after_callback(callback) @callbacks[:after].push(callback) end |
#add_before_callback(callback) ⇒ Object
Adds a before callback
34 35 36 |
# File 'lib/mention_system/mention_processor.rb', line 34 def add_before_callback(callback) @callbacks[:before].push(callback) end |
#extract_handles_from_mentioner(mentioner) ⇒ Array
Extract handles from mentioner
44 45 46 47 |
# File 'lib/mention_system/mention_processor.rb', line 44 def extract_handles_from_mentioner(mentioner) content = extract_mentioner_content(mentioner) handles = content.scan(handle_regexp).map { |handle| handle.gsub("#{mention_prefix}","") } end |
#extract_mentioner_content(mentioner) ⇒ String
Extracts the mentioner content
55 56 57 |
# File 'lib/mention_system/mention_processor.rb', line 55 def extract_mentioner_content(mentioner) raise "Must be implemented in subclass" end |
#find_mentionees_by_handles(*handles) ⇒ Array
Finds mentionees by handles
65 66 67 |
# File 'lib/mention_system/mention_processor.rb', line 65 def find_mentionees_by_handles(*handles) raise "Must be implemented in subclass" end |
#handle_regexp ⇒ Regexp (private)
Retrieves the handle regexp
93 94 95 |
# File 'lib/mention_system/mention_processor.rb', line 93 def handle_regexp /(?<!\w)#{mention_prefix}\w+/ end |
#mention_prefix ⇒ String (private)
Returns the mention prefix
102 103 104 |
# File 'lib/mention_system/mention_processor.rb', line 102 def mention_prefix "@" end |
#process_after_callbacks(mentioner, mentionee) ⇒ Object (private)
Process after callbacks
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/mention_system/mention_processor.rb', line 112 def process_after_callbacks(mentioner, mentionee) result = true @callbacks[:after].each do |callback| unless callback.call(mentioner, mentionee) result = false break end end result end |
#process_before_callbacks(mentioner, mentionee) ⇒ Object (private)
Process before callbacks
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mention_system/mention_processor.rb', line 129 def process_before_callbacks(mentioner, mentionee) result = true @callbacks[:before].each do |callback| unless callback.call(mentioner, mentionee) result = false break end end result end |
#process_mentions(mentioner) ⇒ Object
Process mentions
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/mention_system/mention_processor.rb', line 74 def process_mentions(mentioner) handles = extract_handles_from_mentioner(mentioner) mentionees = find_mentionees_by_handles(handles) mentionees.each do |mentionee| if process_before_callbacks(mentioner, mentionee) if mentioner.mention(mentionee) process_after_callbacks(mentioner, mentionee) end end end end |