Class: HSQL::File

Inherits:
Object
  • Object
show all
Defined in:
lib/hsql/file.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, options) ⇒ File

Returns a new instance of File.



13
14
15
16
17
18
# File 'lib/hsql/file.rb', line 13

def initialize(string, options)
  @string = string
  @timestamp = options.fetch(:timestamp, Time.current)
  @environment = options[:environment]
  @verbose = options[:verbose]
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



11
12
13
# File 'lib/hsql/file.rb', line 11

def environment
  @environment
end

#rendered_sqlObject (readonly)

Returns the value of attribute rendered_sql.



11
12
13
# File 'lib/hsql/file.rb', line 11

def rendered_sql
  @rendered_sql
end

#stringObject (readonly)

Returns the value of attribute string.



11
12
13
# File 'lib/hsql/file.rb', line 11

def string
  @string
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



11
12
13
# File 'lib/hsql/file.rb', line 11

def timestamp
  @timestamp
end

Class Method Details

.parse(source, options) ⇒ Object



27
28
29
# File 'lib/hsql/file.rb', line 27

def self.parse(source, options)
  new(source, options).parse!
end

.parse_file(filename, options) ⇒ Object

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.



23
24
25
# File 'lib/hsql/file.rb', line 23

def self.parse_file(filename, options)
  parse(::File.read(filename), options)
end

Instance Method Details

#metadataObject



35
36
37
38
39
40
41
# File 'lib/hsql/file.rb', line 35

def 
  @metadata ||= begin
    hash = @front_matter ? ::YAML.load(@front_matter) : {}
    environments = hash.delete('environments') || {}
    hash.merge(environments[environment] || {})
  end
end

#parse!Object



47
48
49
50
51
# File 'lib/hsql/file.rb', line 47

def parse!
  split!
  interpolate_metadata!
  self
end

#queriesObject



43
44
45
# File 'lib/hsql/file.rb', line 43

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