class Shrink::Wrap::Metadata

Public Instance Methods

add_coercion(property, coercion) click to toggle source
# File lib/shrink/wrap/metadata.rb, line 15
def add_coercion(property, coercion)
  coercions[property] = Shrink::Wrap::Property::Coercion.new(coercion)
end
add_transformer(klass, opts = {}) click to toggle source
# File lib/shrink/wrap/metadata.rb, line 6
def add_transformer(klass, opts = {})
  transformers.push(klass.new(opts))
end
add_translation(property, opts = {}) click to toggle source
# File lib/shrink/wrap/metadata.rb, line 10
def add_translation(property, opts = {})
  params = { from: property }.merge(opts)
  translations[property] = Shrink::Wrap::Property::Translation.new(params)
end
coerce(data) click to toggle source
# File lib/shrink/wrap/metadata.rb, line 38
def coerce(data)
  data.each_with_object({}) do |(property, value), memo|
    next if value.nil?

    coercion = coercions[property]

    memo[property] = if coercion
                       coercion.coerce(value)
                     else
                       value
                     end
  end
end
transform(data) click to toggle source
# File lib/shrink/wrap/metadata.rb, line 19
def transform(data)
  transformers.each do |transformer|
    data = transformer.transform(data)
  end
  data
end
translate(data) click to toggle source
# File lib/shrink/wrap/metadata.rb, line 26
def translate(data)
  return data if translate_all

  translations.transform_values do |translation|
    translation.translate(data)
  end
end
translate_all!() click to toggle source
# File lib/shrink/wrap/metadata.rb, line 34
def translate_all!
  @translate_all = true
end

Private Instance Methods

coercions() click to toggle source
# File lib/shrink/wrap/metadata.rb, line 66
def coercions
  @coercions ||= {}
end
transformers() click to toggle source
# File lib/shrink/wrap/metadata.rb, line 58
def transformers
  @transformers ||= []
end
translate_all() click to toggle source
# File lib/shrink/wrap/metadata.rb, line 54
def translate_all
  @translate_all ||= false
end
translations() click to toggle source
# File lib/shrink/wrap/metadata.rb, line 62
def translations
  @translations ||= {}
end