Class: Locd::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/locd/pattern.rb

Overview

Abstract base class for patterns used to match Agent instances.

Direct Known Subclasses

Label, Workdir

Defined Under Namespace

Classes: Label, Workdir

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Pattern

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

Parameters:

  • source (String)

    Raw source string the pattern is built off.



85
86
87
# File 'lib/locd/pattern.rb', line 85

def initialize source
  @source = source
end

Instance Attribute Details

#sourceString (readonly)

Raw source string provided at initialization.

Returns:

  • (String)


77
78
79
# File 'lib/locd/pattern.rb', line 77

def source
  @source
end

Class Method Details

.from(object, **options) ⇒ Locd::Pattern::Label | Locd::Pattern::Workdir

Factory method to construct the correct concrete subclass.

Parameters:

  • object (String | Locd::Pattern)
    1. String - a new pattern will be constructed.
    2. Locd::Pattern - will just be returned.
  • options (Hash<Symbol, Object>)

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

Options Hash (**options):

  • full: (Boolean)

    When true, pattern must match entire labels.

    When false, pattern may match any portion of label.

    Label patterns only.

  • ignore_case: (Boolean)

    Case-insensitive match (label patterns only).

  • recursive: (Boolean)

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

  • cwd: (String | Pathname)

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

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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

Instance Method Details

#match?(agent) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NRSER::AbstractMethodError)

    Abstract method, concrete subclasses must implement.



93
94
95
# File 'lib/locd/pattern.rb', line 93

def match? agent
  raise NRSER::AbstractMethodError.new( self, __method__ )
end