Sha256: 451cf73fdff639b8f665cbd3c6201876c54ceaf60371b1bb4b783c49ec9a7740

Contents?: true

Size: 1.83 KB

Versions: 9

Compression:

Stored size: 1.83 KB

Contents

require 'i18n'
require 'data_maker/utilities/array_utilities'
require 'data_maker/utilities/module_utilities'

module DataMaker
  BASE_LIB_PATH = File.expand_path("..", __FILE__)

  if I18n.respond_to?(:enforce_available_locales=)
    I18n.enforce_available_locales = false
  end

  I18n.load_path += Dir[File.join(BASE_LIB_PATH, 'data_maker', 'locales', '*.yml')]

  extend ModuleUtilities

  class Config
    @locale = nil

    class << self
      attr_writer :locale
      def locale
        @locale
      end
    end
  end

  LETTERS = [*'a'..'z']

  HEX = %w(0 1 2 3 4 5 6 7 8 9 A B C D E F)

  def self.hexify(*masks)
    begin
      if valid_mask?(*masks, /#/)
        masks.flatten.sample.gsub(/#/) { HEX.sample }
      end
    rescue
      return false
    end
  end

  def self.numerify(*masks)
    begin
      if valid_mask?(*masks, /#/)
        masks.flatten.sample.gsub(/#/) { rand(10).to_s }
      end
    rescue
      return false
    end
  end

  def self.letterify(*masks)
    begin
      if valid_mask?(*masks, /\?/)
        masks.flatten.sample.gsub(/\?/) { LETTERS.sample }
      end
    rescue
      return false
    end
  end

  def self.alphanumerify(masks)
    letterify(numerify(masks))
  end

  def self.translate(*args)
    options = args.last.is_a?(Hash) ? args.pop : {}
    options[:locale] ||= DataMaker::Config.locale
    options[:raise] = true
    I18n.translate(*(args.push(options)))
    rescue I18n::MissingTranslationData
    I18n.translate(*(args.push(options.merge(locale: :en))))
  end

  # Load all constants.
  Dir["#{BASE_LIB_PATH}/data_maker/**/*.rb"].sort.each do |f|
    require f
  end

  def self.valid_mask?(*masks, match_with)
    masks.each do |mask|
      unless mask.match(match_with)
        raise ArgumentError, "You must pass a #{match_with} sign sir!"
      end
    end
  end

  private_class_method :valid_mask?
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
data_maker-2.5.5 lib/data_maker.rb
data_maker-2.5.4 lib/data_maker.rb
data_maker-2.5.3 lib/data_maker.rb
data_maker-2.5.2 lib/data_maker.rb
data_maker-2.4.2 lib/data_maker.rb
data_maker-2.4.1 lib/data_maker.rb
data_maker-2.3.5 lib/data_maker.rb
data_maker-2.3.4 lib/data_maker.rb
data_maker-2.3.3 lib/data_maker.rb