Class: Locomotive::Wagon::ThemeAssetDecorator

Inherits:
SimpleDelegator
  • Object
show all
Includes:
ToHashConcern
Defined in:
lib/locomotive/wagon/decorators/theme_asset_decorator.rb

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from ToHashConcern

#prepare_value_for_hash, #to_hash

Instance Method Details

#__attributes__Object



19
20
21
# File 'lib/locomotive/wagon/decorators/theme_asset_decorator.rb', line 19

def __attributes__
  %i(source folder checksum)
end

#checksumObject



35
36
37
# File 'lib/locomotive/wagon/decorators/theme_asset_decorator.rb', line 35

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

#filepathObject



57
58
59
# File 'lib/locomotive/wagon/decorators/theme_asset_decorator.rb', line 57

def filepath
  __getobj__.source
end

#filepath=(path) ⇒ Object



61
62
63
# File 'lib/locomotive/wagon/decorators/theme_asset_decorator.rb', line 61

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

#priorityObject



27
28
29
# File 'lib/locomotive/wagon/decorators/theme_asset_decorator.rb', line 27

def priority
  stylesheet_or_javascript? ? 100 : 0
end

#realnameObject

  • 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)



41
42
43
44
45
46
47
# 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_urlObject



49
50
51
# File 'lib/locomotive/wagon/decorators/theme_asset_decorator.rb', line 49

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

#short_relative_urlObject



53
54
55
# File 'lib/locomotive/wagon/decorators/theme_asset_decorator.rb', line 53

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

#sourceObject



23
24
25
# 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?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/locomotive/wagon/decorators/theme_asset_decorator.rb', line 31

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