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:



39
40
41
42
# File 'lib/bade/precompiled.rb', line 39

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
# File 'lib/bade/precompiled.rb', line 21

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

  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



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bade/precompiled.rb', line 46

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

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

  file.write(content)
  file.flush
end