Class: Boxing::Generator

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/boxing/generator.rb

Overview

Generate file from template

Since:

  • 0.11.0

Constant Summary collapse

ASCII_CLEAR =

Since:

  • 0.11.0

"\e[0m"
ASCII_BOLD =

Since:

  • 0.11.0

"\e[1m"
ASCII_RED =

Since:

  • 0.11.0

"\e[31m"
CREATED_MESSAGE =

Since:

  • 0.11.0

"#{ASCII_BOLD}create#{ASCII_CLEAR}\t%s"
IDENTICAL_MESSAGE =

Since:

  • 0.11.0

"#{ASCII_BOLD}identical#{ASCII_CLEAR}\t%s"
CONFLICT_MESSAGE =

Since:

  • 0.11.0

"#{ASCII_BOLD}#{ASCII_RED}conflict#{ASCII_CLEAR}\t%s"
OVERWRITE_MESSAGE =

Since:

  • 0.11.0

'Overwrite %s? (enter "h" for help) [Ynqdhm] '
FORCE_MESSAGE =

Since:

  • 0.11.0

"#{ASCII_BOLD}force#{ASCII_CLEAR}\t%s"
SKIP_MESSAGE =

Since:

  • 0.11.0

"#{ASCII_BOLD}skip#{ASCII_CLEAR}\t%s"
MERGE_TOOL_NOT_FOUND =

Since:

  • 0.11.0

'Please configure merge.tool in your Git config.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#current_path, #template

Constructor Details

#initialize(destination, content) ⇒ Generator

Returns a new instance of Generator.

Since:

  • 0.11.0



27
28
29
30
31
# File 'lib/boxing/generator.rb', line 27

def initialize(destination, content)
  @destination = destination
  @path = current_path.join(@destination)
  @content = content
end

Instance Attribute Details

#destinationObject (readonly)

Since:

  • 0.11.0



22
23
24
# File 'lib/boxing/generator.rb', line 22

def destination
  @destination
end

#pathObject (readonly)

Since:

  • 0.11.0



22
23
24
# File 'lib/boxing/generator.rb', line 22

def path
  @path
end

Instance Method Details

#executeObject

Generate file

Since:

  • 0.11.0



36
37
38
39
40
41
# File 'lib/boxing/generator.rb', line 36

def execute
  with_conflict do
    FileUtils.mkdir_p(File.dirname(path))
    File.write(path, render)
  end
end

#exist?TrueClass|FalseClass

Check if file exists

Returns:

  • (TrueClass|FalseClass)

Since:

  • 0.11.0



61
62
63
# File 'lib/boxing/generator.rb', line 61

def exist?
  path.exist?
end

#identical?TrueClass|FalseClass

Check if file identical

Returns:

  • (TrueClass|FalseClass)

Since:

  • 0.11.0



70
71
72
# File 'lib/boxing/generator.rb', line 70

def identical?
  exist? && File.binread(path) == String.new(render).force_encoding('ASCII-8BIT')
end

#renderString

Render content

Returns:

  • (String)

Since:

  • 0.11.0



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

def render
  @render ||= if @content.is_a?(Proc)
                @content.call
              else
                @content
              end
end