Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and fix RubCop offenses #21

Merged
merged 8 commits into from
May 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inherit_from: .rubocop_todo.yml
101 changes: 101 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-05-18 02:46:59 +0200 using RuboCop version 0.40.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 6
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 485

# Offense count: 3
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 18

# Offense count: 5
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 334

# Offense count: 10
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/notiffany.rb'
- 'lib/notiffany/notifier.rb'
- 'lib/notiffany/notifier/base.rb'
- 'lib/notiffany/notifier/detected.rb'
- 'lib/notiffany/notifier/tmux.rb'

# Offense count: 52
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: leading, trailing
Style/DotPosition:
Enabled: false

# Offense count: 11
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: symmetrical, new_line, same_line
Style/MultilineMethodCallBraceLayout:
Exclude:
- 'spec/lib/notiffany/notifier/growl_spec.rb'
- 'spec/lib/notiffany/notifier/libnotify_spec.rb'
- 'spec/lib/notiffany/notifier/rb_notifu_spec.rb'
- 'spec/lib/notiffany/notifier/terminal_notifier_spec.rb'
- 'spec/lib/notiffany/notifier/tmux_spec.rb'

# Offense count: 25
# Cop supports --auto-correct.
Style/MutableConstant:
Exclude:
- 'lib/notiffany/notifier.rb'
- 'lib/notiffany/notifier/base.rb'
- 'lib/notiffany/notifier/detected.rb'
- 'lib/notiffany/notifier/file.rb'
- 'lib/notiffany/notifier/gntp.rb'
- 'lib/notiffany/notifier/growl.rb'
- 'lib/notiffany/notifier/libnotify.rb'
- 'lib/notiffany/notifier/notifysend.rb'
- 'lib/notiffany/notifier/rb_notifu.rb'
- 'lib/notiffany/notifier/terminal_notifier.rb'
- 'lib/notiffany/notifier/terminal_title.rb'
- 'lib/notiffany/notifier/tmux.rb'
- 'lib/notiffany/version.rb'
- 'spec/lib/notiffany/notifier/base_spec.rb'

# Offense count: 23
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: only_raise, only_fail, semantic
Style/SignalException:
Exclude:
- 'lib/notiffany/notifier.rb'
- 'lib/notiffany/notifier/base.rb'
- 'lib/notiffany/notifier/detected.rb'
- 'lib/notiffany/notifier/file.rb'
- 'lib/notiffany/notifier/growl.rb'
- 'lib/notiffany/notifier/notifysend.rb'
- 'lib/notiffany/notifier/terminal_notifier.rb'
- 'lib/notiffany/notifier/tmux.rb'
- 'spec/lib/notiffany/notifier/detected_spec.rb'

# Offense count: 13
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: use_perl_names, use_english_names
Style/SpecialGlobalVars:
Enabled: false

# Offense count: 967
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Style/StringLiterals:
Enabled: false
13 changes: 4 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
language: ruby
bundler_args: --without development
rvm:
- 1.9.3
- 2.0.0
- 2.1.5
- ruby-head
- jruby
- rbx-2
matrix:
allow_failures:
- rvm: rbx-2
- rvm: jruby
- 2.2.5
- 2.3.1
- jruby-9.0.5.0
sudo: false
cache: bundler
11 changes: 7 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in notiffany.gemspec
gemspec development_group: :gem_build_tools

gem "rake", "~> 10.0"
gem 'nenv', "~> 0.1"
gem "rake", "~> 11.1"
gem 'nenv', "~> 0.3"

group :test do
gem "rspec", "~> 3.1"
gem "rspec", "~> 3.4"
end

group :development do
gem 'guard-rspec', "~> 4.5", require: false
gem 'guard-rspec', "~> 4.6", require: false
gem 'listen', "~> 3.1"
gem 'guard-rubocop', "~> 1.2", require: false
gem 'rubocop', '~> 0.40', require: false
end
33 changes: 20 additions & 13 deletions Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
#
watch ("Guardfile") do
watch("Guardfile") do
UI.info "Exiting because Guard must be restarted for changes to take effect"
exit 0
end
Expand All @@ -38,19 +38,26 @@ end
# * zeus: 'zeus rspec' (requires the server to be started separately)
# * 'just' rspec: 'rspec'

guard :rspec, cmd: "bundle exec rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)
group :specs, halt_on_fail: true do
guard :rspec, cmd: "bundle exec rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)

# Feel free to open issues for suggestions and improvements
# Feel free to open issues for suggestions and improvements

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)
# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)

# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)
# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)
end

