class Yaasql::Reader

Constants

HEADER_PATTERN

Public Instance Methods

arguments(body) click to toggle source
# File lib/yaasql/reader.rb, line 25
def arguments(body)
  body.scan(/:(\w+);?/).flatten.map(&:to_sym)
end
components(query) click to toggle source
# File lib/yaasql/reader.rb, line 29
def components(query)
  name = name(query)
  body = raw_body(query)
  arguments = arguments(body)
  {name: name, body: with_sql_args(body, arguments), arguments: arguments}
end
from_file(path) click to toggle source
# File lib/yaasql/reader.rb, line 36
def from_file(path)
  query_strings = File.read(path).split("\n\n")
  query_strings.map { |q| Query.new(components(q)) }
end
name(query) click to toggle source
# File lib/yaasql/reader.rb, line 4
def name(query)
  match = query.match(HEADER_PATTERN)
  if match
    match[1].to_sym
  else
    raise ArgumentError.new("Must provide a header comment with query name")
  end
end
raw_body(query) click to toggle source
# File lib/yaasql/reader.rb, line 13
def raw_body(query)
  query.split("\n").map(&:strip).reject do |line|
    line.start_with?('--')
  end.join('\n')
end
with_sql_args(body, arguments) click to toggle source
# File lib/yaasql/reader.rb, line 19
def with_sql_args(body, arguments)
  arguments.each.with_index.reduce(body) do |body, (arg, index)|
    body.gsub(":#{arg}", "$#{index+1}")
  end
end