Class: Svgeez::Builder

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

Constant Summary collapse

SOURCE_IS_DESTINATION_MESSAGE =
"Setting `source` and `destination` to the same path isn't allowed!".freeze
SOURCE_DOES_NOT_EXIST =
'Provided `source` folder does not exist.'.freeze
NO_SVGS_IN_SOURCE_MESSAGE =
'No SVGs were found in `source` folder.'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



7
8
9
# File 'lib/svgeez/builder.rb', line 7

def destination
  @destination
end

#prefixObject (readonly)

Returns the value of attribute prefix.



7
8
9
# File 'lib/svgeez/builder.rb', line 7

def prefix
  @prefix
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/svgeez/builder.rb', line 7

def source
  @source
end

Instance Method Details

#buildObject

rubocop:disable Metrics/AbcSize



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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