Skip to content

Transform a flat hash to a structured hash using template

Notifications You must be signed in to change notification settings

denispeplin/hash_template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gem Version Build Status

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: 'milton.waddams@initech.com',
  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: 'milton.waddams@initech.com',
#  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.

About

Transform a flat hash to a structured hash using template

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages