Sha256: b59719a6d8883e23b9c8d45ede417ffefaee50aa2afcc761810249c48bd9743c

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

# 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.
require 'yaml'
require_relative 'query'
module HSQL
  class File < Struct.new(:string, :environment)
    def metadata
      @metadata ||= ::YAML.load(@front_matter)
    end

    def queries
      @queries ||= Query.parse(@rendered_sql)
    end

    def parse!
      split!
      interpolate_data!
      self
    end

    private

    def split!
      @split ||= begin
        @front_matter, divider, @sql = string.partition(/^---$/)
        unless divider == '---'
          fail FormatError, 'The YAML front matter is required, otherwise this is just a SQL file'
        end
        true
      end
    end

    def data
      @data ||= begin
        if metadata['data']
          unless metadata['data'].key?(environment)
            fail ArgumentError, "The environment #{environment.inspect} is not specified"
          end
          metadata['data'][environment]
        end || {}
      end
    end

    def interpolate_data!
      template = Template.new(@sql)
      template.variable_names.each do |name|
        unless data.key?(name)
          fail FormatError, "#{name.inspect} is not set in #{environment.inspect} environment"
        end
      end

      # Insert the `data:` section of YAML for the given environment into our SQL queries.
      @rendered_sql = template.render(data)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hsql-0.2.1 lib/hsql/file.rb
hsql-0.2.0 lib/hsql/file.rb