class Svgeez::Builder

Constants

NO_SVGS_IN_SOURCE_MESSAGE
SOURCE_DOES_NOT_EXIST
SOURCE_IS_DESTINATION_MESSAGE

Attributes

destination[R]
prefix[R]
source[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/svgeez/builder.rb, line 9
def initialize(options = {})
  @source = Source.new(options)
  @destination = Destination.new(options)
  @svgo = options.fetch('svgo', false)
  @prefix = options.fetch('prefix', @destination.file_id)

  raise SOURCE_IS_DESTINATION_MESSAGE if source_is_destination?
  raise SOURCE_DOES_NOT_EXIST unless source_exists?
rescue RuntimeError => exception
  logger.error exception.message
  exit
end

Public Instance Methods

build() click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/svgeez/builder.rb, line 23
def build
  raise NO_SVGS_IN_SOURCE_MESSAGE if source_is_empty?

  logger.info "Generating sprite at `#{destination_file_path}` from #{source_files_count} SVG#{'s' if source_files_count > 1}..."

  # Make destination folder
  FileUtils.mkdir_p(destination.folder_path)

  # Write the file
  File.open(destination_file_path, 'w') do |file|
    file.write destination_file_contents
  end

  logger.info "Successfully generated sprite at `#{destination_file_path}`."
rescue RuntimeError => exception
  logger.warn exception.message
end

Private Instance Methods

destination_file_contents() click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/svgeez/builder.rb, line 44
def destination_file_contents
  file_contents = Elements::SvgElement.new(source, destination, prefix).build
  file_contents = Optimizer.new.optimize(file_contents) if @svgo

  file_contents.insert(4, ' style="display: none;"')
end
destination_file_path() click to toggle source
# File lib/svgeez/builder.rb, line 51
def destination_file_path
  @destination_file_path ||= destination.file_path
end
logger() click to toggle source
# File lib/svgeez/builder.rb, line 55
def logger
  @logger ||= Svgeez.logger
end
source_exists?() click to toggle source
# File lib/svgeez/builder.rb, line 59
def source_exists?
  File.directory?(source.folder_path)
end
source_files_count() click to toggle source
# File lib/svgeez/builder.rb, line 63
def source_files_count
  source.file_paths.length
end
source_is_destination?() click to toggle source
# File lib/svgeez/builder.rb, line 67
def source_is_destination?
  /\A#{source.folder_path}/ =~ destination.folder_path
end
source_is_empty?() click to toggle source
# File lib/svgeez/builder.rb, line 71
def source_is_empty?
  source.file_paths.empty?
end