Class: Rosette::Core::PathMatcherFactory::FileExtensionNode

Inherits:
Node
  • Object
show all
Defined in:
lib/rosette/core/path_matcher_factory.rb

Overview

Matches file extensions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NodeOperatorFactory

#and, #not, #or

Methods included from NodeFactory

#match_file_extension, #match_file_extensions, #match_path, #match_paths, #match_regex, #match_regexes

Constructor Details

#initialize(extension) ⇒ FileExtensionNode

Creates a new file extension node.

Parameters:

  • extension (String)

    The extension to match.


243
244
245
# File 'lib/rosette/core/path_matcher_factory.rb', line 243

def initialize(extension)
  @extension = extension
end

Instance Attribute Details

#extensionString (readonly)

Returns the extension to match.

Returns:

  • (String)

    the extension to match.


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/rosette/core/path_matcher_factory.rb', line 237

class FileExtensionNode < Node
  attr_reader :extension

  # Creates a new file extension node.
  #
  # @param [String] extension The extension to match.
  def initialize(extension)
    @extension = extension
  end

  # Determines if the given path's file extension matches +extension+.
  #
  # @param [String] path The path to match.
  # @return [Boolean] true if the path matches +extension+, false otherwise.
  def matches?(path)
    # avoid using File.extname to allow matching against double extensions,
    # eg. file.html.erb
    path[-extension.size..-1] == extension
  end

  # Generates a string representation of this node.
  #
  # @return [String]
  def to_s
    "has_file_extension('#{extension}')"
  end
end

Instance Method Details

#matches?(path) ⇒ Boolean

Determines if the given path's file extension matches extension.

Parameters:

  • path (String)

    The path to match.

Returns:

  • (Boolean)

    true if the path matches extension, false otherwise.


251
252
253
254
255
# File 'lib/rosette/core/path_matcher_factory.rb', line 251

def matches?(path)
  # avoid using File.extname to allow matching against double extensions,
  # eg. file.html.erb
  path[-extension.size..-1] == extension
end

#to_sString

Generates a string representation of this node.

Returns:

  • (String)

260
261
262
# File 'lib/rosette/core/path_matcher_factory.rb', line 260

def to_s
  "has_file_extension('#{extension}')"
end