guard :rubocop do
watch(/.+\.rb$/)
watch(%r{(?:.+/)?\.rubocop(?:_todo)\.yml$}) { |m| File.dirname(m[0]) }
end
end
3 changes: 1 addition & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ require 'nenv'
default_tasks = []

require 'rspec/core/rake_task'
default_tasks << RSpec::Core::RakeTask.new(:spec) do |t|
default_tasks << RSpec::Core::RakeTask.new(:spec) do |t|
t.verbose = Nenv.ci?
end

task default: default_tasks.map(&:name)

79 changes: 42 additions & 37 deletions lib/notiffany/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
require "rbconfig"
require "pathname"
require "nenv"
require "logger"

require "notiffany/notifier/detected"
require "notiffany/notifier/config"

module Notiffany
# The notifier handles sending messages to different notifiers. Currently the
Expand Down Expand Up @@ -43,9 +43,7 @@ def self.connect(options = {})
end

class Notifier
DEFAULTS = { notify: true }

NOTIFICATIONS_DISABLED = "Notifications disabled by GUARD_NOTIFY" +
NOTIFICATIONS_DISABLED = "Notifications disabled by GUARD_NOTIFY" \
" environment variable"

USING_NOTIFIER = "Notiffany is using %s to send notifications."
Expand Down Expand Up @@ -79,35 +77,16 @@ class Notifier
class NotServer < RuntimeError
end

def initialize(opts)
@env_namespace = opts.fetch(:namespace, "notiffany")
@logger = opts.fetch(:logger) do
Logger.new($stderr).tap { |l| l.level = Logger::WARN }
end
attr_reader :config


@detected = Detected.new(SUPPORTED, @env_namespace, @logger)
def initialize(opts)
@config = Config.new(opts)
@detected = Detected.new(SUPPORTED, config.env_namespace, config.logger)
return if _client?

_env.notify_pid = $$

fail "Already connected" if active?

options = DEFAULTS.merge(opts)
return unless enabled? && options[:notify]

notifiers = opts.fetch(:notifiers, {})
if notifiers.any?
notifiers.each do |name, notifier_options|
@detected.add(name, notifier_options)
end
else
@detected.detect
end

turn_on
_activate
rescue Detected::NoneAvailableError => e
@logger.info e.to_s
config.logger.info e.to_s
end

def disconnect
Expand All @@ -133,13 +112,7 @@ def turn_on(options = {})

fail "Already active!" if active?

silent = options[:silent]

@detected.available.each do |obj|
@logger.debug(format(USING_NOTIFIER, obj.title)) unless silent
obj.turn_on if obj.respond_to?(:turn_on)
end

_turn_on_notifiers(options)
_env.notify_active = true
end

Expand Down Expand Up @@ -191,7 +164,7 @@ def available
private

def _env
@environment ||= Env.new(@env_namespace)
@environment ||= Env.new(config.env_namespace)
end

def _check_server!
Expand All @@ -201,5 +174,37 @@ def _check_server!
def _client?
(pid = _env.notify_pid) && (pid != $$)
end

def _detect_or_add_notifiers
notifiers = config.notifiers
return @detected.detect if notifiers.empty?

notifiers.each do |name, notifier_options|
@detected.add(name, notifier_options)
end
end

def _notification_wanted?
enabled? && config.notify?
end

def _activate
_env.notify_pid = $$

fail "Already connected" if active?

return unless _notification_wanted?

_detect_or_add_notifiers
turn_on
end

def _turn_on_notifiers(options)
silent = options[:silent]
@detected.available.each do |obj|
config.logger.debug(format(USING_NOTIFIER, obj.title)) unless silent
obj.turn_on if obj.respond_to?(:turn_on)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/notiffany/notifier/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize(gem_name)

class UnsupportedPlatform < UnavailableError
def initialize
super "Unsupported platform #{RbConfig::CONFIG["host_os"].inspect}"
super "Unsupported platform #{RbConfig::CONFIG['host_os'].inspect}"
end
end

Expand Down
34 changes: 34 additions & 0 deletions lib/notiffany/notifier/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "logger"

module Notiffany
class Notifier
# Configuration class for Notifier
class Config
DEFAULTS = { notify: true }.freeze

attr_reader :env_namespace
attr_reader :logger
attr_reader :notifiers

def initialize(opts)
options = DEFAULTS.merge(opts)
@env_namespace = opts.fetch(:namespace, "notiffany")
@logger = _setup_logger(options)
@notify = options[:notify]
@notifiers = opts.fetch(:notifiers, {})
end

def notify?
@notify
end

private

def _setup_logger(opts)
opts.fetch(:logger) do
Logger.new($stderr).tap { |l| l.level = Logger::WARN }
end
end
end
end
end