Class: Guard::Copy::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/copy/target.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, options = {}) ⇒ Target

Initialize a new target

Parameters:

  • pattern (String)

    the pattern for this target

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • glob (Symbol)

    target resolution mode, ‘:newest` or `:all`

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
# File 'lib/guard/copy/target.rb', line 12

def initialize(pattern, options = {})
  raise ArgumentError, 'pattern cannot be nil' unless pattern
  raise ArgumentError, 'pattern cannot be empty' if pattern.empty?
  @pattern = pattern
  @options = {
    :glob => :all
  }.merge(options)
  @paths = []
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/guard/copy/target.rb', line 5

def options
  @options
end

#pathsObject (readonly)

Returns the value of attribute paths.



5
6
7
# File 'lib/guard/copy/target.rb', line 5

def paths
  @paths
end

#patternObject (readonly)

Returns the value of attribute pattern.



5
6
7
# File 'lib/guard/copy/target.rb', line 5

def pattern
  @pattern
end

Instance Method Details

#absolute?Boolean

Returns true if the pattern is an absolute path.

Returns:

  • (Boolean)

    true if the pattern is an absolute path



36
37
38
# File 'lib/guard/copy/target.rb', line 36

def absolute?
  @pattern.start_with?('/')
end

#resolveBoolean

Resolve the target into one or more paths

Returns:

  • (Boolean)

    true if the pattern resolved to any paths



25
26
27
28
29
30
31
32
33
# File 'lib/guard/copy/target.rb', line 25

def resolve
  @paths.clear
  if @options[:glob] == :newest
    @paths.concat(Dir[@pattern].sort_by { |f| File.mtime(f) }.last(1))
  else
    @paths.concat(Dir[@pattern])
  end
  @paths.any?
end