Skip to content

Instantly share code, notes, and snippets.

@robwilliams
Created May 19, 2016 12:11
Show Gist options
  • Save robwilliams/1c5472bf289c97e8b6b605abf07afbd5 to your computer and use it in GitHub Desktop.
Save robwilliams/1c5472bf289c97e8b6b605abf07afbd5 to your computer and use it in GitHub Desktop.
module Algolia
module Factory
class VariantAssociation
def initialize(association, mapper=nil)
@association = association
@mapper = mapper || Mapper::Variant.new
end
def build
objects = association.variants.map do |variant|
mapper.map(variant)
end
ObjectList.new(*objects)
end
private
attr_reader :association, :mapper
end
end
end
require 'spec_helper'
describe Algolia::Factory::VariantAssociation do
describe "#build" do
it "returns a list of mapped variants" do
mapper = instance_double(Algolia::Mapper::Variant)
variant_one, variant_two = double(Variant), double(Variant)
association = double('association', variants: [variant_one, variant_two])
map_one, map_two = double('mapped variant one'), double('mapped variant two')
allow(mapper).to receive(:map).once.with(variant_one).and_return(map_one)
allow(mapper).to receive(:map).once.with(variant_two).and_return(map_two)
result = described_class.new(association, mapper).build
expect(result).to be_a(Algolia::ObjectList)
expect(result).to match_array([map_one,map_two])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment