Skip to content

Commit

Permalink
Improve coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
rymai committed May 16, 2013
1 parent c901d76 commit 232b153
Show file tree
Hide file tree
Showing 35 changed files with 329 additions and 286 deletions.
148 changes: 75 additions & 73 deletions lib/guard/interactor.rb
Expand Up @@ -76,6 +76,30 @@ def enabled=(status)
@enabled = status
end


# Converts and validates a plain text scope
# to a valid plugin or group scope.
#
# @param [Array<String>] entries the text scope
# @return [Hash, Array<String>] the plugin or group scope, the unknown entries
#
def convert_scope(entries)
scopes = { :plugins => [], :groups => [] }
unknown = []

entries.each do |entry|
if plugin = ::Guard.guards(entry)
scopes[:plugins] << plugin
elsif group = ::Guard.groups(entry)
scopes[:groups] << group
else
unknown << entry
end
end

[scopes, unknown]
end

end

# Initialize the interactor. This configures
Expand All @@ -89,15 +113,47 @@ def initialize
Pry.config.should_load_local_rc = false
Pry.config.history.file = File.expand_path(self.class.options[:history_file] || HISTORY_FILE)

add_hooks
_add_hooks

_replace_reset_command
_create_run_all_command
_create_command_aliases
_create_guard_commands
_create_group_commands

_configure_prompt
end

# Start the line reader in its own thread and
# stop Guard on Ctrl-D.
#
def start
return if ENV['GUARD_ENV'] == 'test'

_store_terminal_settings if _stty_exists?

if !@thread || !@thread.alive?
::Guard::UI.debug 'Start interactor'

@thread = Thread.new do
Pry.start
::Guard.stop
end
end
end

replace_reset_command
create_run_all_command
create_command_aliases
create_guard_commands
create_group_commands
# Kill interactor thread if not current
#
def stop
return if !@thread || ENV['GUARD_ENV'] == 'test'

unless Thread.current == @thread
::Guard::UI.reset_line
::Guard::UI.debug 'Stop interactor'
@thread.kill
end

configure_prompt
_restore_terminal_settings if _stty_exists?
end

# Add Pry hooks:
Expand All @@ -106,7 +162,7 @@ def initialize
# * Load project's `.guardrc` within each new Pry session.
# * Restore prompt after each evaluation.
#
def add_hooks
def _add_hooks
Pry.config.hooks.add_hook :when_started, :load_guard_rc do
(self.class.options[:guard_rc] || GUARD_RC).tap do |p|
load p if File.exist?(File.expand_path(p))
Expand All @@ -118,7 +174,7 @@ def add_hooks
load project_guard_rc if File.exist?(project_guard_rc)
end

if stty_exists?
if _stty_exists?
Pry.config.hooks.add_hook :after_eval, :restore_visibility do
system('stty echo 2>/dev/null')
end
Expand All @@ -127,8 +183,8 @@ def add_hooks

# Replaces reset defined inside of Pry with a reset that
# instead restarts guard.

def replace_reset_command
#
def _replace_reset_command
Pry.commands.command "reset", "Reset the Guard to a clean state." do
output.puts "Guard reset."
exec "guard"
Expand All @@ -139,7 +195,7 @@ def replace_reset_command
# when the command is empty (just pressing enter on the
# beginning of a line).
#
def create_run_all_command
def _create_run_all_command
Pry.commands.block_command /^$/, 'Hit enter to run all' do
Pry.run_command 'all'
end
Expand All @@ -149,7 +205,7 @@ def create_run_all_command
# `help`, `reload`, `change`, `scope`, `notification`, `pause`, `exit` and `quit`,
# which will be the first letter of the command.
#
def create_command_aliases
def _create_command_aliases
SHORTCUTS.each do |command, shortcut|
Pry.commands.alias_command shortcut, command.to_s
end
Expand All @@ -160,7 +216,7 @@ def create_command_aliases
# when guard-rspec is available, then a command
# `rspec` is created that runs `all rspec`.
#
def create_guard_commands
def _create_guard_commands
::Guard.guards.each do |guard_plugin|
Pry.commands.create_command guard_plugin.name, "Run all #{ guard_plugin.title }" do
group 'Guard'
Expand All @@ -177,7 +233,7 @@ def process
# when you have a group `frontend`, then a command
# `frontend` is created that runs `all frontend`.
#
def create_group_commands
def _create_group_commands
::Guard.groups.each do |group|
next if group.name == :default

Expand All @@ -194,7 +250,7 @@ def process
# Configure the pry prompt to see `guard` instead of
# `pry`.
#
def configure_prompt
def _configure_prompt
Pry.config.prompt = [
proc do |target_self, nest_level, pry|
history = pry.input_array.size
Expand Down Expand Up @@ -229,81 +285,27 @@ def configure_prompt
]
end

# Start the line reader in its own thread and
# stop Guard on Ctrl-D.
#
def start
return if ENV['GUARD_ENV'] == 'test'

store_terminal_settings if stty_exists?

if !@thread || !@thread.alive?
::Guard::UI.debug 'Start interactor'

@thread = Thread.new do
Pry.start
::Guard.stop
end
end
end

# Kill interactor thread if not current
#
def stop
return if !@thread || ENV['GUARD_ENV'] == 'test'

unless Thread.current == @thread
::Guard::UI.reset_line
::Guard::UI.debug 'Stop interactor'
@thread.kill
end

restore_terminal_settings if stty_exists?
end

# Detects whether or not the stty command exists
# on the user machine.
#
# @return [Boolean] the status of stty
#
def stty_exists?
def _stty_exists?
@stty_exists ||= system('hash', 'stty')
end

# Stores the terminal settings so we can resore them
# when stopping.
#
def store_terminal_settings
def _store_terminal_settings
@stty_save = `stty -g 2>#{ DEV_NULL }`.chomp
end

# Restore terminal settings
#
def restore_terminal_settings
def _restore_terminal_settings
system("stty #{ @stty_save } 2>#{ DEV_NULL }") if @stty_save
end

# Converts and validates a plain text scope
# to a valid plugin or group scope.
#
# @param [Array<String>] entries the text scope
# @return [Hash, Array<String>] the plugin or group scope, the unknown entries
#
def self.convert_scope(entries)
scopes = { :plugins => [], :groups => [] }
unknown = []

entries.each do |entry|
if plugin = ::Guard.guards(entry)
scopes[:plugins] << plugin
elsif group = ::Guard.groups(entry)
scopes[:groups] << group
else
unknown << entry
end
end

[scopes, unknown]
end
end
end
40 changes: 20 additions & 20 deletions lib/guard/notifier.rb
Expand Up @@ -98,14 +98,14 @@ def clear_notifications
# library.
#
def turn_on
auto_detect_notification if notifications.empty? && (!::Guard.options || ::Guard.options[:notify])
_auto_detect_notification if notifications.empty? && (!::Guard.options || ::Guard.options[:notify])

if notifications.empty?
ENV['GUARD_NOTIFY'] = 'false'
else
notifications.each do |notification|
::Guard::UI.info "Guard uses #{ get_notifier_module(notification[:name]).to_s.split('::').last } to send notifications."
notifier = get_notifier_module(notification[:name])
notifier = _get_notifier_module(notification[:name])
::Guard::UI.info "Guard uses #{ notifier.to_s.split('::').last } to send notifications."
notifier.turn_on(notification[:options]) if notifier.respond_to?(:turn_on)
end

Expand All @@ -117,7 +117,7 @@ def turn_on
#
def turn_off
notifications.each do |notification|
notifier = get_notifier_module(notification[:name])
notifier = _get_notifier_module(notification[:name])
notifier.turn_off(notification[:options]) if notifier.respond_to?(:turn_off)
end

Expand Down Expand Up @@ -150,10 +150,10 @@ def enabled?
# @param [Hash] options the notifier options
# @return [Boolean] if the notification could be added
#
def add_notification(name, options = { }, silent = false)
def add_notification(name, options = {}, silent = false)
return turn_off if name == :off

notifier = get_notifier_module(name)
notifier = _get_notifier_module(name)

