class RgGen::Core::Utility::CodeUtility::SourceFile

Attributes

file_path[R]

Public Class Methods

new(file_path) { |self| ... } click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 17
def initialize(file_path)
  @file_path = file_path
  block_given? && yield(self)
end

Public Instance Methods

body(&block) click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 46
def body(&block)
  @bodies ||= []
  @bodies << block
end
header(&block) click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 24
def header(&block)
  @file_header = -> { block.call(@file_path) }
end
include_file(file) click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 42
def include_file(file)
  include_files([file])
end
include_files(files) click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 37
def include_files(files)
  @include_files ||= []
  @include_files.concat(Array(files))
end
include_guard() { |default_guard_macro| ... } click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 28
def include_guard
  @guard_macro =
    if block_given?
      yield(default_guard_macro)
    else
      default_guard_macro
    end
end
to_code() click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 51
def to_code
  CodeBlock.new do |code|
    code_blocks.each { |block| execute_code_block(code, block, true) }
  end
end
to_s() click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 57
def to_s
  to_code.to_s
end

Private Instance Methods

code_blocks() click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 63
def code_blocks
  [
    @file_header,
    include_guard_header,
    include_file_block,
    *Array(@bodies),
    include_guard_footer
  ].compact
end
default_guard_macro() click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 78
def default_guard_macro
  File.basename(file_path).upcase.gsub(/\W/, '_')
end
execute_code_block(code, code_block, insert_newline) click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 73
def execute_code_block(code, code_block, insert_newline)
  code.eval_block(&code_block)
  code << nl if insert_newline && !code.last_line_empty?
end
include_file_block() click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 89
def include_file_block
  @include_files && lambda do
    keyword = self.class.include_keyword
    @include_files
      .flat_map { |file| [keyword, space, string(file), nl] }
  end
end
include_guard_header() click to toggle source
# File lib/rggen/core/utility/code_utility/source_file.rb, line 82
def include_guard_header
  @guard_macro && lambda do
    [self.class.ifndef_keyword, self.class.define_keyword]
      .flat_map { |keyword| [keyword, space, @guard_macro, nl] }
  end
end