Class: ConceptQL::Knitter
- Inherits:
-
Object
- Object
- ConceptQL::Knitter
- 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
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #diagram_dir ⇒ Object
- #diagram_path(stmt, &block) ⇒ Object
- #diagram_relative_path ⇒ Object
-
#initialize(db, file, options = {}) ⇒ Knitter
constructor
A new instance of Knitter.
- #knit ⇒ Object
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, = {}) @file = Pathname.new(file) raise "File must end in .md.cql!" unless file =~ /\.md\.cql$/ @db = db @options = .dup end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
8 9 10 |
# File 'lib/conceptql/knitter.rb', line 8 def db @db end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
8 9 10 |
# File 'lib/conceptql/knitter.rb', line 8 def file @file end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/conceptql/knitter.rb', line 8 def @options end |
Instance Method Details
#diagram_dir ⇒ Object
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_path ⇒ Object
36 37 38 |
# File 'lib/conceptql/knitter.rb', line 36 def diagram_relative_path @diagram_relative_path ||= diagram_dir.basename end |
#knit ⇒ Object
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 |