Class: Decant::Collection
- Inherits:
-
Object
- Object
- Decant::Collection
- Defined in:
- lib/decant/collection.rb
Instance Attribute Summary collapse
- #dir ⇒ Pathname readonly
- #ext ⇒ String? readonly
Instance Method Summary collapse
- #entries ⇒ Array<Pathname>
- #find(pattern) ⇒ Pathname?
- #glob(pattern) ⇒ Array<Pathname>
-
#initialize(dir:, ext: nil) ⇒ Collection
constructor
A new instance of Collection.
-
#relative_path_for(path) ⇒ String
The relative path of
pathwithin #dir. -
#slug_for(path) ⇒ String
The extension-less relative path of
pathwithin #dir.
Constructor Details
#initialize(dir:, ext: nil) ⇒ Collection
Returns a new instance of Collection.
15 16 17 18 |
# File 'lib/decant/collection.rb', line 15 def initialize(dir:, ext: nil) self.dir = dir self.ext = ext end |
Instance Attribute Details
#dir ⇒ Pathname
8 9 10 |
# File 'lib/decant/collection.rb', line 8 def dir @dir end |
#ext ⇒ String?
11 12 13 |
# File 'lib/decant/collection.rb', line 11 def ext @ext end |
Instance Method Details
#entries ⇒ Array<Pathname>
21 22 23 |
# File 'lib/decant/collection.rb', line 21 def entries glob('**/*') end |
#find(pattern) ⇒ Pathname?
If #ext is defined then pattern MUST NOT include the file’s extension as it will automatically be added, if #ext is nil then pattern MUST include the file’s extension - essentially becoming the file’s full relative path within #dir.
Technically pattern can be any pattern supported by Dir.glob though it’s more likely to simply be a file name.
36 37 38 |
# File 'lib/decant/collection.rb', line 36 def find(pattern) glob(pattern).first end |
#glob(pattern) ⇒ Array<Pathname>
43 44 45 |
# File 'lib/decant/collection.rb', line 43 def glob(pattern) dir.glob("#{pattern}#{ext}").select { |path| path.file? } end |
#relative_path_for(path) ⇒ String
The relative path of path within #dir.
52 53 54 |
# File 'lib/decant/collection.rb', line 52 def relative_path_for(path) path.relative_path_from(dir).to_s end |
#slug_for(path) ⇒ String
The extension-less relative path of path within #dir.
61 62 63 64 65 66 67 68 69 |
# File 'lib/decant/collection.rb', line 61 def slug_for(path) relative_path = relative_path_for(path) # The collection has no configured extension, files are identified by # their full (relative) path so there's no extension to remove. return relative_path if @delete_ext_regexp.nil? relative_path.sub(@delete_ext_regexp, '') end |