Skip to content

Commit

Permalink
Merge pull request #67 from grosser/grosser/cop
Browse files Browse the repository at this point in the history
rubocop
  • Loading branch information
grosser authored Sep 2, 2024
2 parents de5f22e + f556b8c commit a1f8a02
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 83 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby: [ '2.5', '2.6', '2.7', '3.0', '3.1' ]
ruby: [ '3.0', '3.1', '3.2', '3.3' ]
task: [ 'spec' ]
include:
- ruby: '3.0' # keep in sync with rubocop.yml and gemspec
task: 'rubocop'
name: ${{ matrix.ruby }} rake
steps:
- uses: actions/checkout@v2
Expand Down
110 changes: 110 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
require:
- rubocop-rake
- rubocop-rspec

AllCops:
NewCops: enable
TargetRubyVersion: 3.0 # keep in sync with gemspec and actions.yml

Style/StringLiterals:
Enabled: false

Bundler/OrderedGems:
Enabled: false

Metrics:
Enabled: false

Style/Documentation:
Enabled: false

Layout/EmptyLineAfterMagicComment:
Enabled: false

Layout/EndAlignment:
EnforcedStyleAlignWith: variable

Layout/MultilineOperationIndentation:
Enabled: false

Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented

Style/NumericPredicate:
EnforcedStyle: comparison

Layout/EmptyLineAfterGuardClause:
Enabled: false

Layout/FirstHashElementLineBreak:
Enabled: true # Opt-in

# Opt-in
Layout/FirstMethodArgumentLineBreak:
Enabled: true # Opt-in

Layout/FirstMethodParameterLineBreak:
Enabled: true # Opt-in

# https:/rubocop-hq/rubocop/issues/5891
Style/SpecialGlobalVars:
Enabled: false

Style/WordArray:
EnforcedStyle: brackets

Style/SymbolArray:
EnforcedStyle: brackets

Style/GuardClause:
Enabled: false

Style/EmptyElse:
Enabled: false

RSpec/DescribedClass:
EnforcedStyle: explicit

Style/DoubleNegation:
Enabled: false

RSpec/VerifiedDoubles:
Enabled: false

RSpec/ExampleLength:
Enabled: false

Style/CombinableLoops:
Enabled: false

Lint/Void:
Enabled: false

Security/MarshalLoad:
Enabled: false

Lint/EmptyBlock:
Exclude: [spec/**/*.rb]

Naming/MethodParameterName:
Exclude: [spec/**/*.rb]

RSpec/BeforeAfterAll:
Enabled: false

# could change to `#method` and `.method` instead
RSpec/DescribeSymbol:
Enabled: false

RSpec/NestedGroups:
Enabled: false

RSpec/EmptyExampleGroup:
Enabled: false

Lint/SuppressedException:
Enabled: false

# somehow crashes
RSpec/VariableName:
Enabled: false
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true
source 'https://rubygems.org'
gemspec

gem 'bump'
gem 'rake'
gem 'rspec', '~>3.0'
gem 'rubocop'
gem 'rubocop-rake'
gem 'rubocop-rspec'
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ PATH
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
bump (0.10.0)
diff-lcs (1.4.4)
json (2.7.2)
language_server-protocol (3.17.0.3)
parallel (1.26.3)
parser (3.3.4.2)
ast (~> 2.4.1)
racc
racc (1.8.1)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.9.2)
rexml (3.3.6)
strscan
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
Expand All @@ -23,7 +35,27 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.3)
rubocop (1.65.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.4, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.1)
parser (>= 3.3.1.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.9.0)
rubocop (~> 1.19)
ruby-progressbar (1.13.0)
simple_po_parser (1.1.6)
strscan (3.1.0)
unicode-display_width (2.5.0)

PLATFORMS
ruby
Expand All @@ -36,6 +68,9 @@ DEPENDENCIES
i18n_data!
rake
rspec (~> 3.0)
rubocop
rubocop-rake
rubocop-rspec

BUNDLED WITH
2.3.12
37 changes: 21 additions & 16 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'bundler/setup'
require 'bundler/gem_tasks'
require 'bump/tasks'
Expand All @@ -6,57 +7,61 @@ require 'yaml'
$LOAD_PATH << "lib"
require 'i18n_data'

task default: [:spec]
task default: [:spec, :rubocop]

desc "Run tests"
task :spec do
sh "rspec --warnings spec/"
end

desc "Rubocop"
task :rubocop do
sh "rubocop --parallel"
end

desc "write all languages to output"
task :all_languages do
I18nData.languages.keys.each do |lc|
I18nData.languages.each_key do |lc|
`rake languages LANGUAGE=#{lc}`
end
end

desc "write languages to output/languages_{language}"
task :languages do
raise unless language = ENV['LANGUAGE']
raise unless (language = ENV.fetch('LANGUAGE', nil))
`mkdir -p output`
data = I18nData.languages(language.upcase)
File.write "output/languages_#{language.downcase}.yml", data.to_yaml
end

