Sha256: 95c116dbf0136f8afee9d1d945db241622d7a45b98d1ecc60fee035449e36b2a

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

require "spec_helper"

describe Yaoc::Strategies::ToArrayMapping do
  subject{
    Struct.new(:to_convert) do
      include Yaoc::MappingBase
      include Yaoc::Strategies::ToArrayMapping
    end
  }

  let(:mapper){
    subject.new(source_object)
  }

  let(:source_object){
    {id: 1, name: "paul"}
  }

  let(:expected_array){
    [1, "paul"]
  }

  describe ".call" do

    it "creates a hash from a object" do
      subject.map(0, :id)
      subject.map(1, :name)

      expect(mapper.call).to eq(expected_array)
    end


    it "uses my converter proc" do
      subject.map(0, :id)
      subject.map(3, :fullname, ->(source, result){ result[3] = "#{source.fetch(:name)} Hello World" })

      ext_expectation = expected_array.clone
      ext_expectation[3] = "#{ext_expectation[1]} Hello World"
      ext_expectation[1] = nil
      ext_expectation[2] = nil

      expect(mapper.call).to eq(ext_expectation)
    end

    context "changed fetcher method" do
      let(:source_object){
        Struct.new(:id, :name).new(1, "paul")
      }

      it "uses custom fetcher methods" do
        subject.map(0, :id)
        subject.map(1, :name)

        def mapper.fetcher
          :public_send
        end

        expect(mapper.call).to eq(expected_array)
      end

      it "works with arrays" do
        subject.map(1, 0)
        subject.map(0, 1)

        def mapper.fetcher
          :[]
        end

        mapper.to_convert = [1, "paul"]

        expect(mapper.call).to eq(expected_array.reverse)
      end
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yaoc-0.0.4 spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb
yaoc-0.0.3 spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb
yaoc-0.0.2 spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb
yaoc-0.0.1 spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb