hash_template

Transform a flat hash to a structured hash using template

Downloads
5.1K
Stars
3
Committers
1

hash_template

Transform a flat hash to a structured hash using template.

Installation

gem install hash_template

Example

require 'hash_template'
require 'pp'

data = {
  address: '[email protected]',
  password: 'No salt',
  first_name: 'Milton',
  last_name: 'Waddams',
  department: 'Reporting',
  title: 'Collator',
  privacy: true,
  org_unit_path: '/BasementDwellers'
}

mapping = {
  primaryEmail: :address,
  password: :password,
  name: {
    givenName: :first_name,
    familyName: :last_name
  },
  organizations: [
    {
      department: :department,
      title: :title
    }
  ],
  includeInGlobalAddressList: '!privacy',
  orgUnitPath: 'org_unit_path'
}

pp HashTemplate.new(mapping).map(data)
#{
#  primaryEmail: '[email protected]',
#  password: 'No salt',
#  name: {
#    givenName: 'Milton',
#    familyName: 'Waddams'
#  },
#  organizations: [
#    {
#      department: 'Reporting',
#      title: 'Collator'
#    }
#  ],
#  includeInGlobalAddressList: false,
#  orgUnitPath: '/BasementDwellers'
#}

Thanks

Inspired by Adam Crownoble's article: Transforming Hashes: A Refactoring Story

Backward transformation

For backward transformation use hash_map_hash gem.

License

MIT.