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.

Defined Under Namespace

Classes: FormatError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, options) ⇒ File

Returns a new instance of File.



17
18
19
20
21
22
# File 'lib/hsql/file.rb', line 17

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

#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(string, options) ⇒ Object

Given the contents of 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.



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

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

.parse_file(file, options) ⇒ Object



31
32
33
# File 'lib/hsql/file.rb', line 31

def self.parse_file(file, options)
  parse(file.read, options)
end

Instance Method Details

#metadataObject



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

def 
   ||= @front_matter ? ::YAML.load(@front_matter) : {}
end

#parse!Object



51
52
53
54
55
# File 'lib/hsql/file.rb', line 51

def parse!
  split!
  interpolate_data!
  self
end

#queriesObject



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

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

#to_jsonObject



39
40
41
# File 'lib/hsql/file.rb', line 39

def to_json
  .to_json
end

#to_yamlObject



35
36
37
# File 'lib/hsql/file.rb', line 35

def to_yaml
  .to_yaml
end