Skip to content

Instantly share code, notes, and snippets.

@robhanlon22
Last active December 24, 2015 08:39
Show Gist options
  • Save robhanlon22/6771684 to your computer and use it in GitHub Desktop.
Save robhanlon22/6771684 to your computer and use it in GitHub Desktop.
require 'json'
require 'hashie'
def _transform(object, *tasks)
[*tasks]
.map { |task| task.respond_to?(:call) ? task : task.first.method(task.last) }
.reduce { |memo, task| ->(*args) { task.(memo.(*args)) } }
.call(object)
end
def JSON.read(filename)
_transform \
filename,
[File, :read],
[JSON, :parse],
->(json) do
new = Hashie::Mash.method(:new)
json.kind_of?(Array) ? json.map(&new) : new.call(json)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment