Class: EachSQL
- Inherits:
-
Object
- Object
- EachSQL
- Includes:
- Enumerable
- Defined in:
- lib/each_sql/each_sql.rb,
lib/each_sql/parser.rb
Overview
Enumerable EachSQL object.
Defined Under Namespace
Modules: Parser
Instance Method Summary collapse
-
#<<(input) ⇒ Object
Appends the given String to the buffer.
-
#clear ⇒ EachSQL
Clears the buffer.
-
#delimiter ⇒ Object
@return Current delimiter.
- #delimiter=(delim) ⇒ Object
-
#each {|String| ... } ⇒ NilClass
Parses the buffer and enumerates through the executable blocks.
-
#empty? ⇒ Boolean
Return is the buffer is empty.
- #initialize(type, delimiter = ';') ⇒ EachSQL constructor
-
#shift ⇒ Hash
Parses current buffer and returns the result in Hash.
Constructor Details
#initialize(type, delimiter = ';') ⇒ EachSQL
9 10 11 12 13 14 15 |
# File 'lib/each_sql/each_sql.rb', line 9 def initialize type, delimiter = ';' @type = type @data = '' @sqls = [] self.delimiter = delimiter end |
Instance Method Details
#<<(input) ⇒ Object
Appends the given String to the buffer. @param input String to append
32 33 34 35 36 37 |
# File 'lib/each_sql/each_sql.rb', line 32 def << input if input @data << input.sub(/\A#{[65279].pack('U*')}/, '') # BOM (FIXME) end self end |
#clear ⇒ EachSQL
Clears the buffer
41 42 43 44 |
# File 'lib/each_sql/each_sql.rb', line 41 def clear @data = '' self end |
#delimiter ⇒ Object
@return Current delimiter.
26 27 28 |
# File 'lib/each_sql/each_sql.rb', line 26 def delimiter @delim end |
#delimiter=(delim) ⇒ Object
19 20 21 22 23 |
# File 'lib/each_sql/each_sql.rb', line 19 def delimiter= delim @delim = delim @parser = EachSQL::Parser.parser_for @type, delim self end |
#each {|String| ... } ⇒ NilClass
Parses the buffer and enumerates through the executable blocks.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/each_sql/each_sql.rb', line 70 def each return enum_for(:each) unless block_given? result = shift sqls = (result[:sqls] + result[:leftover]). map { |sql| strip_sql(sql) }. reject(&:empty?) sqls.each do |sql| yield sql end end |
#empty? ⇒ Boolean
Return is the buffer is empty
63 64 65 |
# File 'lib/each_sql/each_sql.rb', line 63 def empty? @data.gsub(/\s/, '').empty? end |
#shift ⇒ Hash
Parses current buffer and returns the result in Hash. :sqls is an Array of processed executable SQL blocks, :leftover is the unparsed trailing data
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/each_sql/each_sql.rb', line 50 def shift result = @parser.parse @data @data = result.captures[:leftover].join leftover = strip_sql(@data) { :sqls => result.captures[:execution_block].map { |b| strip_sql b }, :leftover => leftover.empty? ? nil : leftover } end |