desc "write all countries to output to debug"
task :all_countries do
I18nData.languages.keys.each do |lc|
I18nData.languages.each_key do |lc|
`rake countries LANGUAGE=#{lc}`
end
end

desc "write countries to output/countries_{language} to debug"
task :countries do
raise unless language = ENV['LANGUAGE']
raise unless (language = ENV.fetch('LANGUAGE', nil))
`mkdir -p output`
data = I18nData.countries(language.upcase)
File.open("output/countries_#{language.downcase}.yml",'w') {|f|f.puts data.to_yaml}
File.open("output/countries_#{language.downcase}.yml", 'w') { |f| f.puts data.to_yaml }
end

desc "write example output, just to show off :D"
task :example_output do
`mkdir -p example_output`

#all names for germany, france, united kingdom and unites states
['DE','FR','GB','US'].each do |cc|
# all names for germany, france, united kingdom and unites states
['DE', 'FR', 'GB', 'US'].each do |cc|
names = I18nData.languages.keys.map do |lc|
begin
[I18nData.countries(lc)[cc], I18nData.languages[lc]]
rescue I18nData::NoTranslationAvailable
nil
end
[I18nData.countries(lc)[cc], I18nData.languages[lc]]
rescue I18nData::NoTranslationAvailable
nil
end
File.open("example_output/all_names_for_#{cc}.txt",'w') do |f|
f.puts names.reject(&:nil?).map{|x|x*" ---- "} * "\n"
File.open("example_output/all_names_for_#{cc}.txt", 'w') do |f|
f.puts names.compact.map { |x| x * " ---- " } * "\n"
end
end
end
Expand All @@ -66,7 +71,7 @@ task :stats do
dir = "cache/file_data_provider"
[:languages, :countries].each do |type|
files = FileList["#{dir}/#{type}*"]
lines = File.readlines(files.first).reject{|l|l.empty?}
lines = File.readlines(files.first).reject(&:empty?)
puts "#{lines.size} #{type} in #{files.size} languages"
end
end
Expand Down
5 changes: 3 additions & 2 deletions i18n_data.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
name = "i18n_data"
require "./lib/#{name}/version"

Expand All @@ -8,6 +9,6 @@ Gem::Specification.new name, I18nData::VERSION do |s|
s.homepage = "https:/grosser/#{name}"
s.files = `git ls-files lib cache `.split("\n")
s.license = "MIT"
s.required_ruby_version = '>= 2.5.0'
s.add_runtime_dependency 'simple_po_parser', '~> 1.1'
s.required_ruby_version = '>= 3.0.0' # keep in sync with rubocop.yml and actions.yml
s.add_dependency 'simple_po_parser', '~> 1.1'
end
18 changes: 9 additions & 9 deletions lib/i18n_data.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'i18n_data/version'

module I18nData

class BaseException < StandardError
def to_s
"#{self.class} -- #{super}"
Expand All @@ -13,13 +13,13 @@ class AccessDenied < BaseException; end
class Unknown < BaseException; end

class << self
def languages(language_code='EN')
def languages(language_code = 'EN')
fetch :languages, language_code do
data_provider.codes(:languages, normal_to_region_code(language_code.to_s.upcase))
end
end

def countries(language_code='EN')
def countries(language_code = 'EN')
fetch :countries, language_code do
data_provider.codes(:countries, normal_to_region_code(language_code.to_s.upcase))
end
Expand All @@ -34,10 +34,10 @@ def language_code(name)
end

def data_provider
@data_provider ||= (
@data_provider ||= begin
require 'i18n_data/file_data_provider'
FileDataProvider
)
end
end

def data_provider=(provider)
Expand All @@ -57,15 +57,15 @@ def fetch(type, language_code)
def normal_to_region_code(normal)
{
"ZH" => "zh_CN",
"BN" => "bn_IN",
"BN" => "bn_IN"
}[normal] || normal
end

def recognise_code(type, search)
search = search.strip

# common languages first <-> faster in majority of cases
language_codes = ['EN','ES','FR','DE','ZH'] | available_language_codes
language_codes = ['EN', 'ES', 'FR', 'DE', 'ZH'] | available_language_codes

language_codes.each do |language_code|
options =
Expand All @@ -88,8 +88,8 @@ def recognise_code(type, search)
# NOTE: this is not perfect since the used provider might have more or less languages available
# but it's better than just using the available english language codes
def available_language_codes
@available_languges ||= begin
files = Dir[File.expand_path("../../cache/file_data_provider/languages-*", __FILE__)]
@available_language_codes ||= begin
files = Dir[File.expand_path('../cache/file_data_provider/languages-*', __dir__)]
files.map! { |f| f[/languages-(.*)\./, 1] }
end
end
Expand Down
Loading

0 comments on commit a1f8a02

Please sign in to comment.