class MentionSystem::MentionProcessor

MentionProcessor class

This class defines mention processor behavior in mention system

Public Class Methods

new() click to toggle source

Constructor of the MentionProcessor class

# File lib/mention_system/mention_processor.rb, line 16
def initialize
  @callbacks = { after: [], before: [] }
end

Public Instance Methods

add_after_callback(callback) click to toggle source

Adds an after callback

@param [Proc] callback - the callback to add

# File lib/mention_system/mention_processor.rb, line 25
def add_after_callback(callback)
  @callbacks[:after].push(callback)
end
add_before_callback(callback) click to toggle source

Adds a before callback

@param [Proc] callback - the callback to add

# File lib/mention_system/mention_processor.rb, line 34
def add_before_callback(callback)
  @callbacks[:before].push(callback)
end
extract_handles_from_mentioner(mentioner) click to toggle source

Extract handles from mentioner

@param [Mentioner] mentioner - the {Mentioner} to extract handles from @return [Array]

# 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) click to toggle source

Extracts the mentioner content

@param [Mentioner] mentioner - the {Mentioner} to extract content from @return [String]

# 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) click to toggle source

Finds mentionees by handles

@param [Array] handles - the array of {Mentionee} handles to find @return [Array]

# File lib/mention_system/mention_processor.rb, line 65
def find_mentionees_by_handles(*handles)
  raise "Must be implemented in subclass"
end
process_mentions(mentioner) click to toggle source

Process mentions

@param [Mentioner] mentioner - the {Mentioner} to process mentions from

# 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

Private Instance Methods

handle_regexp() click to toggle source

Retrieves the handle regexp

@return [Regexp]

# File lib/mention_system/mention_processor.rb, line 93
def handle_regexp
  /(?<!\w)#{mention_prefix}\w+/
end
mention_prefix() click to toggle source

Returns the mention prefix

@return [String]

# File lib/mention_system/mention_processor.rb, line 102
def mention_prefix
  "@"
end
process_after_callbacks(mentioner, mentionee) click to toggle source

Process after callbacks

@param [Mentioner] mentioner - the mentioner of the callback @param [Mentionee] mentionee - the mentionee of the callback

# 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) click to toggle source

Process before callbacks

@param [Mentioner] mentioner - the mentioner of the callback @param [Mentionee] mentionee - the mentionee of the callback

# 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