class RedMatter::Asset

Asset node. Has multiple input files and one output file. @!attr [r] infiles

@return [Array<String,Hash>] All input files

@!attr [r] outfile

@return [String] Output file

@!attr [rw] name

@return [String] Asset's name

Attributes

infiles[R]
name[RW]
outfile[R]

Public Class Methods

new(&block) click to toggle source
# File lib/red_matter/asset.rb, line 13
def initialize(&block)
  @name    = ''
  @infiles = []
  @outfile = nil
  instance_exec(&block)
end

Public Instance Methods

input(input, &block) click to toggle source

Adds input file(s) to asset @param input [String,Array] @param block [Proc] If it is given, will be executed to get processed file content @return [RedMatter::Asset] self

# File lib/red_matter/asset.rb, line 24
def input(input, &block)
  if input.instance_of? String
    if block
      @infiles << {
        enable_cache: input
        block: block
      }
    else
      @infiles << File.expand_path(input)
    end
  elsif input.instance_of? Array
    input.each do |file|
      if block
        @infiles << {
          file:  File.expand_path(file),
          block: block
        }
      else
        @infiles << File.expand_path(file)
      end
    end
  else
    fail TypeError, "Input #{f.inspect} is not a string nor an array!"
  end
  self
end
output(file, &block) click to toggle source

Sets output file of current asset @param file [String] @param block [Proc] If given, packed result will be post-processed. @return [RedMatter::Asset] self

# File lib/red_matter/asset.rb, line 55
def output(file, &block)
  @outfile = File.expand_path(file)
  @block   = block
  self
end
process() click to toggle source

Processes all input files and writes result to output file (if any) @return [String] output

# File lib/red_matter/asset.rb, line 63
def process
  outs = []
  @infiles.each do |f|
    if f.instance_of? Hash
      dat = f[:block].(f[:file])
      if dat.instance_of? String
        out << dat + "\n\n"
      else
        fail TypeError, "#{dat.class} returned, String expected"
      end
    elsif f.instance_of? String
      outs << File.read(f)
    elsif f.instance_of? Array
      outs << f.map { |fn| File.read(fn) }
    else
      fail TypeError, "Infile #{f.inspect} is not a hash nor a string nor an array!"
    end
  end
  out = outs.join("\n\n")
  if @block
    out = @block.(out)
  end
  if @outfile
    File.delete(@outfile) if File.exist? @outfile
    File.open(@outfile, 'w') do |f|
      f.write(out)
    end
  end
  out
end