Class: Boxing::Generator
- Inherits:
-
Object
- Object
- Boxing::Generator
- Includes:
- Utils
- Defined in:
- lib/boxing/generator.rb
Overview
Generate file from template
Constant Summary collapse
- ASCII_CLEAR =
"\e[0m"
- ASCII_BOLD =
"\e[1m"
- ASCII_RED =
"\e[31m"
- CREATED_MESSAGE =
"#{ASCII_BOLD}create#{ASCII_CLEAR}\t%s"
- IDENTICAL_MESSAGE =
"#{ASCII_BOLD}identical#{ASCII_CLEAR}\t%s"
- CONFLICT_MESSAGE =
"#{ASCII_BOLD}#{ASCII_RED}conflict#{ASCII_CLEAR}\t%s"
- OVERWRITE_MESSAGE =
'Overwrite %s? (enter "h" for help) [Ynqdhm] '
- FORCE_MESSAGE =
"#{ASCII_BOLD}force#{ASCII_CLEAR}\t%s"
- SKIP_MESSAGE =
"#{ASCII_BOLD}skip#{ASCII_CLEAR}\t%s"
- MERGE_TOOL_NOT_FOUND =
'Please configure merge.tool in your Git config.'
Instance Attribute Summary collapse
- #destination ⇒ Object readonly
- #path ⇒ Object readonly
Instance Method Summary collapse
-
#execute ⇒ Object
Generate file.
-
#exist? ⇒ TrueClass|FalseClass
Check if file exists.
-
#identical? ⇒ TrueClass|FalseClass
Check if file identical.
-
#initialize(destination, content) ⇒ Generator
constructor
A new instance of Generator.
-
#render ⇒ String
Render content.
Methods included from Utils
Constructor Details
#initialize(destination, content) ⇒ Generator
Returns a new instance of Generator.
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
#destination ⇒ Object (readonly)
22 23 24 |
# File 'lib/boxing/generator.rb', line 22 def destination @destination end |
#path ⇒ Object (readonly)
22 23 24 |
# File 'lib/boxing/generator.rb', line 22 def path @path end |
Instance Method Details
#execute ⇒ Object
Generate file
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
61 62 63 |
# File 'lib/boxing/generator.rb', line 61 def exist? path.exist? end |
#identical? ⇒ TrueClass|FalseClass
Check if file identical
70 71 72 |
# File 'lib/boxing/generator.rb', line 70 def identical? exist? && File.binread(path) == String.new(render).force_encoding('ASCII-8BIT') end |
#render ⇒ String
Render content
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 |