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.



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

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

Instance Attribute Details

#matchersObject (readonly)

Returns the value of attribute matchers.



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

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.



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

def performed
  @performed
end

Instance Method Details

#default(&block) ⇒ Object



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

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

#matchObject



22
23
24
25
26
27
28
# File 'lib/troy/extension_matcher.rb', line 22

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

  matchers["default"]&.call
end

#on(extension, &block) ⇒ Object



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

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