Class: Matchi::Change::From

Inherits:
Object
  • Object
show all
Defined in:
lib/matchi/change/from.rb,
lib/matchi/change/from/to.rb

Overview

Initial state wrapper for building a value transition matcher.

This class acts as a wrapper that captures the expected initial state and provides methods to build a complete transition matcher. When combined with the ‘to’ method, it creates a matcher that verifies both the starting and ending values of a change operation. This is useful when you need to ensure not only the final state but also the initial state of a value.

Examples:

Basic string transformation

text = "hello"
Change.new(text, :to_s).from("hello").to("HELLO").match? { text.upcase! }  # => true

Object state transition

class User
  attr_accessor :status
  def initialize
    @status = "pending"
  end
end

user = User.new
Change.new(user, :status).from("pending").to("active").match? {
  user.status = "active"
}  # => true

See Also:

Defined Under Namespace

Classes: To

Instance Method Summary collapse

Constructor Details

#initialize(expected, &state) ⇒ From

Initialize the wrapper with an object and a block.

Examples:

require "matchi/change/from"

object = "foo"

Matchi::Change::From.new("foo") { object.to_s }

Parameters:

  • expected (#object_id)

    An expected initial value.

  • state (Proc)

    A block of code to execute to get the state of the object.

Raises:

  • (::ArgumentError)


46
47
48
49
50
51
# File 'lib/matchi/change/from.rb', line 46

def initialize(expected, &state)
  raise ::ArgumentError, "a block must be provided" unless block_given?

  @expected = expected
  @state    = state
end

Instance Method Details

#to(expected_new_value) ⇒ #match?

Specifies the new value to expect.

Creates a complete transition matcher that verifies both the initial and final states of a value. The matcher will succeed only if the value starts at the expected initial state and changes to the specified new value after executing the test block.

Examples:

require "matchi/change/from"

object = "foo"

change_from_wrapper = Matchi::Change::From.new("foo") { object.to_s }
change_from_wrapper.to("FOO")

Parameters:

  • expected_new_value (#object_id)

    The new value to expect.

Returns:

  • (#match?)

    A *change from to* matcher.



71
72
73
# File 'lib/matchi/change/from.rb', line 71

def to(expected_new_value)
  To.new(@expected, expected_new_value, &@state)
end