From c1c8244bba88854608e5e306404720563aa94e44 Mon Sep 17 00:00:00 2001 From: Denis Vazhenin Date: Thu, 8 Oct 2015 11:04:21 +0900 Subject: [PATCH 01/10] Updated all tests and adjusted code to use proper locale identifiers --- .gitignore | 12 +++- .rspec | 2 - Rakefile | 22 ++++-- bin/console | 11 +++ bin/setup | 7 ++ faker_japanese.gemspec | 43 +++++++----- lib/faker_japanese.rb | 103 ++++++++++++++-------------- lib/faker_japanese/name.rb | 10 +-- lib/faker_japanese/version.rb | 6 +- spec/lib/faker_japanese_spec.rb | 117 ++++++++++++++++++++++---------- spec/spec_helper.rb | 6 ++ 11 files changed, 215 insertions(+), 124 deletions(-) delete mode 100644 .rspec create mode 100755 bin/console create mode 100755 bin/setup diff --git a/.gitignore b/.gitignore index a3673bc..9c4e828 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,8 @@ .LSOverride # Icon must end with two \r -Icon +Icon + # Thumbnails ._* @@ -39,3 +40,12 @@ spec/reports test/tmp test/version_tmp tmp + +# Intellij IDEA +.idea +.idea_modules +*.iml +*.ipr +*.iws +*.sc +.history diff --git a/.rspec b/.rspec deleted file mode 100644 index b7afd2d..0000000 --- a/.rspec +++ /dev/null @@ -1,2 +0,0 @@ ---format Fuubar ---color diff --git a/Rakefile b/Rakefile index 461e22a..be5ac05 100644 --- a/Rakefile +++ b/Rakefile @@ -1,15 +1,23 @@ -require "bundler/gem_tasks" -require "rspec/core/rake_task" -require "yard" +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' +require 'yard' RSpec::Core::RakeTask.new -task :default => :spec task :test => :spec +# Testing with rspec +RSpec::Core::RakeTask.new(:spec) do |task| + task.rspec_opts = %w(--color --format documentation) +end +task test: :spec + # Documentation +desc "Generate gem documentation (same as running 'rake yard')" YARD::Rake::YardocTask.new do |t| - t.files = ['lib/**/*.rb'] # optional + t.files = %w(lib/**/*.rb) + t.stats_options = %w(--list-undoc --compact) end +task doc: :yard -# desc "Generate gem documentation (same as running 'rake yard')" -task :doc => :yard +# Default Rake task +task default: :spec diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..85e34ee --- /dev/null +++ b/bin/console @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby + +require 'bundler/setup' +require 'faker_japanese' + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# Use Pry by default +require 'pry' +Pry.start diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..b65ed50 --- /dev/null +++ b/bin/setup @@ -0,0 +1,7 @@ +#!/bin/bash +set -euo pipefail +IFS=$'\n\t' + +bundle install + +# Do any other automated setup that you need to do here diff --git a/faker_japanese.gemspec b/faker_japanese.gemspec index e267def..f41f6f7 100644 --- a/faker_japanese.gemspec +++ b/faker_japanese.gemspec @@ -4,24 +4,31 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'faker_japanese/version' Gem::Specification.new do |spec| - spec.name = "faker_japanese" - spec.version = Faker::Japanese::VERSION - spec.authors = ["Denis Vazhenin"] - spec.email = ["denis.vazhenin@me.com"] - spec.description = %q{Faker extension providing japanese names} - spec.summary = %q{Faker extension for japanese names} - spec.license = "MIT" + spec.name = 'faker_japanese' + spec.version = Faker::Japanese::VERSION + spec.authors = ['Denis Vazhenin'] + spec.email = ['denis.vazhenin@me.com'] + spec.description = %q{Faker extension providing japanese names} + spec.summary = %q{Faker extension for japanese names} + spec.license = 'MIT' - spec.files = `git ls-files`.split($/) - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ["lib"] + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ['lib'] - spec.add_runtime_dependency "faker" - spec.add_development_dependency "bundler", "~> 1.3" - spec.add_development_dependency "rake" - spec.add_development_dependency "rspec" - spec.add_development_dependency "rspec-expectations" - spec.add_development_dependency "fuubar" - spec.add_development_dependency "yard" + spec.add_development_dependency 'bundler', '~> 1.8' + spec.add_development_dependency 'rake', '~> 10.0' + spec.add_development_dependency 'rspec', '~> 3.2.0' + spec.add_development_dependency 'rspec-expectations', '~> 3.2.0' + spec.add_development_dependency 'rspec-mocks', '~> 3.2.1' + spec.add_development_dependency 'awesome_print', '~> 1.6.1' + spec.add_development_dependency 'looksee', '~> 3.1.0' + spec.add_development_dependency 'pry', '~> 0.10.1' + spec.add_development_dependency 'yard', '~> 0.8.7.6' + spec.add_development_dependency 'fuubar' + spec.add_development_dependency 'coveralls' + spec.add_development_dependency 'rubocop' + + spec.add_runtime_dependency 'faker' end diff --git a/lib/faker_japanese.rb b/lib/faker_japanese.rb index e81c14f..0ec32a3 100644 --- a/lib/faker_japanese.rb +++ b/lib/faker_japanese.rb @@ -1,68 +1,69 @@ require 'faker' +require 'yaml' module Faker - module Japanese - SELFDIR = File.expand_path(File.dirname(__FILE__)) + module Japanese + SELFDIR = File.expand_path(File.dirname(__FILE__)) - class Kanji < String - attr_reader :yomi, :kana, :romaji + class Kanji < String + attr_reader :yomi, :kana, :romaji - def initialize(kanji, yomi, kana, romaji) - super(kanji) - @yomi = yomi - @kana = kana - @romaji = romaji - end - end + def initialize(kanji, yomi, kana, romaji) + super(kanji) + @yomi = yomi + @kana = kana + @romaji = romaji + end + end - class Base - class << self + class Base + class << self - # Using callback to store loaded data when subclass is created - def inherited(klass) - klass.class_variable_set "@@data", load_data(klass) - end + # Using callback to store loaded data when subclass is created + def inherited(klass) + klass.class_variable_set '@@data', load_data(klass) + end - # Load fake data from yml file - def load_data(klass) - datafile = File.join(SELFDIR, 'faker_japanese', - 'data', "#{klass.to_s.split('::').last.downcase}.yml") - if datafile && File.readable?(datafile) - data = YAML.load_file(datafile).each_with_object({}){|(k,v), h| h[k.to_sym] = v} + # Load fake data from yml file + def load_data(klass) + datafile = File.join(SELFDIR, 'faker_japanese', + 'data', "#{klass.to_s.split('::').last.downcase}.yml") + if datafile && File.readable?(datafile) + data = YAML.load_file(datafile).each_with_object({}) { |(k, v), h| h[k.to_sym] = v } data.inject({}) do |res, item| - key, values, = item - res.update(key => values.map! { |v| Kanji.new(*v) }) + key, values, = item + res.update(key => values.map! { |v| Kanji.new(*v) }) end - else - nil - end - end + else + nil + end + end - # Search - def fetch(key) - val = self.class_variable_get("@@data")[key] - ret = val[rand(val.size)] - end + # Search + def fetch(key) + val = self.class_variable_get('@@data')[key] + val[rand(val.size)] + end - # Swap method if block was evaluated to true - def swap_method(klass, name) - original_method =klass.method(name) - new_method =method(name) - klass.singleton_class.send :define_method, name, proc { - yield ? new_method.call : original_method.call - } - end + # Swap method if block was evaluated to true + def swap_method(klass, name) + original_method =klass.method(name) + new_method =method(name) + klass.singleton_class.send(:define_method, + name, + proc { yield ? new_method.call : original_method.call }) + end - # Decide which method to use based on value of Faker::Config.locale - def use_japanese_method(klass, name) - swap_method(klass, name) do - Faker::Config.locale == "ja" - end - end + # Decide which method to use based on value of Faker::Config.locale + def use_japanese_method(klass, name) + swap_method(klass, name) do + Faker::Config.locale == :ja + end + end - end # << self - end # Base - end # Japanese + end # << self + end # Base + end # Japanese end # Faker require 'faker_japanese/version' diff --git a/lib/faker_japanese/name.rb b/lib/faker_japanese/name.rb index 61ad729..1d856bd 100644 --- a/lib/faker_japanese/name.rb +++ b/lib/faker_japanese/name.rb @@ -15,11 +15,11 @@ def last_name # Return fake name, which is a string combining last and first name def name - first_name =fetch(:first_name) - last_name =fetch(:last_name) - name_kanji ="#{last_name} #{first_name}" - name_yomi ="#{last_name.yomi} #{first_name.yomi}" - name_kana ="#{last_name.kana} #{first_name.kana}" + first_name =fetch(:first_name) + last_name =fetch(:last_name) + name_kanji ="#{last_name} #{first_name}" + name_yomi ="#{last_name.yomi} #{first_name.yomi}" + name_kana ="#{last_name.kana} #{first_name.kana}" name_romaji ="#{last_name.romaji} #{first_name.romaji}" Kanji.new(name_kanji, name_yomi, name_kana, name_romaji) end diff --git a/lib/faker_japanese/version.rb b/lib/faker_japanese/version.rb index a946c4b..a47ec89 100644 --- a/lib/faker_japanese/version.rb +++ b/lib/faker_japanese/version.rb @@ -1,5 +1,5 @@ module Faker - module Japanese - VERSION = "0.2.0" - end + module Japanese + VERSION = '0.2.1' + end end diff --git a/spec/lib/faker_japanese_spec.rb b/spec/lib/faker_japanese_spec.rb index 7a55bf3..5b4576b 100644 --- a/spec/lib/faker_japanese_spec.rb +++ b/spec/lib/faker_japanese_spec.rb @@ -1,48 +1,91 @@ require 'spec_helper' describe Faker::Japanese do + context 'when locale is NOT set to :ja' do - it "should use native methods when locale is not set to ja" do - if (Faker::Config.locale != 'ja') - name = Faker::Name.name - name.should_not be_nil - name.contains_cjk?.should == false - first_name = Faker::Name.first_name - first_name.should_not be_nil - first_name.contains_cjk?.should == false - last_name = Faker::Name.first_name - last_name.should_not be_nil - last_name.contains_cjk?.should == false - else - fail + before(:all) { + Faker::Config.locale = :en + } + + after(:all) { + Faker::Config.locale = :en + } + + it 'uses default locale' do + expect(Faker::Config.locale).to eq(:en) + expect(Faker::Config.locale).not_to eq(:ja) end - end - it "should not use japanese method when locale is not set to ja" do - name = Faker::Name.name - expect { name.yomi }.to raise_error NoMethodError - expect { name.kana }.to raise_error NoMethodError - expect { name.romaji }.to raise_error NoMethodError - end + describe Faker::Name, 'use original methods' do + it '#name' do + name = Faker::Name.name + expect(name).not_to be_nil + expect(name.ascii_only?).to be_truthy + expect(name.contains_cjk?).to be_falsey + expect { name.yomi }.to raise_error NoMethodError + expect { name.kana }.to raise_error NoMethodError + expect { name.romaji }.to raise_error NoMethodError + end - it "should use japanese method when locale is set to ja" do - Faker::Config.locale = 'ja' - if (Faker::Config.locale == 'ja') - first_name = Faker::Name.first_name - first_name.should_not be_nil - first_name.contains_cjk?.should == true - last_name = Faker::Name.last_name - last_name.should_not be_nil - last_name.contains_cjk?.should == true - name = Faker::Name.name - name.should_not be_nil - name.contains_cjk?.should == true - name.yomi.contains_cjk?.should == true - name.kana.contains_cjk?.should == true - name.romaji.contains_cjk?.should == false - else - fail + it '#first_name' do + first_name = Faker::Name.first_name + expect(first_name).not_to be_nil + expect(first_name.ascii_only?).to be_truthy + expect(first_name.contains_cjk?).to be_falsey + end + + it '#last_name' do + last_name = Faker::Name.last_name + expect(last_name).not_to be_nil + expect(last_name.ascii_only?).to be_truthy + expect(last_name.contains_cjk?).to be_falsey + end end end + context 'when locale is set to ja' do + + before(:all) { + Faker::Config.locale = :ja + } + + after(:all) { + Faker::Config.locale = :en + } + + it 'uses japanese locale :ja' do + expect(Faker::Config.locale).to eq(:ja) + expect(Faker::Config.locale).to_not eq(:en) + end + + describe Faker::Name, 'use japanese specific methods' do + it '#name' do + name = Faker::Name.name + expect(name).not_to be_nil + expect(name.ascii_only?).to be_falsey + expect(name.contains_cjk?).to be_truthy + [:yomi, :kana, :romaji].each do |method| + expect(name.respond_to?(method)).to be_truthy + end + expect(name.yomi.contains_cjk?).to be_truthy + expect(name.kana.contains_cjk?).to be_truthy + expect(name.romaji.contains_cjk?).to be_falsey + end + + it '#first_name' do + first_name = Faker::Name.first_name + expect(first_name).not_to be_nil + expect(first_name.ascii_only?).to be_falsey + expect(first_name.contains_cjk?).to be_truthy + end + + it '#last_name' do + last_name = Faker::Name.last_name + expect(last_name).not_to be_nil + expect(last_name.ascii_only?).to be_falsey + expect(last_name.contains_cjk?).to be_truthy + end + + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 528680f..ddbf60e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,7 @@ require 'faker_japanese' +require 'pry' + class String # Method to check if string contains cjk characters def contains_cjk? @@ -10,6 +12,10 @@ def contains_cjk? I18n.enforce_available_locales = true RSpec.configure do |config| + config.color = true config.failure_color = :magenta config.tty = true + config.expect_with :rspec do |c| + c.syntax = :expect + end end From bb311161d01e9eca1a4b3c9c9b60b0b57299f598 Mon Sep 17 00:00:00 2001 From: Denis Vazhenin Date: Thu, 8 Oct 2015 12:42:29 +0900 Subject: [PATCH 02/10] Added better documentation --- lib/faker_japanese.rb | 17 ++++++++++++++++- lib/faker_japanese/name.rb | 9 ++++++--- lib/faker_japanese/version.rb | 1 + spec/spec_helper.rb | 5 +++-- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/faker_japanese.rb b/lib/faker_japanese.rb index 0ec32a3..63bf66a 100644 --- a/lib/faker_japanese.rb +++ b/lib/faker_japanese.rb @@ -3,11 +3,18 @@ module Faker module Japanese + # Location on the disk of this script SELFDIR = File.expand_path(File.dirname(__FILE__)) + # Kanji string class Kanji < String attr_reader :yomi, :kana, :romaji + # Create new Kanji string instance + # @param [String] kanji string in kanji from + # @param [String] yomi string with hiragana readings + # @param [String] kana string with katakana readings + # @param [String] romaji romanized string def initialize(kanji, yomi, kana, romaji) super(kanji) @yomi = yomi @@ -20,11 +27,13 @@ class Base class << self # Using callback to store loaded data when subclass is created + # @param [Module] klass class name def inherited(klass) klass.class_variable_set '@@data', load_data(klass) end # Load fake data from yml file + # @param [Module] klass class name def load_data(klass) datafile = File.join(SELFDIR, 'faker_japanese', 'data', "#{klass.to_s.split('::').last.downcase}.yml") @@ -39,13 +48,17 @@ def load_data(klass) end end - # Search + # Fetch random value from name dictionary + # @param [String] key in the @@data + # @return [Kanji] def fetch(key) val = self.class_variable_get('@@data')[key] val[rand(val.size)] end # Swap method if block was evaluated to true + # @param [Module] klass name of the class + # @param [Symbol] name method name def swap_method(klass, name) original_method =klass.method(name) new_method =method(name) @@ -55,6 +68,8 @@ def swap_method(klass, name) end # Decide which method to use based on value of Faker::Config.locale + # @param [Module] klass name of the class + # @param [Symbol] name method name def use_japanese_method(klass, name) swap_method(klass, name) do Faker::Config.locale == :ja diff --git a/lib/faker_japanese/name.rb b/lib/faker_japanese/name.rb index 1d856bd..8d4afae 100644 --- a/lib/faker_japanese/name.rb +++ b/lib/faker_japanese/name.rb @@ -3,17 +3,20 @@ module Japanese class Name < Base class << self - # Return fake first name + # Fake first name + # @return [Kanji] def first_name fetch(:first_name) end - # Return fake last name + # Fake last name + # @return [Kanji] def last_name fetch(:last_name) end - # Return fake name, which is a string combining last and first name + # Fake full name + # @return [Kanji] string combining last and first name def name first_name =fetch(:first_name) last_name =fetch(:last_name) diff --git a/lib/faker_japanese/version.rb b/lib/faker_japanese/version.rb index a47ec89..24847df 100644 --- a/lib/faker_japanese/version.rb +++ b/lib/faker_japanese/version.rb @@ -1,5 +1,6 @@ module Faker module Japanese + # Current version VERSION = '0.2.1' end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ddbf60e..2a69ba2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,5 @@ require 'faker_japanese' -require 'pry' - class String # Method to check if string contains cjk characters def contains_cjk? @@ -19,3 +17,6 @@ def contains_cjk? c.syntax = :expect end end + +# For debugging +require 'pry' From e25c073ff52a28b2498c51aee6f0b6b5f5f7e85b Mon Sep 17 00:00:00 2001 From: Denis Vazhenin Date: Thu, 8 Oct 2015 12:46:06 +0900 Subject: [PATCH 03/10] Added coverage tracking --- spec/spec_helper.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2a69ba2..34cfeac 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,22 @@ +# Coverage and Coveralls +require 'simplecov' +require 'coveralls' + +SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ + Coveralls::SimpleCov::Formatter, + SimpleCov::Formatter::HTMLFormatter +] +SimpleCov.start do + # disable coverage tracking + add_filter 'spec' # rspec tests +end + +# Use Coveralls formatter only in CI environment +if ENV['CI'].eql?('true') || ENV['TRAVIS'].eql?('true') || ENV['COVERALLS_REPO_TOKEN'] + Coveralls.wear! +end + +$LOAD_PATH.unshift File.expand_path('../lib', __FILE__) require 'faker_japanese' class String From 6d10e1ae79032abe281a9a6208ca2e5dd12819d3 Mon Sep 17 00:00:00 2001 From: Denis Vazhenin Date: Thu, 8 Oct 2015 12:53:59 +0900 Subject: [PATCH 04/10] Added travis configuration --- .travis.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8ce12a7 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: ruby + +sudo: false + +rvm: + - 1.9.3 + - 2.2.1 + - ruby-head + +cache: bundler + +script: 'bundle exec rake test' + +matrix: + allow_failures: + - rvm: ruby-head + fast_finish: true From 590af9a6c23341ecbff03485605749eea34543c1 Mon Sep 17 00:00:00 2001 From: Denis Vazhenin Date: Thu, 8 Oct 2015 12:58:39 +0900 Subject: [PATCH 05/10] Added rubocop configuration --- .rubocop.yml | 9 +++++ .rubocop_todo.yml | 85 +++++++++++++++++++++++++++++++++++++++++++++++ Rakefile | 11 +++++- 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..7ed94d7 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,9 @@ +AllCops: + Exclude: + - spec/**/* + - exe/* + +inherit_from: .rubocop_todo.yml + +Style/FileName: + Enabled: false \ No newline at end of file diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..db19676 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,85 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2015-10-08 12:57:47 +0900 using RuboCop version 0.34.2. +# 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: 3 +# Configuration parameters: AllowURI, URISchemes. +Metrics/LineLength: + Max: 96 + +# Offense count: 1 +# Configuration parameters: CountComments. +Metrics/MethodLength: + Max: 11 + +# Offense count: 4 +# Configuration parameters: Exclude. +Style/Documentation: + Exclude: + - 'lib/faker_japanese.rb' + - 'lib/faker_japanese/name.rb' + - 'lib/faker_japanese/version.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/EmptyElse: + Exclude: + - 'lib/faker_japanese.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/EmptyLines: + Exclude: + - 'Rakefile' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/EmptyLinesAroundClassBody: + Exclude: + - 'lib/faker_japanese.rb' + - 'lib/faker_japanese/name.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues. +Style/HashSyntax: + Enabled: false + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: PreferredDelimiters. +Style/PercentLiteralDelimiters: + Exclude: + - 'faker_japanese.gemspec' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/RedundantSelf: + Exclude: + - 'lib/faker_japanese.rb' + +# Offense count: 8 +# Cop supports --auto-correct. +# Configuration parameters: MultiSpaceAllowedForOperators. +Style/SpaceAroundOperators: + Exclude: + - 'lib/faker_japanese.rb' + - 'lib/faker_japanese/name.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/SpecialGlobalVars: + Exclude: + - 'faker_japanese.gemspec' + +# Offense count: 2 +# Cop supports --auto-correct. +Style/UnneededPercentQ: + Exclude: + - 'faker_japanese.gemspec' diff --git a/Rakefile b/Rakefile index be5ac05..fc1c0ba 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,6 @@ require 'bundler/gem_tasks' require 'rspec/core/rake_task' +require 'rubocop/rake_task' require 'yard' RSpec::Core::RakeTask.new @@ -11,6 +12,14 @@ RSpec::Core::RakeTask.new(:spec) do |task| end task test: :spec +# Use RuboCop to check for code/style offenses +desc 'Run RuboCop to validate/lint code' +RuboCop::RakeTask.new(:rubocop) do |task| + # don't abort rake on failure + task.fail_on_error = false +end + + # Documentation desc "Generate gem documentation (same as running 'rake yard')" YARD::Rake::YardocTask.new do |t| @@ -20,4 +29,4 @@ end task doc: :yard # Default Rake task -task default: :spec +task default: [:rubocop, :spec] From e6197ff214af46427a2100b38e89b6935b604822 Mon Sep 17 00:00:00 2001 From: Denis Vazhenin Date: Thu, 8 Oct 2015 13:33:54 +0900 Subject: [PATCH 06/10] Fixed offences reported by rubocop --- .rubocop.yml | 2 +- .rubocop_todo.yml | 85 ----------------------------------- Rakefile | 3 +- faker_japanese.gemspec | 11 +++-- lib/faker_japanese.rb | 60 ++++++++++++++++--------- lib/faker_japanese/name.rb | 15 ++++--- lib/faker_japanese/version.rb | 1 + 7 files changed, 56 insertions(+), 121 deletions(-) delete mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml index 7ed94d7..5dd882a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,7 +3,7 @@ AllCops: - spec/**/* - exe/* -inherit_from: .rubocop_todo.yml +#inherit_from: .rubocop_todo.yml Style/FileName: Enabled: false \ No newline at end of file diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml deleted file mode 100644 index db19676..0000000 --- a/.rubocop_todo.yml +++ /dev/null @@ -1,85 +0,0 @@ -# This configuration was generated by -# `rubocop --auto-gen-config` -# on 2015-10-08 12:57:47 +0900 using RuboCop version 0.34.2. -# 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: 3 -# Configuration parameters: AllowURI, URISchemes. -Metrics/LineLength: - Max: 96 - -# Offense count: 1 -# Configuration parameters: CountComments. -Metrics/MethodLength: - Max: 11 - -# Offense count: 4 -# Configuration parameters: Exclude. -Style/Documentation: - Exclude: - - 'lib/faker_japanese.rb' - - 'lib/faker_japanese/name.rb' - - 'lib/faker_japanese/version.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/EmptyElse: - Exclude: - - 'lib/faker_japanese.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -Style/EmptyLines: - Exclude: - - 'Rakefile' - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/EmptyLinesAroundClassBody: - Exclude: - - 'lib/faker_japanese.rb' - - 'lib/faker_japanese/name.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues. -Style/HashSyntax: - Enabled: false - -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: PreferredDelimiters. -Style/PercentLiteralDelimiters: - Exclude: - - 'faker_japanese.gemspec' - -# Offense count: 1 -# Cop supports --auto-correct. -Style/RedundantSelf: - Exclude: - - 'lib/faker_japanese.rb' - -# Offense count: 8 -# Cop supports --auto-correct. -# Configuration parameters: MultiSpaceAllowedForOperators. -Style/SpaceAroundOperators: - Exclude: - - 'lib/faker_japanese.rb' - - 'lib/faker_japanese/name.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -Style/SpecialGlobalVars: - Exclude: - - 'faker_japanese.gemspec' - -# Offense count: 2 -# Cop supports --auto-correct. -Style/UnneededPercentQ: - Exclude: - - 'faker_japanese.gemspec' diff --git a/Rakefile b/Rakefile index fc1c0ba..fbff659 100644 --- a/Rakefile +++ b/Rakefile @@ -4,7 +4,7 @@ require 'rubocop/rake_task' require 'yard' RSpec::Core::RakeTask.new -task :test => :spec +task test: :spec # Testing with rspec RSpec::Core::RakeTask.new(:spec) do |task| @@ -19,7 +19,6 @@ RuboCop::RakeTask.new(:rubocop) do |task| task.fail_on_error = false end - # Documentation desc "Generate gem documentation (same as running 'rake yard')" YARD::Rake::YardocTask.new do |t| diff --git a/faker_japanese.gemspec b/faker_japanese.gemspec index f41f6f7..a51f131 100644 --- a/faker_japanese.gemspec +++ b/faker_japanese.gemspec @@ -8,12 +8,15 @@ Gem::Specification.new do |spec| spec.version = Faker::Japanese::VERSION spec.authors = ['Denis Vazhenin'] spec.email = ['denis.vazhenin@me.com'] - spec.description = %q{Faker extension providing japanese names} - spec.summary = %q{Faker extension for japanese names} + spec.description = 'Faker extension providing japanese names' + spec.summary = 'Faker extension for japanese names' spec.license = 'MIT' - spec.files = `git ls-files`.split($/) - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.bindir = 'exe' + spec.files = `git ls-files -z`.split("\x0").reject do |f| + f.match(%r{^(test|spec|features)/}) + end + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ['lib'] diff --git a/lib/faker_japanese.rb b/lib/faker_japanese.rb index 63bf66a..ecb537f 100644 --- a/lib/faker_japanese.rb +++ b/lib/faker_japanese.rb @@ -2,6 +2,7 @@ require 'yaml' module Faker + # Japanese localization for Faker module Japanese # Location on the disk of this script SELFDIR = File.expand_path(File.dirname(__FILE__)) @@ -23,59 +24,74 @@ def initialize(kanji, yomi, kana, romaji) end end + # Faker Base class class Base class << self - # Using callback to store loaded data when subclass is created - # @param [Module] klass class name + # @param [Class] klass class name def inherited(klass) + fail("#{klass} should be Class") unless klass.is_a? Class klass.class_variable_set '@@data', load_data(klass) end - # Load fake data from yml file + # Convert full class name to lower case string with class name only + # @param [Class] klass class name + # @return [String] + def demodulize(klass) + klass.to_s.split('::').last.downcase + end + + # Load yml file with minor pre-processing + # @param [String] filepath full path to the file + # @return [Hash] + def load_raw_yaml(filepath) + return nil unless filepath && File.readable?(filepath) + YAML.load_file(filepath).each_with_object({}) do |(k, v), h| + h[k.to_sym] = v + end + end + + # Created dictionary with loaded data # @param [Module] klass class name def load_data(klass) - datafile = File.join(SELFDIR, 'faker_japanese', - 'data', "#{klass.to_s.split('::').last.downcase}.yml") - if datafile && File.readable?(datafile) - data = YAML.load_file(datafile).each_with_object({}) { |(k, v), h| h[k.to_sym] = v } - data.inject({}) do |res, item| - key, values, = item - res.update(key => values.map! { |v| Kanji.new(*v) }) - end - else - nil + datafile = File.join(SELFDIR, + 'faker_japanese', + 'data', "#{demodulize(klass)}.yml") + data = load_raw_yaml(datafile) + return data if data.nil? + data.inject({}) do |res, item| + key, values = item + res.update(key => values.map! { |v| Kanji.new(*v) }) end + data end # Fetch random value from name dictionary # @param [String] key in the @@data # @return [Kanji] def fetch(key) - val = self.class_variable_get('@@data')[key] + val = class_variable_get('@@data')[key] val[rand(val.size)] end # Swap method if block was evaluated to true - # @param [Module] klass name of the class + # @param [Class] klass name of the class # @param [Symbol] name method name def swap_method(klass, name) - original_method =klass.method(name) - new_method =method(name) - klass.singleton_class.send(:define_method, - name, - proc { yield ? new_method.call : original_method.call }) + original_method = klass.method(name) + new_method = method(name) + proc = proc { yield ? new_method.call : original_method.call } + klass.singleton_class.send(:define_method, name, proc) end # Decide which method to use based on value of Faker::Config.locale - # @param [Module] klass name of the class + # @param [Class] klass name of the class # @param [Symbol] name method name def use_japanese_method(klass, name) swap_method(klass, name) do Faker::Config.locale == :ja end end - end # << self end # Base end # Japanese diff --git a/lib/faker_japanese/name.rb b/lib/faker_japanese/name.rb index 8d4afae..62969ed 100644 --- a/lib/faker_japanese/name.rb +++ b/lib/faker_japanese/name.rb @@ -1,8 +1,9 @@ module Faker + # Japanese localization for Faker module Japanese + # Name localization class Name < Base class << self - # Fake first name # @return [Kanji] def first_name @@ -18,12 +19,12 @@ def last_name # Fake full name # @return [Kanji] string combining last and first name def name - first_name =fetch(:first_name) - last_name =fetch(:last_name) - name_kanji ="#{last_name} #{first_name}" - name_yomi ="#{last_name.yomi} #{first_name.yomi}" - name_kana ="#{last_name.kana} #{first_name.kana}" - name_romaji ="#{last_name.romaji} #{first_name.romaji}" + first_name = fetch(:first_name) + last_name = fetch(:last_name) + name_kanji = "#{last_name} #{first_name}" + name_yomi = "#{last_name.yomi} #{first_name.yomi}" + name_kana = "#{last_name.kana} #{first_name.kana}" + name_romaji = "#{last_name.romaji} #{first_name.romaji}" Kanji.new(name_kanji, name_yomi, name_kana, name_romaji) end end diff --git a/lib/faker_japanese/version.rb b/lib/faker_japanese/version.rb index 24847df..32b3bce 100644 --- a/lib/faker_japanese/version.rb +++ b/lib/faker_japanese/version.rb @@ -1,4 +1,5 @@ module Faker + # Japanese localization for Faker module Japanese # Current version VERSION = '0.2.1' From f03ee047d77d79bb4516e3026fe4a0fb006f7804 Mon Sep 17 00:00:00 2001 From: Denis Vazhenin Date: Thu, 8 Oct 2015 13:42:19 +0900 Subject: [PATCH 07/10] Added documentation comment --- lib/faker_japanese.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/faker_japanese.rb b/lib/faker_japanese.rb index ecb537f..f06dd0d 100644 --- a/lib/faker_japanese.rb +++ b/lib/faker_japanese.rb @@ -1,6 +1,7 @@ require 'faker' require 'yaml' +# Faker module Faker # Japanese localization for Faker module Japanese From 7d5df99b35b04dd6dce2f36d7e100492edaa5ae6 Mon Sep 17 00:00:00 2001 From: Denis Vazhenin Date: Thu, 8 Oct 2015 13:52:45 +0900 Subject: [PATCH 08/10] Using same bundler version for all builds --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8ce12a7..0072f1d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,10 @@ rvm: cache: bundler -script: 'bundle exec rake test' +before_install: + - gem install bundler -v '1.8.6' + +script: bundle exec rake test matrix: allow_failures: From be9da356f3730416339a5e7bb16063c8073548be Mon Sep 17 00:00:00 2001 From: Denis Vazhenin Date: Thu, 8 Oct 2015 13:54:57 +0900 Subject: [PATCH 09/10] Removed builds on older ruby version --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0072f1d..9b9bfab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ language: ruby sudo: false rvm: - - 1.9.3 + - 2.1 - 2.2.1 - ruby-head From fdb6df9726f0fa0269fcc0fd6169125e68415e6e Mon Sep 17 00:00:00 2001 From: Denis Vazhenin Date: Thu, 8 Oct 2015 14:02:07 +0900 Subject: [PATCH 10/10] Alternative method to load library --- lib/faker/japanese.rb | 1 + 1 file changed, 1 insertion(+) create mode 100644 lib/faker/japanese.rb diff --git a/lib/faker/japanese.rb b/lib/faker/japanese.rb new file mode 100644 index 0000000..656b939 --- /dev/null +++ b/lib/faker/japanese.rb @@ -0,0 +1 @@ +require_relative '../faker_japanese'