class ConceptQL::Knitter

Constants

CONCEPTQL_CHUNK_START
RESULT_KEYS

Attributes

file[RW]

Public Class Methods

new(db, file, options = {}) click to toggle source
# File lib/conceptql/knitter.rb, line 12
def initialize(db, file, options = {})
  @file = Pathname.new(file)
  raise "File must end in .md.cql!" unless file =~ /\.md\.cql$/
  @db = db
  @options = options.dup
end

Public Instance Methods

diagram_dir() click to toggle source
# File lib/conceptql/knitter.rb, line 32
def diagram_dir
  @diagram_dir ||= (dir + file.basename('.md.cql')).tap { |d| d.rmtree if d.exist? ; d.mkpath }
end
diagram_path(stmt, &block) click to toggle source
# File lib/conceptql/knitter.rb, line 40
def diagram_path(stmt, &block)
  png_contents = cache.fetch_or_create(stmt.inspect, &block)
  file_name = (cache.hash_it(stmt) + ".png")
  new_path = (diagram_dir + file_name)
  new_path.write(png_contents)
  diagram_relative_path + file_name
end
diagram_relative_path() click to toggle source
# File lib/conceptql/knitter.rb, line 36
def diagram_relative_path
  @diagram_relative_path ||= diagram_dir.basename
end
knit() click to toggle source
# File lib/conceptql/knitter.rb, line 19
def knit
  lines = file.readlines
  chunks = lines.slice_before { |l| l =~ CONCEPTQL_CHUNK_START }.to_a
  outputs = []
  outputs << chunks.shift unless chunks.first =~ CONCEPTQL_CHUNK_START
  outputs += chunks.map do |chunk|
    cql, *remainder = chunk.slice_after { |l| l =~ /^```\n$/ }.to_a
    cql = ConceptQLChunk.new(cql, cache, self)
    [cql.output, remainder].flatten
  end.flatten
  File.write(file.to_s.sub(/.cql$/, ''), outputs.join)
end

Private Instance Methods

cache() click to toggle source
# File lib/conceptql/knitter.rb, line 217
def cache
  @cache ||= Cache.new(db.db, file, options[:cache_options])
end
dir() click to toggle source
# File lib/conceptql/knitter.rb, line 213
def dir
  file.dirname
end