class Locomotive::Wagon::ThemeAssetDecorator

Constants

EXTENSIONS_TABLE

Public Instance Methods

__attributes__() click to toggle source
# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 19
def __attributes__
  %i(source folder checksum)
end
checksum() click to toggle source
# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 35
def checksum
  Digest::MD5.hexdigest(_readfile(filepath) { |io| io.read })
end
filepath() click to toggle source
# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 57
def filepath
  __getobj__.source
end
filepath=(path) click to toggle source
# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 61
def filepath=(path)
  __getobj__[:source] = path
end
priority() click to toggle source
# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 27
def priority
  stylesheet_or_javascript? ? 100 : 0
end
realname() click to toggle source
  • 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)

# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 41
def realname
  return @realname if @realname

  filename = File.basename(filepath)

  @realname = _realname(filename, 2) || _realname(filename, 1) || filename
end
relative_url() click to toggle source
# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 49
def relative_url
  "#{folder.gsub('\\', '/')}/#{realname}"
end
short_relative_url() click to toggle source
# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 53
def short_relative_url
  relative_url[/^(javascripts|stylesheets|fonts)\/(.*)$/, 2]
end
source() click to toggle source
# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 23
def source
  Locomotive::Coal::UploadIO.new(_readfile(filepath), nil, realname)
end
stylesheet_or_javascript?() click to toggle source
# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 31
def stylesheet_or_javascript?
  File.extname(realname) =~ /^\.(css|scss|less|js|coffee)$/
end

Private Instance Methods

_readfile(path, &block) click to toggle source
# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 75
def _readfile(path, &block)
  File.open(path, 'rb', &block)
end
_realname(filename, length) click to toggle source
# File lib/locomotive/wagon/decorators/theme_asset_decorator.rb, line 67
def _realname(filename, length)
  extension = '.' + filename.split('.').last(length).join('.')

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