Sha256: 893de8618102525ce2a64b88c91ce465b3cd7822fcc1f495c3b810135a9cad13

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

require "spec_helper"

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

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

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

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

  describe ".call" do

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

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

    it "renames attributes" do
      subject.map(:id, :id)
      subject.map(:fullname, :name)

      renamed_expectation = expected_hash.clone
      renamed_expectation[:fullname] = renamed_expectation.delete :name

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

    it "uses my converter proc" do
      subject.map(:id, :id)
      subject.map(:name, :fullname, ->(source, result){ result.merge({name: source.fetch(:name) + " Hello World"}) })

      ext_expectation = expected_hash.clone
      ext_expectation[:name] += " Hello World"

      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(:id, :id)
        subject.map(:name, :name)

        def mapper.fetcher
          :public_send
        end

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

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

        def mapper.fetcher
          :[]
        end

        mapper.to_convert = [1, "paul"]

        expect(mapper.call).to eq(expected_hash)
      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_hash_mapping_spec.rb
yaoc-0.0.3 spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb
yaoc-0.0.2 spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb
yaoc-0.0.1 spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb