Class: ConceptQL::Knitter::ConceptQLChunk

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, cache, knitter) ⇒ ConceptQLChunk

Returns a new instance of ConceptQLChunk.



51
52
53
54
55
# File 'lib/conceptql/knitter.rb', line 51

def initialize(lines, cache, knitter)
  @cache = cache
  @lines = lines.to_a
  @knitter = knitter
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



50
51
52
# File 'lib/conceptql/knitter.rb', line 50

def cache
  @cache
end

#knitterObject (readonly)

Returns the value of attribute knitter.



50
51
52
# File 'lib/conceptql/knitter.rb', line 50

def knitter
  @knitter
end

#linesObject (readonly)

Returns the value of attribute lines.



50
51
52
# File 'lib/conceptql/knitter.rb', line 50

def lines
  @lines
end

Instance Method Details

#create_outputObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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_markupObject



108
109
110
111
112
# File 'lib/conceptql/knitter.rb', line 108

def diagram_markup
  diagram_path = diagram(statement)
  return "![#{title}](#{diagram_path})" if diagram_path
  nil
end

#make_statement_and_titleObject



69
70
71
72
73
74
75
76
77
78
79
# 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

#outputObject



57
58
59
60
61
62
# File 'lib/conceptql/knitter.rb', line 57

def output
  diagram_markup
  cache.fetch_or_create(lines.join) do
    create_output
  end
end

#query(stmt) ⇒ Object



163
164
165
# File 'lib/conceptql/knitter.rb', line 163

def query(stmt)
  knitter.db.query(stmt)
end

#resultify(results) ⇒ Object



134
135
136
137
138
139
140
141
142
# 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) ⇒ Object



144
145
146
# File 'lib/conceptql/knitter.rb', line 144

def rowify(columns)
  "| #{columns.join(" | ")} |"
end

#statementObject



81
82
83
84
# File 'lib/conceptql/knitter.rb', line 81

def statement
  @statement || make_statement_and_title
  @statement
end

#tableObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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

#titleObject



86
87
88
89
# File 'lib/conceptql/knitter.rb', line 86

def title
  @title || make_statement_and_title
  @title
end

#titleize(title) ⇒ Object



64
65
66
67
# File 'lib/conceptql/knitter.rb', line 64

def titleize(title)
  return '' unless title
  title.map(&:strip).join(" ").gsub('#', '')
end