Class: AutoC::Module::State

Inherits:
Hash
  • Object
show all
Defined in:
lib/autoc/module.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m) ⇒ State

Returns a new instance of State.



109
110
111
112
# File 'lib/autoc/module.rb', line 109

def initialize(m)
  super
  @module = m
end

Instance Attribute Details

#moduleObject (readonly)

Returns the value of attribute module.



105
106
107
# File 'lib/autoc/module.rb', line 105

def module
  @module
end

Instance Method Details

#collectObject



114
115
116
117
118
# File 'lib/autoc/module.rb', line 114

def collect
  self[self.module.header.file_name] = self.module.header.digest
  self.module.sources.each { |source| self[source.file_name] = source.digest }
  self
end

#file_nameObject



107
# File 'lib/autoc/module.rb', line 107

def file_name = "#{self.module.name}.state"

#readObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/autoc/module.rb', line 120

def read
  if self.module.stateful? && File.exist?(file_name)
    # It's OK not to have this file but if it exists it must have proper contents
    io = File.open(file_name, 'rt', chomp: true)
    begin
      hash = {}
      io.readlines.each do |x|
        raise 'improper state file format' if (/\s*([^\s]+)\s+\*(.*)/ =~ x).nil?
        hash[$2] = $1
      end
      update(hash)
    ensure
      io.close
    end
  end
  self
end

#writeObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/autoc/module.rb', line 138

def write
  io = File.open(file_name, 'wt')
  begin
    begin
      each { |file_name, digest| io << "#{digest} *#{file_name}\n" }
    ensure
      io.close
    end
  rescue
    File.unlink(file_name) # Delete improperly rendered state file
    raise
  end
  self
end