Class: ComplianceEngine::DataLoader::File

Inherits:
ComplianceEngine::DataLoader show all
Defined in:
lib/compliance_engine/data_loader/file.rb

Overview

Load compliance engine data from a file

Direct Known Subclasses

Json, Yaml

Instance Attribute Summary collapse

Attributes inherited from ComplianceEngine::DataLoader

#data

Instance Method Summary collapse

Constructor Details

#initialize(file, fileclass: ::File, key: file) ⇒ File

Initialize a new instance of the ComplianceEngine::DataLoader::File class

Parameters:

  • file (String)

    The path to the file to be loaded

  • fileclass (Class) (defaults to: ::File)

    The class to use for file operations, defaults to ‘::File`

  • key (String) (defaults to: file)

    The key to use for identifying the data, defaults to the file path



13
14
15
16
17
18
19
# File 'lib/compliance_engine/data_loader/file.rb', line 13

def initialize(file, fileclass: ::File, key: file)
  @fileclass = fileclass
  @filename = file
  @size = fileclass.size(file)
  @mtime = fileclass.mtime(file)
  super(parse(fileclass.read(file)), key: key)
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



34
35
36
# File 'lib/compliance_engine/data_loader/file.rb', line 34

def key
  @key
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



34
35
36
# File 'lib/compliance_engine/data_loader/file.rb', line 34

def mtime
  @mtime
end

#sizeObject (readonly)

Returns the value of attribute size.



34
35
36
# File 'lib/compliance_engine/data_loader/file.rb', line 34

def size
  @size
end

Instance Method Details

#refreshNilClass

Refresh the data from the file if it has changed

Returns:

  • (NilClass)


24
25
26
27
28
29
30
31
32
# File 'lib/compliance_engine/data_loader/file.rb', line 24

def refresh
  newsize = @fileclass.size(@filename)
  newmtime = @fileclass.mtime(@filename)
  return if newsize == @size && newmtime == @mtime

  @size = newsize
  @mtime = newmtime
  self.data = parse(@fileclass.read(@filename))
end