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(include_results = true) ⇒ Object
Constructor Details
#initialize(file, db, number) ⇒ SqlToExample
Returns a new instance of SqlToExample.
8 9 10 11 12 |
# File 'lib/sqltorial/sql_to_example.rb', line 8 def initialize(file, db, number) @file = file @db = db @number = number end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
7 8 9 |
# File 'lib/sqltorial/sql_to_example.rb', line 7 def file @file end |
Instance Method Details
#formatted ⇒ Object
14 15 16 |
# File 'lib/sqltorial/sql_to_example.rb', line 14 def formatted @formatted ||= formatter.format(file) end |
#formatted_lines ⇒ Object
22 23 24 25 26 27 |
# File 'lib/sqltorial/sql_to_example.rb', line 22 def formatted_lines if @formatted_lines.nil? @title_line, @formatted_lines = get_title_and_formatted_lines end @formatted_lines end |
#formatter ⇒ Object
18 19 20 |
# File 'lib/sqltorial/sql_to_example.rb', line 18 def formatter @formatter ||= Formatter.new end |
#make_prose_directives_and_query(query) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/sqltorial/sql_to_example.rb', line 44 def make_prose_directives_and_query(query) lines = query.dup prose_lines = [] lines.shift while lines.first.strip.empty? prose_lines << lines.shift.sub(/^\s*-+\s*/, ' ').chomp.sub(/^ $/, "\n\n") while lines.first =~ /^\s*(-+|$)/ directives, prose_lines = prose_lines.partition { |line| Directive.match(line) } [prose_lines.join(''), process_directives(directives), lines.join("\n")] end |
#number ⇒ Object
53 54 55 |
# File 'lib/sqltorial/sql_to_example.rb', line 53 def number @number ||= file.basename.to_s.to_i end |
#queries ⇒ Object
29 30 31 |
# File 'lib/sqltorial/sql_to_example.rb', line 29 def queries @queries ||= formatted_lines.slice_after { |l| l =~ /;$/ } end |
#title ⇒ Object
40 41 42 |
# File 'lib/sqltorial/sql_to_example.rb', line 40 def title @title ||= title_line.gsub(/^\s*-+\s*/, '') end |
#title_line ⇒ Object
33 34 35 36 37 38 |
# File 'lib/sqltorial/sql_to_example.rb', line 33 def title_line if @title_line.nil? @title_line, @formatted_lines = get_title_and_formatted_lines end @title_line end |
#to_str(include_results = true) ⇒ Object
57 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 57 def to_str(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) execute(sql, include_results) hash[sql] = [prose, create_to_md(include_results, sql, directives)]; next elsif is_drop(sql) execute(sql, include_results) hash[sql] = [prose, nil]; next end hash[sql] = [prose, query_to_md(include_results, sql, directives)] rescue puts sql puts $!. puts $!.backtrace.join("\n") $stdin.gets 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" end |