class ConceptQL::Knitter::ConceptQLChunk
Attributes
lines[RW]
Public Class Methods
new(lines, cache, knitter)
click to toggle source
# File lib/conceptql/knitter.rb, line 51 def initialize(lines, cache, knitter) @cache = cache @lines = lines.to_a @knitter = knitter end
Public Instance Methods
create_output()
click to toggle source
# File lib/conceptql/knitter.rb, line 91 def create_output output = [] output << title unless title.empty? output << '' unless title.empty? output << "```JSON" output << '' output << statement.to_json output << '' output << "```" output << '' output << diagram_markup output << '' output << table output << '' output.compact.join("\n") end
diagram(stmt)
click to toggle source
# File lib/conceptql/knitter.rb, line 148 def diagram(stmt) knitter.diagram_path(stmt) do |path_name| annotated = nil begin annotated = query(stmt).annotate rescue puts $!.message puts $!.backtrace.join("\n") p stmt annotated = FakeAnnotater.new(stmt).annotate end ConceptQL::AnnotateGrapher.new.graph_it(annotated, path_name, output_type: 'png') end end
diagram_markup()
click to toggle source
# File lib/conceptql/knitter.rb, line 108 def diagram_markup diagram_path = diagram(statement) return "" if diagram_path nil end
make_statement_and_title()
click to toggle source
# File lib/conceptql/knitter.rb, line 69 def make_statement_and_title lines.shift lines.pop title, statement = lines.slice_before { |l| l =~ /^\s*#/ }.to_a if statement.nil? statement = title title = nil end @statement = eval(statement.join) @title = titleize(title) end
output()
click to toggle source
# File lib/conceptql/knitter.rb, line 57 def output diagram_markup cache.fetch_or_create(lines.join) do create_output end end
query(stmt)
click to toggle source
# File lib/conceptql/knitter.rb, line 163 def query(stmt) knitter.db.query(stmt) end
resultify(results)
click to toggle source
# File lib/conceptql/knitter.rb, line 134 def resultify(results) rows = [] rows << rowify(RESULT_KEYS) rows << rowify(RESULT_KEYS.map { |c| c.to_s.gsub(/./, '-')}) results.each do |result| rows << rowify(result.values_at(*RESULT_KEYS)) end rows.join("\n") end
rowify(columns)
click to toggle source
# File lib/conceptql/knitter.rb, line 144 def rowify(columns) "| #{columns.join(" | ")} |" end
statement()
click to toggle source
# File lib/conceptql/knitter.rb, line 81 def statement @statement || make_statement_and_title @statement end
table()
click to toggle source
# File lib/conceptql/knitter.rb, line 114 def table results = nil begin results = query(statement).query.limit(10).all rescue puts $!.message puts $!.backtrace.join("\n") end if results.nil? "```No Results. Statement is experimental.```" else if results.empty? "```No Results found.```" else resultify(results) end end end
title()
click to toggle source
# File lib/conceptql/knitter.rb, line 86 def title @title || make_statement_and_title @title end
titleize(title)
click to toggle source
# File lib/conceptql/knitter.rb, line 64 def titleize(title) return '' unless title title.map(&:strip).join(" ").gsub('#', '') end