Sha256: feb5bbcc880fc744ec1bcdc9af0d6d0b66cc46be63f29db61e0f32656084a17c

Contents?: true

Size: 653 Bytes

Versions: 8

Compression:

Stored size: 653 Bytes

Contents

require "metamorpher"

class RefactorWhereFirstStrictMocks
  include Metamorpher::Refactorer
  include Metamorpher::Builders::Ruby

  def pattern
    builder.build("TYPE.expects(:where).with(PARAMS_).returns(ARRAY_VALUE)")
  end

  def replacement
    builder
      .build("TYPE.expects(:find_by).with(PARAMS_).returns(SINGLE_VALUE)")
      .deriving("SINGLE_VALUE", "ARRAY_VALUE") { |array_value| take_first(array_value) }
  end

  private

  # Refactor the argument from [] to nil, or from [X] to X
  def take_first(array_value)
    if array_value.children.empty?
      builder.build("nil")
    else
      array_value.children.first
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
metamorpher-0.2.6 examples/refactorings/rails/where_first/refactorers/refactor_where_first_strict_mocks.rb
metamorpher-0.2.5 examples/refactorings/rails/where_first/refactorers/refactor_where_first_strict_mocks.rb
metamorpher-0.2.4 examples/refactorings/rails/where_first/refactorers/refactor_where_first_strict_mocks.rb
metamorpher-0.2.3 examples/refactorings/rails/where_first/refactorers/refactor_where_first_strict_mocks.rb
metamorpher-0.2.2 examples/refactorings/rails/where_first/refactorers/refactor_where_first_strict_mocks.rb
metamorpher-0.2.1 examples/refactorings/rails/where_first/refactorers/refactor_where_first_strict_mocks.rb
metamorpher-0.2.0 examples/refactorings/rails/where_first/refactorers/refactor_where_first_strict_mocks.rb
metamorpher-0.1.1 examples/refactorings/rails/where_first/refactorers/refactor_where_first_strict_mocks.rb