Sha256: 8a3b73fd4c9ac2966c7b8e98ddb9cd50c45642561002b0b57ffcac1956b0202a

Contents?: true

Size: 1.88 KB

Versions: 12

Compression:

Stored size: 1.88 KB

Contents

module Locomotive
  module Wagon

    class ThemeAssetDecorator < SimpleDelegator

      include ToHashConcern

      EXTENSIONS_TABLE = {
        '.scss'       => '.css',
        '.css.scss'   => '.css',
        '.sass'       => '.css',
        '.css.sass'   => '.css',
        '.less'       => '.css',
        '.css.less'   => '.css',
        '.coffee'     => '.js',
        '.js.coffee'  => '.js'
      }.freeze

      def __attributes__
        %i(source folder checksum)
      end

      def source
        Locomotive::Coal::UploadIO.new(_readfile(filepath), nil, realname)
      end

      def priority
        stylesheet_or_javascript? ? 100 : 0
      end

      def stylesheet_or_javascript?
        File.extname(realname) =~ /^\.(css|scss|less|js|coffee)$/
      end

      def checksum
        Digest::MD5.hexdigest(_readfile(filepath) { |io| io.read })
      end

      # - memoize it because it will not change even if we change the filepath (or source)
      # - we keep the first extension and drop the others (.coffee, .scss, ...etc)
      def realname
        return @realname if @realname

        filename = File.basename(filepath)

        @realname = _realname(filename, 2) || _realname(filename, 1) || filename
      end

      def relative_url
        "#{folder.gsub('\\', '/')}/#{realname}"
      end

      def short_relative_url
        relative_url[/^(javascripts|stylesheets|fonts)\/(.*)$/, 2]
      end

      def filepath
        __getobj__.source
      end

      def filepath=(path)
        __getobj__[:source] = path
      end

      private

      def _realname(filename, length)
        extension = '.' + filename.split('.').last(length).join('.')

        if new_extension = EXTENSIONS_TABLE[extension]
          File.basename(filename, extension) + new_extension
        end
      end

      def _readfile(path, &block)
        File.open(path, 'rb', &block)
      end

    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
locomotivecms_wagon-3.2.0.alpha2 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-3.2.0.alpha1 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-3.1.1 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-3.1.0 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-3.1.0.beta1 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-3.0.5 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-3.0.4 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-3.0.3 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-3.0.2 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-3.0.0 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-3.0.0.rc0 lib/locomotive/wagon/decorators/theme_asset_decorator.rb
locomotivecms_wagon-3.0.0.beta2 lib/locomotive/wagon/decorators/theme_asset_decorator.rb