class Cardio::Mod::ModuleTemplate

ModuleTemplate is an abstract class to build ruby modules out of deckos dsl for sets and set patterns. {Loader::SetTemplate} and {Loader::SetPatternTemplate} inherit from it and adapt the template to their needs.

Public Class Methods

new(modules, content_path, strategy) click to toggle source
# File lib/cardio/mod/module_template.rb, line 8
def initialize modules, content_path, strategy
  modules = Array.wrap modules
  @pattern = modules.shift
  @modules = modules
  @content = ::File.read content_path
  @content_path = content_path
  @strategy = strategy
end

Public Instance Methods

build() click to toggle source

Evaluates the module in the top level namespace.

# File lib/cardio/mod/module_template.rb, line 18
def build
  eval to_s, TOPLEVEL_BINDING, @content_path, offset
end
processed_content() click to toggle source
# File lib/cardio/mod/module_template.rb, line 27
def processed_content
  capture_module_comment if @strategy.clean_comments?
  module_content
end
simple_load?() click to toggle source

Just run the code of the source. Don’t use the path to the file as module hierarchy.

# File lib/cardio/mod/module_template.rb, line 34
def simple_load?
  @content =~ /\A#!\s?simple load/
end
to_s() click to toggle source

@return [String] the ruby code to build the modal

# File lib/cardio/mod/module_template.rb, line 23
def to_s
  simple_load? ? @content : processed_content
end

Private Instance Methods

capture_module_comment() click to toggle source

find all comment lines at the beginning of a mod file, up to the first non-comment line. (These will be inserted before the module declaration, so that Yard will interpret them as a module comment.)

# File lib/cardio/mod/module_template.rb, line 43
def capture_module_comment
  content_lines = @content.split "\n"
  comment_lines = []

  content_lines.each do |line|
    comment?(line) ? comment_lines << content_lines.shift : break
  end

  @content = content_lines.join "\n"
  @module_comment = comment_lines.join "\n"
end
comment?(line) click to toggle source
# File lib/cardio/mod/module_template.rb, line 55
def comment? line
  line.match?(/^ *\#/)
end
module_comment() click to toggle source
# File lib/cardio/mod/module_template.rb, line 64
def module_comment
  return "" unless @strategy.clean_comments?

  @module_comment = nil if @module_comment.blank?
  [auto_comment, @module_comment].compact.join "\n"
end
module_content() click to toggle source
# File lib/cardio/mod/module_template.rb, line 71
      def module_content
        # for unknown reasons strip_heredoc doesn't work properly
        # and with trailing whitespace code like `=begin` fails
        <<~RUBY.strip_heredoc
          # -*- encoding : utf-8 -*-
          #{preamble}
          #{@content}
          #{postamble}
          # ~~ generated from #{@content_path} ~~
        RUBY
      end
preamble() click to toggle source

loader template must implement preamble_bits

# File lib/cardio/mod/module_template.rb, line 60
def preamble
  preamble_bits.join "\n"
end