Sha256: fbe604c5a51c127dd8270c32324008e0dddd7c039139b0b78897f0c8c7ab7237
Contents?: true
Size: 1.98 KB
Versions: 1
Compression:
Stored size: 1.98 KB
Contents
require 'yaml' require 'json' require_relative 'query' require_relative 'data' require_relative 'template' module HSQL # HSQL::File parses the input file and provides reader methods to the hash of # YAML data from the front matter section and a list of the queries in the SQL # portion. class File attr_reader :string, :timestamp, :environment, :rendered_sql def initialize(string, options) @string = string @timestamp = options.fetch(:timestamp, Time.current) @environment = options[:environment] @verbose = options[:verbose] end # Given a SQL file with YAML front matter (see README for an # example) this will return a HSQL::File object providing access to # the parts of that file. def self.parse_file(filename, options) parse(::File.read(filename), options) end def self.parse(source, options) new(source, options).parse! end def to_yaml metadata.to_yaml end def to_json metadata.to_json end def metadata @metadata ||= begin hash = @front_matter ? ::YAML.load(@front_matter) : {} environments = hash.delete('environments') || {} hash.merge(environments[environment] || {}) end end def queries @queries ||= Query.parse(@rendered_sql) end def parse! split! interpolate_metadata! self end private # Insert the `environments:` data for the given environment into our # SQL queries. def template_data @data ||= metadata.merge(Data.for_machines(timestamp)) end def split! @split ||= begin top_half, divider, rest = string.partition(/^---$/) if divider.present? @front_matter = top_half @sql = rest else # No divider found, therefore no YAML header @sql = top_half end true end end def interpolate_metadata! @rendered_sql = Template.new(@sql, @verbose).render(template_data) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
hsql-0.4.0 | lib/hsql/file.rb |