class Faker::Japanese::Base

Faker Base class

Public Class Methods

demodulize(klass) click to toggle source

Convert full class name to the lower case string with class name only @param [Class] klass class name @return [String]

# File lib/faker_japanese.rb, line 47
def demodulize(klass)
  klass.to_s.split('::').last.downcase
end
fetch(key) click to toggle source

Fetch random value from the name dictionary @param [String] key in the @@data @return [Kanji]

# File lib/faker_japanese.rb, line 77
def fetch(key)
  val = class_variable_get('@@data')[key]
  val[rand(val.size)]
end
inherited(klass) click to toggle source

Use callback to store loaded data when subclass is created @param [Class] klass class name

# File lib/faker_japanese.rb, line 39
def inherited(klass)
  raise("#{klass} should be Class") unless klass.is_a?(Class)
  klass.class_variable_set '@@data', load_data(klass)
end
load_data(klass) click to toggle source

Created dictionary with loaded data @param [Module] klass class name

# File lib/faker_japanese.rb, line 63
def load_data(klass)
  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
    res.update(key => values.map! { |v| Kanji.new(*v) })
  end
  data
end
load_raw_yaml(filepath) click to toggle source

Load yml file with minor pre-processing @param [String] filepath full path to the file @return [Hash]

# File lib/faker_japanese.rb, line 54
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
swap_method(klass, name) { |? call : call| ... } click to toggle source

Swap methods if block was evaluated to true @param [Class] klass name of the class @param [Symbol] name method name

# File lib/faker_japanese.rb, line 85
def swap_method(klass, name)
  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
use_japanese_method(klass, name) click to toggle source

Decide which method to use based on value of Faker::Config.locale @param [Class] klass name of the class @param [Symbol] name method name

# File lib/faker_japanese.rb, line 95
def use_japanese_method(klass, name)
  swap_method(klass, name) { Faker::Config.locale == :ja }
end