Skip to content

Commit

Permalink
Add rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
Attila Horváth committed Sep 9, 2016
1 parent c29661f commit 16af145
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
AllCops:
TargetRubyVersion: 2.0
Exclude:
- 'i18n-globals.gemspec'

Documentation:
Enabled: false

Metrics/LineLength:
Max: 100
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ rvm:
matrix:
allow_failures:
- rvm: ruby-head
script:
- bundle exec rake test
- bundle exec rubocop
7 changes: 4 additions & 3 deletions i18n-globals.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 2.0'

spec.add_development_dependency 'bundler', '~> 1.6'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'minitest'
spec.add_development_dependency 'bundler', '~> 1.12.5'
spec.add_development_dependency 'rake', '~> 11.2.2'
spec.add_development_dependency 'minitest', '~> 5.9.0'
spec.add_development_dependency 'rubocop', '~> 0.42.0'

spec.add_runtime_dependency 'i18n'
end
3 changes: 2 additions & 1 deletion lib/i18n-globals.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require 'i18n/globals'
# rubocop:disable Style/FileName
require 'i18n/globals'
14 changes: 9 additions & 5 deletions lib/i18n/globals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Config
class CachedGlobals < Hash
def []=(key, val)
clear_cache
annotate_hash(val) if val.is_a?(Hash) # see annotate hash below why whis must be done
annotate_hash(val) if val.is_a?(Hash)
super(key, val)
end

Expand All @@ -27,7 +27,6 @@ def clear

def merge!(val)
clear_cache
# see annotate hash below why whis must be done
val.select { |_, v| v.is_a?(Hash) }.each { |_, v| annotate_hash(v) }
super(val)
end
Expand All @@ -53,6 +52,8 @@ def clear_cache
# To overcome this we annotate every hash that might passed in with this
# method. So when the sub hash is changed like above, the whole cache
# is cleared like it should.

# rubocop:disable Metrics/MethodLength
def annotate_hash(hash)
return if hash.instance_variable_defined?(:@cached_global)
hash.instance_variable_set(:@cached_global, self)
Expand All @@ -72,19 +73,21 @@ def hash.clear
@cached_global.send(:clear_cache)
end
end
# rubocop:enable Metrics/MethodLength
end

def globals
@@globals ||= CachedGlobals.new
@@globals ||= CachedGlobals.new # rubocop:disable Style/ClassVars
end

def globals=(new_globals)
globals.clear.merge!(new_globals) # maybe there is something better than `clear` and `merge!`
globals.clear.merge!(new_globals)
end

prepend(
Module.new do
def missing_interpolation_argument_handler
# rubocop:disable Style/ClassVars
@@missing_interpolation_argument_handler_with_globals ||=
lambda do |missing_key, provided_hash, string|
# Since the key was not found in a interpolation variable, check
Expand All @@ -96,6 +99,7 @@ def missing_interpolation_argument_handler
super.call(missing_key, provided_hash, string)
end
end
# rubocop:enable Style/ClassVars
end
end
)
Expand All @@ -111,6 +115,6 @@ def translate(*args)
super(*args)
end

alias :t :translate
alias t translate
end
end
26 changes: 13 additions & 13 deletions test/test_i18n_globals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'minitest/pride'
require 'i18n-globals'

# rubocop:disable Metrics/ClassLength
class TestI18nGlobals < Minitest::Test
def setup
I18n.backend.load_translations 'test/fixtures/translations.yml'
Expand Down Expand Up @@ -46,14 +47,16 @@ def test_that_the_other_global_variable_can_be_overwritten
I18n.config.globals[:name] = 'Barney'
I18n.config.globals[:company] = 'Black Mesa'

assert_equal 'Hello Barney, welcome to Aperture Science!', I18n.translate('welcome', company: 'Aperture Science')
assert_equal 'Hello Barney, welcome to Aperture Science!',
I18n.translate('welcome', company: 'Aperture Science')
end

def test_that_all_of_the_global_variables_can_be_overwritten
I18n.config.globals[:name] = 'Barney'
I18n.config.globals[:company] = 'Black Mesa'

assert_equal 'Hello Gordon, welcome to Aperture Science!', I18n.translate('welcome', name: 'Gordon', company: 'Aperture Science')
assert_equal 'Hello Gordon, welcome to Aperture Science!',
I18n.translate('welcome', name: 'Gordon', company: 'Aperture Science')
end

def test_that_the_t_alias_work
Expand Down Expand Up @@ -125,7 +128,7 @@ def test_that_cache_is_cleared_after_merging_a_new_locale_hash

assert_equal 'Hi there, Debby!', I18n.translate('greeting')

I18n.config.globals[:en].merge!(name: 'Elisa')
I18n.config.globals[:en].merge!(name: 'Elisa') # rubocop:disable Performance/RedundantMerge

assert_equal 'Hi there, Elisa!', I18n.translate('greeting')
end
Expand All @@ -144,9 +147,7 @@ def test_that_cache_is_cleared_after_clearing_locale_hash
end

def test_that_cache_is_cleared_after_setting_a_new_global
I18n.config.globals = {
name: 'Greg'
}
I18n.config.globals[:name] = 'Greg'

assert_equal 'Hi there, Greg!', I18n.translate('greeting')

Expand All @@ -156,13 +157,11 @@ def test_that_cache_is_cleared_after_setting_a_new_global
end

def test_that_cache_is_cleared_after_merging_a_new_global
I18n.config.globals = {
name: 'Greg'
}
I18n.config.globals[:name] = 'Greg'

assert_equal 'Hi there, Greg!', I18n.translate('greeting')

I18n.config.globals.merge!(name: 'Dobby')
I18n.config.globals.merge!(name: 'Dobby') # rubocop:disable Performance/RedundantMerge

assert_equal 'Hi there, Dobby!', I18n.translate('greeting')
end
Expand All @@ -182,15 +181,14 @@ def test_that_it_allows_to_set_a_custom_missing_interpolation_argument_handler
def test_that_it_translates_globals_with_custom_missing_interpolation_argument_handler
I18n.config.missing_interpolation_argument_handler = -> { raise 'works!' }

I18n.config.globals = {
name: 'Greg'
}
I18n.config.globals[:name] = 'Greg'

assert_equal 'Hi there, Greg!', I18n.translate('greeting')

I18n.config.missing_interpolation_argument_handler = nil
end

# rubocop:disable Metrics/MethodLength
def test_that_it_does_not_polute_the_object_space_with_hashes
I18n.config.globals = {
name: 'Greg'
Expand All @@ -211,4 +209,6 @@ def test_that_it_does_not_polute_the_object_space_with_hashes

assert_equal 0, created_due_to_globals
end
# rubocop:enable Metrics/MethodLength
end
# rubocop:enable Metrics/ClassLength

0 comments on commit 16af145

Please sign in to comment.