Class: Browsable::Sources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/browsable/sources/base.rb

Overview

Base class for a file source. A source expands a set of globs (relative to the project root) into a concrete, de-duplicated list of file paths.

Sources only discover files — routing each file to the right analyzer is the orchestrator’s job, done by extension.

Direct Known Subclasses

Builds, Javascripts, PublicAssets, Stylesheets, Views

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, globs:, excludes: []) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
# File 'lib/browsable/sources/base.rb', line 13

def initialize(root:, globs:, excludes: [])
  @root = File.expand_path(root)
  @globs = Array(globs)
  @excludes = Array(excludes)
end

Instance Attribute Details

#excludesObject (readonly)

Returns the value of attribute excludes.



11
12
13
# File 'lib/browsable/sources/base.rb', line 11

def excludes
  @excludes
end

#globsObject (readonly)

Returns the value of attribute globs.



11
12
13
# File 'lib/browsable/sources/base.rb', line 11

def globs
  @globs
end

#rootObject (readonly)

Returns the value of attribute root.



11
12
13
# File 'lib/browsable/sources/base.rb', line 11

def root
  @root
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


34
# File 'lib/browsable/sources/base.rb', line 34

def any? = files.any?

#filesObject

The discovered files: existing, non-excluded, sorted, unique.



25
26
27
28
29
30
31
32
# File 'lib/browsable/sources/base.rb', line 25

def files
  globs
    .flat_map { |glob| Dir.glob(File.join(root, glob), File::FNM_EXTGLOB) }
    .select { |path| File.file?(path) }
    .reject { |path| excluded?(path) }
    .uniq
    .sort
end

#nameObject

A short symbol naming this source, used in reports (e.g. :stylesheets).



20
21
22
# File 'lib/browsable/sources/base.rb', line 20

def name
  self.class.name.split("::").last.gsub(/([a-z])([A-Z])/, '\1_\2').downcase.to_sym
end