Class: GrubEditEnv::GrubEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/grub-editenv-ruby/grubenv.rb

Defined Under Namespace

Classes: FileNotFound

Constant Summary collapse

ENVBLK_SIGNATURE =
"# GRUB Environment Block\n"
ENVBLK_SIZE =
1024

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GrubEnv

Returns a new instance of GrubEnv.

Raises:



19
20
21
22
23
24
25
26
# File 'lib/grub-editenv-ruby/grubenv.rb', line 19

def initialize(options = {})
  @options = self.class.default_options.merge(options)
  
  raise "Misconfigured" unless grubenv_path
  raise FileNotFound, "File #{grubenv_path} was not found!" unless File.file?(grubenv_path)
  
  reload
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



17
18
19
# File 'lib/grub-editenv-ruby/grubenv.rb', line 17

def entries
  @entries
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/grub-editenv-ruby/grubenv.rb', line 16

def options
  @options
end

Class Method Details

.default_optionsObject



8
9
10
11
12
13
14
# File 'lib/grub-editenv-ruby/grubenv.rb', line 8

def self.default_options
  {
    :envblk_signature => ENVBLK_SIGNATURE,
    :envblk_size => ENVBLK_SIZE,
    :grubenv_path => nil
  }
end

Instance Method Details

#dumpObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/grub-editenv-ruby/grubenv.rb', line 46

def dump
  contents = options[:envblk_signature]
  
  @entries.each do |key, value|
    data = "#{key}=#{value}\n"
    contents << data
  end
  
  contents << "#" while contents.size < options[:envblk_size]

  File.open grubenv_path, "wb" do |f|
    f << contents
  end
end

#grubenv_pathObject



28
29
30
# File 'lib/grub-editenv-ruby/grubenv.rb', line 28

def grubenv_path
  options[:grubenv_path]
end

#reloadObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grub-editenv-ruby/grubenv.rb', line 32

def reload
  @entries = nil
  data = File.read(grubenv_path)
  raise "Wrong grubenv format on reload!" unless data.start_with?(options[:envblk_signature])
  data[0, options[:envblk_signature].size] = ''
  data.sub!(/#*\z/, "")
  
  @entries = {}
  data.split("\n").each do |line|
    key, value = line.split("=")
    @entries[key] = value
  end
end