Class: ConceptQL::Knitter

Inherits:
Object
  • Object
show all
Defined in:
lib/conceptql/knitter.rb

Defined Under Namespace

Classes: Cache, ConceptQLChunk

Constant Summary collapse

CONCEPTQL_CHUNK_START =
/```ConceptQL/
RESULT_KEYS =
%i(person_id criterion_id criterion_domain start_date end_date source_value)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, file, options = {}) ⇒ Knitter

Returns a new instance of Knitter.



12
13
14
15
16
17
# 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

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



8
9
10
# File 'lib/conceptql/knitter.rb', line 8

def db
  @db
end

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/conceptql/knitter.rb', line 8

def file
  @file
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/conceptql/knitter.rb', line 8

def options
  @options
end

Instance Method Details

#diagram_dirObject



32
33
34
# 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) ⇒ Object



40
41
42
43
44
45
46
# 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_pathObject



36
37
38
# File 'lib/conceptql/knitter.rb', line 36

def diagram_relative_path
  @diagram_relative_path ||= diagram_dir.basename
end

#knitObject



19
20
21
22
23
24
25
26
27
28
29
30
# 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