Last active
December 26, 2015 23:09
-
-
Save dkubb/7228155 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'coercible' | |
class Transformer | |
include Enumerable | |
COERCER = Coercible::Coercer.new | |
COERCIONS = Hash.new(:to_string).update( | |
id: :to_integer, | |
updated_on: :to_date, | |
active: :to_boolean, | |
) | |
def initialize(rows) | |
@rows = rows | |
end | |
def each | |
return to_enum unless block_given? | |
@rows.each do |row| | |
yield row.map { |key, value| | |
COERCER[value.class].public_send(COERCIONS[key], value) | |
} | |
end | |
self | |
end | |
end | |
rows = [ | |
{ | |
id: '1', | |
name: 'Dan Kubb', | |
updated_on: '2013-01-01', | |
active: 'yes', | |
} | |
] | |
Transformer.new(rows) |
warmwaffles
commented
Oct 30, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment