Yahm
Yahm is a hash to hash translator for ruby.
Installation
Add this line to your application's Gemfile:
gem 'yahm'
And then execute:
$ bundle
Or install it yourself as:
$ gem install yahm
Dependencies
- thread (https://github.com/meh/ruby-thread) for threading support
Usage
Most basic example
require "yahm"
mapping = Yahm::Mapping.new do
map "/foo", to: "/bar"
end
mapping.apply_to({foo: 1})
# => {:bar=>1}
More advanced examples
require "yahm"
class Record
def initialize(hash)
@record = mapping.apply_to(hash)
end
private
def lookup_id(id)
# ...
end
def mapping
@mapping ||= Yahm::Mapping.new(context: self) do
map "/record/id", to: "/id", processed_by: lambda { |id| lookup_id(id) }
map "/record/count", to: "/count", processed_by: proc { |count| count.to_i }
map "/record/subject[0]", to: "/subjects/most_important_subject"
map "/record/languages[]", to: "/languages", force_array: true
map "/record/authors", to: "/authors", split_by: ";"
map "/record/version", to: "/version", default: 1
map "/record/label", to: lambda { |label, result| result[:label] = label }
end
end
end
Record.new({ ... })
=> { :id => "...", :count => ..., :subjects => { :most_important_subject => "..."}, ... }
Mapping options
context:
Something that responds to a method call, e.g. a class. Is used the resolve undefined methods of lambdas/procs.
use_threads: [true|false]
[experimental]
Use a thread pool to execute to rules of a mapping. The current implementation is very naiv. No locking of shared data etc. Use this only if you know what you are doing and if your rules a complex enough to compensate the overhead introduced by threading.
thread_pool_size: [number]
[experimental]
The number of threads used to execute rules if use_threads: true
.
Per rule options
to: [string|lambda|proc]
The to
option of a mapping rule can either be a string, naming the path in the resultung hash or a lambda/proc, which is executed within the created mapper. When a context
option was supplied to #new
or apply_to
methods are also resolved against this scope.
default: [value]
If a value would be nil
and default
is given, the default
value is used instead of nil
.
force_array: [true|false]
This ensures, that the value written in the result ist always a hash. Even when the result would be nil
, an empty array is created.
processed_by: [lambda|proc]
You can pass a lambda/proc as processed_by
option. Inside of lambdas or procs, self
corresponds to the context of the defined mapper. When a context
option was supplied to #new
or apply_to
methods are also resolved against this scope.
split_by: [string]
If a value is a string and split_by
is supplied, the string is splitted by the given character and an array would be returned.
Related work
- hash_mapper (https://github.com/ismasan/hash_mapper)
Contributing
- Fork it ( http://github.com/ubpb/yahm/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request