locomotivecms/steam

View on GitHub
lib/locomotive/steam/adapters/filesystem/yaml_loaders/theme_asset.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Locomotive
  module Steam
    module Adapters
      module Filesystem
        module YAMLLoaders

          class ThemeAsset

            include Adapters::Filesystem::YAMLLoader

            def load(scope)
              super
              [].tap do |list|
                each_file do |filepath, folder|
                  list << { source: filepath, folder: folder }
                end
              end
            end

            private

            def each_file(&block)
              # Follows symlinks and makes sure subdirectories are handled
              pattern = ['**', '*', '**', '*']

              Dir.glob(File.join(path, *pattern)).each do |file|
                next if exclude?(file)

                folder = File.dirname(file.gsub(File.join(path, ''), ''))

                yield(file, folder)
              end
            end

            def exclude?(file)
              File.directory?(file) ||
              file.starts_with?(File.join(path, 'samples')) ||
              File.basename(file).starts_with?('_')
            end

            def path
              @path ||= File.join(site_path, 'public')
            end

          end
        end
      end
    end
  end
end