class TwitterCldr::Resources::Importer

Constants

DEFAULT_ENGINE

Attributes

params[R]
requirements[R]

Public Class Methods

default_params() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 38
def default_params
  parameters.merge(
    output_path: @output_path,
    locales: get_locales,
    ruby_engine: @ruby_engine || DEFAULT_ENGINE
  )
end
locales(locs) click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 30
def locales(locs)
  @locales = locs
end
new(options = {}) click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 67
def initialize(options = {})
  @params = self.class.default_params.merge(options)
  @requirements = self.class.requirements
end
output_path(path) click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 22
def output_path(path)
  @output_path = if path.start_with?('/')
    path
  else
    File.join(TwitterCldr::RESOURCES_DIR, path)
  end
end
parameter(key, value) click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 34
def parameter(key, value)
  parameters[key] = value
end
parameters() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 50
def parameters
  @parameters ||= {}
end
requirement(name, *args) click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 13
def requirement(name, *args)
  const_name = "#{name.to_s.capitalize}Requirement".to_sym
  requirements[name] = Requirements.const_get(const_name).new(*args)
end
requirements() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 46
def requirements
  @requirements ||= {}
end
ruby_engine(engine) click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 18
def ruby_engine(engine)
  @ruby_engine = engine
end

Private Class Methods

get_locales() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 56
def get_locales
  if ENV.include?('LOCALES')
    ENV['LOCALES'].split(',').map { |loc| loc.strip.to_sym }
  else
    @locales
  end
end

Public Instance Methods

can_import?() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 72
def can_import?
  importability_errors.empty?
end
import() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 76
def import
  if can_import?
    puts "Importing #{name}..."
    prepare
    execute
  else
    raise "Can't import #{name}: #{importability_errors.first}"
  end
end
prepare() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 86
def prepare
  before_prepare
  requirements.each { |_, req| req.prepare }
  after_prepare
end

Private Instance Methods

after_prepare() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 123
def after_prepare
end
before_prepare() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 120
def before_prepare
end
compatible_engine?() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 109
def compatible_engine?
  case params.fetch(:ruby_engine)
    when :mri
      RUBY_ENGINE == 'ruby'
    when :jruby
      RUBY_ENGINE == 'jruby'
    else
      false
  end
end
importability_errors() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 103
def importability_errors
  @importability_errors ||= [].tap do |errors|
    errors << 'incompatible RUBY_ENGINE' unless compatible_engine?
  end
end
name() click to toggle source
# File lib/twitter_cldr/resources/importer.rb, line 94
def name
  @name ||= self.class.name
    .split('::').last
    .chomp('Importer')
    .gsub(/([A-Z])([a-z])/) { " #{$1.downcase}#{$2}" }
    .strip
    .tap { |n| n << 's' unless n.end_with?('s') }
end