Class: Alister::Assets

Inherits:
Object
  • Object
show all
Defined in:
lib/assets.rb

Overview

An asset builder that helps with putting website asset files into the correct place

Examples:

Simple asset building

a = Alister::Assets.new
a.source "./assets/"
a.file "**/*"
a.build_to "./build/assets/"

Convert all png to webp

a = Alister::Assets.new
a.source "./assets/"
a.file "*.png"
a.enable_webp_conversion
a.build "./build/assets/gallery/"

Multiple sources and specific files

a = Alister::Assets.new
a.source "./"
a.files "robots.txt", "main.css"
a.build_and_reset "./build/assets/gallery/"
a.source "./assets/"
a.file "*.png"
a.enable_webp_conversion
a.build_and_reset "./build/assets/gallery/"

Constant Summary collapse

WEBP_EXTS =

All supported image filetype that can be converted to .webp

%w[.png .jpg .jpeg .tiff].freeze

Instance Method Summary collapse

Constructor Details

#initializeAssets

Returns a new instance of Assets.



31
32
33
34
35
36
37
38
# File 'lib/assets.rb', line 31

def initialize
  @source_path = nil
  @build_path = nil
  @webp_conversion = false
  @excludes = nil
  @files = nil
  @source_files = nil
end

Instance Method Details

#build_to(build_path) ⇒ void Also known as: build

This method returns an undefined value.

Builds the assets



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/assets.rb', line 74

def build_to(build_path)
  raise '[ERROR] Files are empty' if @files.nil?
  raise '[ERROR] Source is empty' if @source_path.nil?

  @build_path = build_path

  glob_source_files
  remove_excludes unless @excludes.nil?
  ensure_folders_are_built_first
  build_da_thing
end

#build_to_and_reset(build_path) ⇒ void Also known as: build_and_reset

This method returns an undefined value.

Shorthand for building and resetting at the same time



109
110
111
112
# File 'lib/assets.rb', line 109

def build_to_and_reset(build_path)
  build_to(build_path)
  reset
end

#enable_webp_conversionvoid

This method returns an undefined value.

Enables turning all suitable images into webp when building



42
43
44
# File 'lib/assets.rb', line 42

def enable_webp_conversion
  @webp_conversion = true
end

#excludes(*args) ⇒ void Also known as: exclude

This method returns an undefined value.

Exclude certain folders from being built



48
49
50
51
52
53
54
55
# File 'lib/assets.rb', line 48

def excludes(*args)
  if args.size == 1
    @excludes = args.first
    nil
  else
    @excludes = args
  end
end

#files(*args) ⇒ void Also known as: file

This method returns an undefined value.

Defines the files that will be included in the build



61
62
63
64
65
66
67
68
# File 'lib/assets.rb', line 61

def files(*args)
  if args.size == 1
    @files = args.first
    nil
  else
    @files = args
  end
end

#resetvoid

This method returns an undefined value.

Resets the builder for another building



98
99
100
101
102
103
104
105
# File 'lib/assets.rb', line 98

def reset
  @source_path = nil
  @build_path = nil
  @webp_conversion = false
  @excludes = nil
  @files = nil
  @source_files = nil
end

#source_from(source_path) ⇒ void Also known as: source

This method returns an undefined value.

Defines the source of the files



90
91
92
# File 'lib/assets.rb', line 90

def source_from(source_path)
  @source_path = source_path
end