lib/lotus/config/assets.rb in lotusrb-0.4.1 vs lib/lotus/config/assets.rb in lotusrb-0.5.0

- old
+ new

@@ -5,27 +5,64 @@ # @since 0.1.0 # @api private class Assets < Utils::LoadPaths DEFAULT_DIRECTORY = 'public'.freeze + # Assets source (directory) + # + # @since 0.5.0 + # @api private + class Source + # @since 0.5.0 + # @api private + BLANK = ''.freeze + + # @since 0.5.0 + # @api private + URL_SEPARATOR = '/'.freeze + + # @since 0.5.0 + # @api private + attr_reader :urls + + # @since 0.5.0 + # @api private + attr_reader :root + + # @since 0.5.0 + # @api private + def initialize(path) + @path = path.to_s + @root = @path.sub("#{ Lotus.root }/", BLANK) + @urls = {} + + Dir.glob("#{ path }/**/*").each do |file| + next if ::File.directory?(file) + + @urls.store( + file.sub(@path, BLANK).sub(::File::SEPARATOR, URL_SEPARATOR), + file.sub("#{ @path }/", BLANK) + ) + end + + @urls.freeze + end + end + # @since 0.1.0 # @api private def initialize(root) - @root = root + @root = root @paths = Array(DEFAULT_DIRECTORY) end - # @since 0.1.0 + # @since 0.5.0 # @api private - def entries - hash = Hash.new { |k, v| k[v] = [] } + def for_each_source each do |path| - if path.exist? - hash[path.to_s] = path.children.map { |child| "/#{ child.basename }" } - end + yield Source.new(path) if path.exist? end - hash end # @since 0.2.0 # @api private def any? @@ -39,5 +76,6 @@ @root.join(path).realpath end end end end +