class Natives::Catalog::Normalizer

Public Instance Methods

normalize(hash) click to toggle source
# File lib/natives/catalog/normalizer.rb, line 7
def normalize(hash)
  normalize_package_provider_hash(hash)
end

Protected Instance Methods

convert_to_hash(hash) { || ... } click to toggle source
# File lib/natives/catalog/normalizer.rb, line 17
def convert_to_hash(hash)
  return hash.to_h    if hash.respond_to?(:to_h)
  return hash.to_hash if hash.respond_to?(:to_hash)
  if block_given?
    yield
  else
    raise InvalidCatalogFormat
  end
end
default?(str) click to toggle source
# File lib/natives/catalog/normalizer.rb, line 13
def default?(str)
  'default' == str
end
normalize_native_package_list(list) click to toggle source
# File lib/natives/catalog/normalizer.rb, line 72
def normalize_native_package_list(list)
  Array(list)
end
normalize_package_provider_hash(hash) click to toggle source
# File lib/natives/catalog/normalizer.rb, line 27
def normalize_package_provider_hash(hash)
  hash = convert_to_hash(hash) do
    raise InvalidCatalogFormat,
      "expected a hash of package providers, but got: #{hash.inspect}"
  end

  hash.inject({}) do |normalized_hash, (k, v)|
    normalized_hash[k.to_s] = normalize_platform_hash(v)
    normalized_hash
  end
end
normalize_platform_hash(hash) click to toggle source
# File lib/natives/catalog/normalizer.rb, line 39
def normalize_platform_hash(hash)
  hash = convert_to_hash(hash) do
    raise InvalidCatalogFormat,
      "expected a hash of platforms, but got: #{hash.inspect}"
  end

  hash.inject({}) do |normalized_hash, (k, v)|
    platform = k.to_s
    value = if default?(platform)
              normalize_native_package_list(v)
            else
              normalize_version_hash(v)
            end
    normalized_hash[platform] = value
    normalized_hash
  end
end
normalize_version_hash(hash) click to toggle source
# File lib/natives/catalog/normalizer.rb, line 57
def normalize_version_hash(hash)
  hash = convert_to_hash(hash) do
    raise InvalidCatalogFormat,
      "expected a hash of versions, but got: #{hash.inspect}"
  end

  hash.inject({}) do |normalized_hash, (k, v)|
    versions = Array(k)
    versions.each do |version|
      normalized_hash[version.to_s] = normalize_native_package_list(v)
    end
    normalized_hash
  end
end