class Gitdocs::GitNotifier

Public Class Methods

new(root, show_notifications) click to toggle source

@param [String] root @param [Boolean] show_notifications

# File lib/gitdocs/git_notifier.rb, line 7
def initialize(root, show_notifications)
  @root               = root
  @show_notifications = show_notifications
end

Public Instance Methods

for_merge(result) click to toggle source

@param [nil, Symbol, Array<String>, Hash<String => Integer>, to_s] result

@return [void]

# File lib/gitdocs/git_notifier.rb, line 15
def for_merge(result)
  return if result.nil?
  return if result == :no_remote
  return if result == :ok
  return if result == {}

  if result.is_a?(Array)
    Notifier.warn(
      'There were some conflicts',
      result.map { |f| "* #{f}" }.join("\n"),
      @show_notifications
    )
  elsif result.is_a?(Hash)
    Notifier.info(
      "Updated with #{change_to_s(result)}",
      "In #{@root}:\n#{author_list(result)}",
      @show_notifications
    )
  else
    Notifier.error(
      'There was a problem synchronizing this gitdoc',
      "A problem occurred in #{@root}:\n#{result}",
      @show_notifications
    )
  end
  nil
end
for_push(result) click to toggle source

@param [nil, Symbol, Hash<String => Integer>, to_s] result of push

@return [void]

# File lib/gitdocs/git_notifier.rb, line 46
def for_push(result)
  return if result.nil?
  return if result == :no_remote
  return if result == :nothing

  if result == :conflict
    Notifier.warn(
      "There was a conflict in #{@root}, retrying",
      '',
      @show_notifications
    )
  elsif result.is_a?(Hash)
    Notifier.info(
      "Pushed #{change_to_s(result)}",
      "#{@root} has been pushed",
      @show_notifications
    )
  else
    Notifier.error(
      "BAD Could not push changes in #{@root}",
      result.to_s,
      @show_notifications
    )
  end
  nil
end
on_error(exception) click to toggle source

@param [Exception] exception

@return [void]

# File lib/gitdocs/git_notifier.rb, line 76
def on_error(exception)
  Notifier.error(
    "Unexpected error when fetching/pushing in #{@root}",
    exception.to_s,
    @show_notifications
  )
  nil
end

Private Instance Methods

author_list(changes) click to toggle source

@param [Hash<String => Integer>] changes @return [String]

# File lib/gitdocs/git_notifier.rb, line 91
def author_list(changes)
  changes
    .map { |author, count| "* #{author} (#{change_to_s(count)})" }
    .join("\n")
end
change_to_s(count_or_hash) click to toggle source

@param [Integer, Hash<String => Integer>] count_or_hash @return [String]

# File lib/gitdocs/git_notifier.rb, line 99
def change_to_s(count_or_hash)
  count =
    if count_or_hash.respond_to?(:values)
      count_or_hash.values.reduce(:+)
    else
      count_or_hash
    end

  "#{count} change#{count == 1 ? '' : 's'}"
end