class Locd::Pattern

Abstract base class for patterns used to match {Locd::Agent} instances.

Attributes

source[R]

Raw source string provided at initialization.

@return [String]

Public Class Methods

from(object, **options) click to toggle source

Factory method to construct the correct concrete subclass.

@param [String | Locd::Pattern] object

1.  {String}        - a new pattern will be constructed.
2.  {Locd::Pattern} - will just be returned.

@param [Hash<Symbol, Object>] options

Options that will be passed to the concrete class constructor when
`object` is a string.

@option options [Boolean] full:

When `true`, pattern must match entire labels.

When `false`, pattern may match any portion of label.

Label patterns only.

@option options [Boolean] ignore_case:

Case-insensitive match (label patterns only).

@option options [Boolean] recursive:

Additionally match agents with `workdir` in subdirectories (workdir
patterns only).

@option options [String | Pathname] cwd:

Current working directory to base relative paths from (workdir patterns
only).

@return [Locd::Pattern::Label | Locd::Pattern::Workdir]

Pattern instance.
# File lib/locd/pattern.rb, line 42
  def self.from object, **options
    case object
    when String
      string = object
      case string
      when ''
        raise ArgumentError, "Empty string is not a valid pattern"
      when /\A[\.\~\/]/
        Locd::Pattern::Workdir.new string, **options
      else
        Locd::Pattern::Label.new string, **options
      end
    when Locd::Pattern
      object
    else
      raise TypeError.new binding.erb <<-END
        Expected `object` to be {String} or {Locd::Pattern}, found <%= object.class %>
        
        `object` (first argument):
        
            <%= object.pretty_inspect %>
        
        Options:
        
            <%= options.pretty_inspect %>
        
      END
    end
  end
new(source) click to toggle source

Construct a new pattern. Should only be called via `super`.

@param [String] source

Raw source string the pattern is built off.
# File lib/locd/pattern.rb, line 85
def initialize source
  @source = source
end

Public Instance Methods

match?(agent) click to toggle source

@raise [NRSER::AbstractMethodError]

Abstract method, concrete subclasses must implement.
# File lib/locd/pattern.rb, line 93
def match? agent
  raise NRSER::AbstractMethodError.new( self, __method__ )
end