Class: Troy::ExtensionMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/troy/extension_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ExtensionMatcher

Returns a new instance of ExtensionMatcher.



11
12
13
14
# File 'lib/troy/extension_matcher.rb', line 11

def initialize(path)
  @path = path
  @matchers = {}
end

Instance Attribute Details

#matchersObject (readonly)

Returns the value of attribute matchers.



9
10
11
# File 'lib/troy/extension_matcher.rb', line 9

def matchers
  @matchers
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/troy/extension_matcher.rb', line 5

def path
  @path
end

#performedObject (readonly)

Returns the value of attribute performed.



7
8
9
# File 'lib/troy/extension_matcher.rb', line 7

def performed
  @performed
end

Instance Method Details

#default(&block) ⇒ Object



21
22
23
24
# File 'lib/troy/extension_matcher.rb', line 21

def default(&block)
  matchers["default"] = block
  self
end

#matchObject



26
27
28
29
30
31
32
# File 'lib/troy/extension_matcher.rb', line 26

def match
  matchers.each do |ext, handler|
    return handler.call if File.extname(path) == ext
  end

  matchers["default"]&.call
end

#on(extension, &block) ⇒ Object



16
17
18
19
# File 'lib/troy/extension_matcher.rb', line 16

def on(extension, &block)
  matchers[".#{extension}"] = block
  self
end