Class: Philiprehberger::FileWatcher::Change

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/file_watcher/change.rb

Overview

Value object representing a single file system change.

Constant Summary collapse

VALID_TYPES =
%i[created modified deleted].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, type) ⇒ Change

Returns a new instance of Change.

Parameters:

  • path (String)

    the file path that changed

  • type (Symbol)

    the change type (:created, :modified, or :deleted)

Raises:

  • (ArgumentError)

    if the type is not valid



17
18
19
20
21
22
23
24
# File 'lib/philiprehberger/file_watcher/change.rb', line 17

def initialize(path, type)
  unless VALID_TYPES.include?(type)
    raise ArgumentError, "invalid change type: #{type.inspect} (must be one of #{VALID_TYPES.join(', ')})"
  end

  @path = path
  @type = type
end

Instance Attribute Details

#pathString (readonly)

absolute path to the changed file

Returns:

  • (String)

    the current value of path



9
10
11
# File 'lib/philiprehberger/file_watcher/change.rb', line 9

def path
  @path
end

#typeSymbol (readonly)

one of :created, :modified, or :deleted

Returns:

  • (Symbol)

    the current value of type



9
10
11
# File 'lib/philiprehberger/file_watcher/change.rb', line 9

def type
  @type
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Returns true if path and type match.

Returns:

  • (Boolean)

    true if path and type match



32
33
34
# File 'lib/philiprehberger/file_watcher/change.rb', line 32

def ==(other)
  other.is_a?(Change) && other.path == path && other.type == type
end

#hashInteger

Returns hash code.

Returns:

  • (Integer)

    hash code



38
39
40
# File 'lib/philiprehberger/file_watcher/change.rb', line 38

def hash
  [path, type].hash
end

#to_sString

Returns human-readable representation of the change.

Returns:

  • (String)

    human-readable representation of the change



27
28
29
# File 'lib/philiprehberger/file_watcher/change.rb', line 27

def to_s
  "#{type}: #{path}"
end