module Hashie::Extensions::Dash::PropertyTranslation::ClassMethods

Defines input translations and value transformations.

@example

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  property :name, from: :fullName
end
klass.translations # => {fullName: :name}

Attributes

transforms[R]

@example Inspect value transformers

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  property :age, transform_with: ->(value) { value.to_i }
end
klass.transforms[:age].call('37') # => 37
klass.translations_hash # => {}
translations_hash[R]

@example Inspect value transformers

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  property :age, transform_with: ->(value) { value.to_i }
end
klass.transforms[:age].call('37') # => 37
klass.translations_hash # => {}

Public Instance Methods

inherited(klass) click to toggle source

Ensures that any inheriting classes maintain their translations.

  • `:default` - The class inheriting the translations.

@example Copy translation settings to subclasses

parent = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  property :name, from: :fullName
end
child = Class.new(parent) { property :age, from: :years }
child.new(fullName: 'Ada', years: 37).age # => 37
parent.translations # => {fullName: :name}
Calls superclass method
# File lib/hashie/extensions/dash/property_translation.rb, line 88
def inherited(klass)
  super
  klass.instance_variable_set(:@transforms, transforms.dup)
  klass.instance_variable_set(:@translations_hash, _deep_dup(translations_hash))
end
inverse_translations() click to toggle source

@example Map destination properties back to source keys

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  property :name, from: :fullName
end
klass.inverse_translations # => {name: :fullName}
# File lib/hashie/extensions/dash/property_translation.rb, line 195
def inverse_translations
  @inverse_translations ||= {}.tap do |translations|
    translations_hash.each do |(property_name, property_translations)|
      property_translations.each_key do |key|
        translations[key] = property_name
      end
    end
  end
end
permitted_input_keys() click to toggle source

@example List translated input keys alongside untranslated properties

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  property :name, from: :fullName
  property :age
end
klass.permitted_input_keys # => [:fullName, :age]
# File lib/hashie/extensions/dash/property_translation.rb, line 101
def permitted_input_keys
  @permitted_input_keys ||=
    properties
    .map { |property| inverse_translations.fetch property, property }
end
property(property_name, options = {}) click to toggle source

Defines a property on the Trash. Options are as follows:

  • `:default` - Specify a default value for this property, to be

returned before a value is set on the property in a new Dash.

  • `:from` - Specify the original key name that will be write only.

  • `:with` - Specify a lambda to be used to convert value.

  • `:transform_with` - Specify a lambda to be used to convert value

without using the :from option. It transform the property itself.

@example Declare a translated and converted property

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  property :age, from: :years, with: ->(value) { value.to_i }
end
klass.new(years: '37').age # => 37
Calls superclass method
# File lib/hashie/extensions/dash/property_translation.rb, line 122
def property(property_name, options = {})
  super

  from = options[:from]
  converter = options[:with]
  transformer = options[:transform_with]

  if from
    fail_self_transformation_error!(property_name) if property_name.to_s == from.to_s
    define_translation(from, property_name, converter || transformer)
    define_writer_for_source_property(from)
  elsif valid_transformer?(transformer)
    transforms[property_name] = transformer
  end
end
transformation_exists?(name) click to toggle source

@example Check for a property transformer

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  property :age, transform_with: ->(value) { value.to_i }
end
klass.transformation_exists?(:age) # => true
klass.transformation_exists?(:name) # => false
# File lib/hashie/extensions/dash/property_translation.rb, line 155
def transformation_exists?(name)
  transforms.key? name
end
transformed_property(property_name, value) click to toggle source

@example Apply a declared transformer directly

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  property :age, transform_with: ->(value) { value.to_i }
end
klass.transformed_property(:age, '37') # => 37
# File lib/hashie/extensions/dash/property_translation.rb, line 144
def transformed_property(property_name, value)
  transforms[property_name].call(value)
end
translation_exists?(name) click to toggle source

@example Check for a translated source key

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  property :name, from: :fullName
end
klass.translation_exists?(:fullName) # => true
klass.translation_exists?(:name) # => false
# File lib/hashie/extensions/dash/property_translation.rb, line 166
def translation_exists?(name)
  translations_hash.key? name
end
translations() click to toggle source

@example Map source keys to destination properties

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  property :name, from: :fullName
end
klass.translations # => {fullName: :name}
# File lib/hashie/extensions/dash/property_translation.rb, line 176
def translations
  @translations ||= {}.tap do |translations|
    translations_hash.each do |(property_name, property_translations)|
      translations[property_name] =
        if property_translations.size > 1
          property_translations.keys
        else
          property_translations.keys.first
        end
    end
  end
end

Private Instance Methods

define_translation(from, property_name, translator) click to toggle source
# File lib/hashie/extensions/dash/property_translation.rb, line 207
def define_translation(from, property_name, translator)
  translations_hash[from][property_name] = translator
end
define_writer_for_source_property(property) click to toggle source
# File lib/hashie/extensions/dash/property_translation.rb, line 211
def define_writer_for_source_property(property)
  define_method "#{property}=" do |val|
    __translations[property].each do |name, with|
      self[name] = with.respond_to?(:call) ? with.call(val) : val
    end
  end
end
fail_self_transformation_error!(property_name) click to toggle source
# File lib/hashie/extensions/dash/property_translation.rb, line 219
def fail_self_transformation_error!(property_name)
  raise ArgumentError,
        "property name `#{property_name}` and :from option must not be the same"
end
valid_transformer?(transformer) click to toggle source
# File lib/hashie/extensions/dash/property_translation.rb, line 224
def valid_transformer?(transformer)
  transformer.respond_to? :call
end