Class: SQLtorial::SqlToExample
- Inherits:
-
Object
- Object
- SQLtorial::SqlToExample
- Defined in:
- lib/sqltorial/sql_to_example.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
- #formatted ⇒ Object
- #formatted_lines ⇒ Object
- #formatter ⇒ Object
-
#initialize(file, db, number) ⇒ SqlToExample
constructor
A new instance of SqlToExample.
- #make_prose_directives_and_query(query) ⇒ Object
- #number ⇒ Object
- #queries ⇒ Object
- #title ⇒ Object
- #title_line ⇒ Object
- #to_str(options = {}) ⇒ Object
Constructor Details
#initialize(file, db, number) ⇒ SqlToExample
Returns a new instance of SqlToExample.
9 10 11 12 13 |
# File 'lib/sqltorial/sql_to_example.rb', line 9 def initialize(file, db, number) @file = file @db = db @number = number end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
8 9 10 |
# File 'lib/sqltorial/sql_to_example.rb', line 8 def file @file end |
Instance Method Details
#formatted ⇒ Object
15 16 17 |
# File 'lib/sqltorial/sql_to_example.rb', line 15 def formatted @formatted ||= formatter.format(file) end |
#formatted_lines ⇒ Object
23 24 25 26 27 28 |
# File 'lib/sqltorial/sql_to_example.rb', line 23 def formatted_lines if @formatted_lines.nil? @title_line, @formatted_lines = get_title_and_formatted_lines end @formatted_lines end |
#formatter ⇒ Object
19 20 21 |
# File 'lib/sqltorial/sql_to_example.rb', line 19 def formatter @formatter ||= Formatter.new end |
#make_prose_directives_and_query(query) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/sqltorial/sql_to_example.rb', line 45 def make_prose_directives_and_query(query) lines = query.dup prose_lines = [] lines.shift while lines.first.strip.empty? prose_lines << lines.shift.sub(WHITESPACE_REGEX, ' ').sub(/^\s*$/, "\n\n") while lines.first && (lines.first =~ WHITESPACE_REGEX || lines.first.empty?) directives, prose_lines = prose_lines.partition { |line| Directive.match(line) } [prose_lines.map(&:strip).join("\n"), process_directives(directives), lines.join("\n")] end |
#number ⇒ Object
54 55 56 |
# File 'lib/sqltorial/sql_to_example.rb', line 54 def number @number ||= file.basename.to_s.to_i end |
#queries ⇒ Object
30 31 32 |
# File 'lib/sqltorial/sql_to_example.rb', line 30 def queries @queries ||= formatted_lines.slice_after { |l| l =~ /;$/ } end |
#title ⇒ Object
41 42 43 |
# File 'lib/sqltorial/sql_to_example.rb', line 41 def title @title ||= title_line.gsub(WHITESPACE_REGEX, '') end |
#title_line ⇒ Object
34 35 36 37 38 39 |
# File 'lib/sqltorial/sql_to_example.rb', line 34 def title_line if @title_line.nil? @title_line, @formatted_lines = get_title_and_formatted_lines end @title_line end |
#to_str(options = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sqltorial/sql_to_example.rb', line 58 def to_str( = {}) = .merge(include_results: true) hash = {} queries.each_with_index do |query, index| prose, directives, sql = make_prose_directives_and_query(query) begin if is_create(sql) hash[sql] = [prose, create_to_md(, sql, directives)]; next elsif is_passthru(sql) execute(sql, ) hash[sql] = [prose, nil]; next end hash[sql] = [prose, query_to_md(, sql, directives)] rescue puts sql puts $!. puts $!.backtrace.join("\n") end end parts = [] parts << "## Example #{number}: #{title}\n" part_num = 0 parts += hash.map do |key, value| arr = [value.first] if key && !key.empty? part_num += 1 arr << "**Query #{number}.#{part_num}**" arr << "```sql\n#{key}\n```" end arr << value.last arr.join("\n\n") end parts.join("\n\n") + "\n\n" end |