if notifier && notifier.available?(silent, options)
self.notifications = notifications << { :name => name, :options => options }
Expand All @@ -169,15 +169,15 @@ def add_notification(name, options = { }, silent = false)
# @option options [Symbol, String] image the image symbol or path to an image
# @option options [String] title the notification title
#
def notify(message, options = { })
def notify(message, options = {})
if enabled?
type = notification_type(options[:image] || :success)
image = image_path(options.delete(:image) || :success)
type = _notification_type(options[:image] || :success)
image = _image_path(options.delete(:image) || :success)
title = options.delete(:title) || 'Guard'

notifications.each do |notification|
begin
get_notifier_module(notification[:name]).notify(type, title, message, image, options.merge(notification[:options]))
_get_notifier_module(notification[:name]).notify(type, title, message, image, options.merge(notification[:options]))
rescue Exception => e
::Guard::UI.error "Error sending notification with #{ notification[:name] }: #{ e.message }"
end
Expand All @@ -192,7 +192,7 @@ def notify(message, options = { })
# @param [Symbol] name the notifier name
# @return [Module] the notifier module
#
def get_notifier_module(name)
def _get_notifier_module(name)
notifier = NOTIFIERS.flatten(1).detect { |n| n.first == name }
notifier ? notifier.last : notifier
end
Expand All @@ -201,12 +201,12 @@ def get_notifier_module(name)
# the list of supported notification gems and picks the first that
# is available in each notification group.
#
def auto_detect_notification
def _auto_detect_notification
available = nil
self.notifications = []

NOTIFIERS.each do |group|
added = group.map { |n| n.first }.find { |notifier| add_notification(notifier, { }, true) }
added = group.map { |n| n.first }.find { |notifier| add_notification(notifier, {}, true) }
available = available || added
end

Expand All @@ -225,14 +225,14 @@ def auto_detect_notification
# @param [Symbol, String] image the image symbol or path to an image
# @return [String] the image path
#
def image_path(image)
def _image_path(image)
case image
when :failed
images_path.join('failed.png').to_s
_images_path.join('failed.png').to_s
when :pending
images_path.join('pending.png').to_s
_images_path.join('pending.png').to_s
when :success
images_path.join('success.png').to_s
_images_path.join('success.png').to_s
else
image
end
Expand All @@ -242,8 +242,8 @@ def image_path(image)
#
# @return [Pathname] the path to the images directory
#
def images_path
@images_path ||= Pathname.new(File.dirname(__FILE__)).join('../../images')
def _images_path
@_images_path ||= Pathname.new(File.dirname(__FILE__)).join('../../images')
end

# Get the notification type depending on the
Expand All @@ -252,7 +252,7 @@ def images_path
# @param [Symbol, String] image the image symbol or path to an image
# @return [String] the notification type
#
def notification_type(image)
def _notification_type(image)
case image
when :failed
'failed'
Expand Down
2 changes: 1 addition & 1 deletion lib/guard/notifiers/emacs.rb
Expand Up @@ -49,7 +49,7 @@ def available?(silent = false, options = {})
# @option options [String] client the client to use for notification (default is 'emacsclient')
# @option options [String, Integer] priority specify an int or named key (default is 0)
#
def notify(type, title, message, image, options = { })
def notify(type, title, message, image, options = {})
options = DEFAULTS.merge options
color = emacs_color type, options
fontcolor = emacs_color :fontcolor, options
Expand Down
2 changes: 1 addition & 1 deletion lib/guard/notifiers/file_notifier.rb
Expand Up @@ -35,7 +35,7 @@ def available?(silent = false, options = {})
# @option options [String] format printf style format for file contents
# @option options [String] path the path of where to write the file
#
def notify(type, title, message, image, options = { })
def notify(type, title, message, image, options = {})
if options[:path]
format = options.fetch(:format, DEFAULTS[:format])

Expand Down
2 changes: 1 addition & 1 deletion lib/guard/notifiers/gntp.rb
Expand Up @@ -84,7 +84,7 @@ def available?(silent = false, options = {})
# @option options [Integer] port the port to send a remote notification
# @option options [Boolean] sticky make the notification sticky
#
def notify(type, title, message, image, options = { })
def notify(type, title, message, image, options = {})
require 'ruby_gntp'

options = DEFAULTS.merge(options)
Expand Down

0 comments on commit 232b153

Please sign in to comment.