class Schizm::Chunk

Structure to resolve chunks of source code.

Attributes

blocks[RW]
filename[R]
label[R]
share[RW]
target[RW]

Public Class Methods

each() { |chunk| ... } click to toggle source

Hash iteration interface.

# File lib/schizm/chunk.rb, line 83
def self.each
  @@hash.each do |filename, hash|
    hash.each do |label, chunk|
      yield chunk
    end
  end
end
hash() click to toggle source

Hash reader.

# File lib/schizm/chunk.rb, line 78
def self.hash
  @@hash
end
new(filename, label) click to toggle source
# File lib/schizm/chunk.rb, line 37
def initialize filename, label
  @filename = String.new filename
  @label = String.new label
  @share = Env.var[:share] if Env.var? :share
  @target = nil
  @blocks = Array.new
  @labels = Array.new
  @params = Hash.new
end

Public Instance Methods

find(label) click to toggle source

Find a chunk relative to self. That is, consider only chunks defined in the same file, plus chunks shared with the entire project.

# File lib/schizm/chunk.rb, line 102
def find label
  Chunk.each do |chunk|
    if chunk.label == label and
      (chunk.filename == @filename or chunk.share?)
      return chunk
    end
  end
  nil
end
id(block = @blocks.size) click to toggle source

Identifier for block.

# File lib/schizm/chunk.rb, line 152
def id block = @blocks.size
  String.new "#{@label.slugify}-#{block}"
end
push(block, label = nil) click to toggle source

Push a code block.

# File lib/schizm/chunk.rb, line 92
def push block, label = nil
  block = String.new block
  label = String.new label if label
  @blocks.push block
  @labels.push label
end
share?() click to toggle source

Boolean reader for +@share+.

# File lib/schizm/chunk.rb, line 54
def share?
  case
  when (@share == true) then return true
  when (@share != true) then return false
  end
end
target?() click to toggle source

Boolean reader for +@target+.

# File lib/schizm/chunk.rb, line 62
def target?
  return false unless @target.is_a? ::String
  return false unless @target != ""
  return true
end
to_s(*trace) click to toggle source

Resolve chunk references to generate the final source string.

# File lib/schizm/chunk.rb, line 113
def to_s *trace
  if trace.include? @label
    raise "Infinite recursion!"
  end
  total = String.new(@blocks.join "\n")
  total.trim!
  while true
    unless (total.sub! CHUNK_LABEL do
        if (chunk = find $1)
          replace = chunk.to_s *trace, @label
          replace.gsub /(?<=\n)/, " " * String.new($`).find_indent
        else
          "/* " + $1 + " ... */"
        end
        end)
      break
    end
  end
  if target?
    if Env.var? :guard and @target =~ HEADER_PATH
      guardify = String.new(@target).guardify
      if Env.var? :title
        guardify.prepend "_"
        guardify.prepend String.new(Env.var[:title]).guardify
      end
      total.prepend "\#define #{guardify}\n\n"
      total.prepend "\#ifndef #{guardify}\n"
      total.concat "\n\n"
      total.concat "\#endif /* \#ifndef #{guardify} */"
    end
    if File.file? "LICENSE"
      total.prepend "\n"
      total.prepend String.new(File.read("LICENSE")).comment
    end
  end
  total
end