Class: Bade::Precompiled

Inherits:
Object show all
Defined in:
lib/bade/precompiled.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, source_file_path = nil) ⇒ Precompiled

Returns a new instance of Precompiled.

Parameters:



43
44
45
46
# File 'lib/bade/precompiled.rb', line 43

def initialize(code, source_file_path = nil)
  @code_string = code
  @source_file_path = source_file_path
end

Instance Attribute Details

#code_stringString

Returns:



9
10
11
# File 'lib/bade/precompiled.rb', line 9

def code_string
  @code_string
end

#source_file_pathString

Returns:



13
14
15
# File 'lib/bade/precompiled.rb', line 13

def source_file_path
  @source_file_path
end

Class Method Details

.from_yaml_file(file) ⇒ Object

Parameters:

  • file (String, File)

    file instance or path to file

Raises:

  • (LoadError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bade/precompiled.rb', line 21

def self.from_yaml_file(file)
  file = if file.is_a?(String)
           File.new(file, 'r')
         else
           file
         end

  hash = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.0')
           Psych.safe_load(file, filename: file.path, permitted_classes: [Symbol])
         else
           Psych.safe_load(file, [Symbol])
         end
  raise LoadError, 'YAML file is not in valid format' unless hash.is_a?(Hash)

  file_path = hash[:source_file_path]
  content = hash[:code_string]

  new(content, file_path)
end

Instance Method Details

#write_yaml_to_file(file) ⇒ Object

Parameters:

  • file (String, File)

    file instance or path to file



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bade/precompiled.rb', line 50

def write_yaml_to_file(file)
  file = if file.is_a?(String)
           File.new(file, 'w')
         else
           file
         end

  content = {
    source_file_path: source_file_path,
    code_string: code_string,
  }.to_yaml

  file.write(content)
  file.flush
end