Last active
August 29, 2015 14:12
-
-
Save fgarcia/189e2dc221fc51084780 to your computer and use it in GitHub Desktop.
generic array adapter
This file contains 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
class ArrayAdapter | |
def initialize(array, &block) | |
raise 'Adapter is useless without a block' unless block_given? | |
@raw = array | |
@transformer = block | |
end | |
def each(&block) | |
# StackOverflow | |
# http://stackoverflow.com/q/27798855/253098 | |
@raw.map { |raw| @transformer.call(raw) }.each(&block) | |
end | |
end | |
describe '#each' do | |
it 'applies transformation block' do | |
old = [1, 2, 3] | |
adapted = ArrayAdapter.new old do |v| | |
v.to_s | |
end | |
adapted.each do |v| | |
expect(v).to be_kind_of String | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment