https://github.com/lightsofapollo/mappable_attributes
DSL to create aliases of keys in a hash
https://github.com/lightsofapollo/mappable_attributes
Last synced: 8 months ago
JSON representation
DSL to create aliases of keys in a hash
- Host: GitHub
- URL: https://github.com/lightsofapollo/mappable_attributes
- Owner: lightsofapollo
- License: bsd-3-clause
- Created: 2011-07-26T00:47:32.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2011-07-31T06:05:18.000Z (almost 15 years ago)
- Last Synced: 2024-12-27T15:12:43.976Z (over 1 year ago)
- Language: Ruby
- Homepage:
- Size: 117 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# What is MappableAttributes?
Often I find myself needing to rename the keys of one hash into another.
A trival example of this looks like:
hash1 = {:werid_name => 'value'}
hash2 = {:name => hash1[:werid_name]}
If you only need to rename one or two keys a simple approach like the
above is fine.
This library was designed for my need of renaming multiple
fields with extensability via blocks and by inherting from MappableAttributes::Base.
An example on a sudo Model.
class SudoModel
@@renamer = MappableAttributes::Base.new do
map :output_name => :input_name
map :another_name do |input|
input[:input_name].downcase
end
# ....
end
def attributes
{
:input_name => 'FIRST LAST'
}
end
def export_attributes
@renamer.map_attributes(attributes)
# =>
# {
# :output_name => 'FIRST LAST',
# :another_name => 'first last'
# }
end
end