Class: PgVerify::Interpret::PgScript

Inherits:
Object
  • Object
show all
Defined in:
lib/pg-verify/interpret/pg_script.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePgScript

Returns a new instance of PgScript.



13
14
15
# File 'lib/pg-verify/interpret/pg_script.rb', line 13

def initialize()
    @models = []
end

Instance Attribute Details

#modelsObject

List of models which are defined in this script



8
9
10
# File 'lib/pg-verify/interpret/pg_script.rb', line 8

def models
  @models
end

#script_fileObject

The full path to the source script file



10
11
12
# File 'lib/pg-verify/interpret/pg_script.rb', line 10

def script_file
  @script_file
end

Instance Method Details

#find_source_location(trace = caller()) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/pg-verify/interpret/pg_script.rb', line 58

def find_source_location(trace = caller())
    line_number = trace
        .select { |l| l.include?(@script_file) }
        .map { |l| l.split(":in")[0] }.reject(&:blank?)
        .map { |l| l.split(":")[-1] }.reject(&:blank?)
        .uniq.first.to_i
    Model::SourceLocation.new(@script_file, line_number)
end

#get_bindingObject



67
68
69
# File 'lib/pg-verify/interpret/pg_script.rb', line 67

def get_binding()
    binding()
end

#interpret(file, validate: true) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pg-verify/interpret/pg_script.rb', line 17

def interpret(file, validate: true)
    raise "Not a file path string: '#{file}'::#{file.class}" unless file.is_a?(String)
    file = File.expand_path(file)
    raise NoSuchScriptError.new(file) unless File.file?(file)
    @script_file ||= file
    
    begin
        Dir.chdir(File.dirname(file)) { eval(File.read(file), get_binding(), file) }
    rescue Exception => e
        re_raise_exception(file, self, e)
    end

    @models.each { |model| Model::Validation.validate!(model) } if validate
    
    return @models
end

#model(name, &blk) ⇒ Object

Raises:



34
35
36
37
38
39
# File 'lib/pg-verify/interpret/pg_script.rb', line 34

def model(name, &blk)
    raise InvalidDSL_model.new("Name '#{name}' is neither a symbol nor string") unless name.is_a?(Symbol) || name.is_a?(String)
    graph_ctx = Interpret::GraphContext.new(name.to_sym, self)
    graph_ctx.instance_eval(&blk)
    @models << graph_ctx.to_model()
end