Skip to content

Commit

Permalink
Merge 9ae587f into 3765da6
Browse files Browse the repository at this point in the history
  • Loading branch information
denvazh committed Aug 28, 2017
2 parents 3765da6 + 9ae587f commit 595dcb5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ task test: :spec

# Testing with rspec
RSpec::Core::RakeTask.new(:spec) do |task|
task.rspec_opts = %w(--color --format documentation)
task.rspec_opts = %w[--color --format documentation]
end
task test: :spec

Expand All @@ -22,8 +22,8 @@ end
# Documentation
desc "Generate gem documentation (same as running 'rake yard')"
YARD::Rake::YardocTask.new do |t|
t.files = %w(lib/**/*.rb)
t.stats_options = %w(--list-undoc --compact)
t.files = %w[lib/**/*.rb]
t.stats_options = %w[--list-undoc --compact]
end
task doc: :yard

Expand Down
2 changes: 1 addition & 1 deletion faker_japanese.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
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 'looksee', '~> 4.0.0'
spec.add_development_dependency 'pry', '~> 0.10.1'
spec.add_development_dependency 'yard', '~> 0.8.7.6'
spec.add_development_dependency 'fuubar'
Expand Down
25 changes: 11 additions & 14 deletions lib/faker_japanese.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require 'faker'
require 'yaml'
require 'pathname'

# Faker
module Faker
# Japanese localization for Faker
module Japanese
# Location on the disk of this script
SELFDIR = File.expand_path(File.dirname(__FILE__))
# Working directory where this script is located
WORKDIR = Pathname.new(::File.expand_path(::File.dirname(__FILE__)))

# Kanji string
class Kanji < String
Expand All @@ -33,14 +34,14 @@ def initialize(kanji, yomi, kana, romaji)
# Faker Base class
class Base
class << self
# Using callback to store loaded data when subclass is created
# Use callback to store loaded data when subclass is created
# @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

# Convert full class name to lower case string with class name only
# Convert full class name to the lower case string with class name only
# @param [Class] klass class name
# @return [String]
def demodulize(klass)
Expand All @@ -51,7 +52,7 @@ def demodulize(klass)
# @param [String] filepath full path to the file
# @return [Hash]
def load_raw_yaml(filepath)
return nil unless filepath && File.readable?(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
Expand All @@ -60,10 +61,8 @@ def load_raw_yaml(filepath)
# Created dictionary with loaded data
# @param [Module] klass class name
def load_data(klass)
datafile = File.join(SELFDIR,
'faker_japanese',
'data', "#{demodulize(klass)}.yml")
data = load_raw_yaml(datafile)
datafile = "#{demodulize(klass)}.yml"
data = load_raw_yaml(WORKDIR.join('faker_japanese', 'data', datafile))
return data if data.nil?
data.inject({}) do |res, item|
key, values = item
Expand All @@ -72,15 +71,15 @@ def load_data(klass)
data
end

# Fetch random value from name dictionary
# Fetch random value from the name dictionary
# @param [String] key in the @@data
# @return [Kanji]
def fetch(key)
val = class_variable_get('@@data')[key]
val[rand(val.size)]
end

# Swap method if block was evaluated to true
# Swap methods if block was evaluated to true
# @param [Class] klass name of the class
# @param [Symbol] name method name
def swap_method(klass, name)
Expand All @@ -94,9 +93,7 @@ def swap_method(klass, name)
# @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
swap_method(klass, name) { Faker::Config.locale == :ja }
end
end # << self
end # Base
Expand Down
2 changes: 1 addition & 1 deletion lib/faker_japanese/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module Faker
# Japanese localization for Faker
module Japanese
# Current version
VERSION = '0.2.2'
VERSION = '0.2.2'.freeze
end
end
6 changes: 3 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require 'coveralls'

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
Coveralls::SimpleCov::Formatter,
SimpleCov::Formatter::HTMLFormatter
Coveralls::SimpleCov::Formatter,
SimpleCov::Formatter::HTMLFormatter
]
SimpleCov.start do
# disable coverage tracking
Expand All @@ -22,7 +22,7 @@
class String
# Method to check if string contains cjk characters
def contains_cjk?
!!(self =~ /\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}/)
(self =~ /\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}/)
end
end

Expand Down

0 comments on commit 595dcb5

Please sign in to comment.