Class: Spandx::Core::DataFile
- Inherits:
-
Object
- Object
- Spandx::Core::DataFile
- Includes:
- Enumerable
- Defined in:
- lib/spandx/core/data_file.rb
Instance Attribute Summary collapse
-
#absolute_path ⇒ Object
readonly
Returns the value of attribute absolute_path.
Instance Method Summary collapse
- #each ⇒ Object
- #exist? ⇒ Boolean
- #index ⇒ Object
-
#initialize(absolute_path) ⇒ DataFile
constructor
A new instance of DataFile.
- #insert(name, version, licenses) ⇒ Object
- #open_file(mode: 'rb') ⇒ Object
- #search(name:, version:) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(absolute_path) ⇒ DataFile
Returns a new instance of DataFile.
10 11 12 13 |
# File 'lib/spandx/core/data_file.rb', line 10 def initialize(absolute_path) @absolute_path = Pathname.new(absolute_path) FileUtils.mkdir_p(@absolute_path.dirname) end |
Instance Attribute Details
#absolute_path ⇒ Object (readonly)
Returns the value of attribute absolute_path.
8 9 10 |
# File 'lib/spandx/core/data_file.rb', line 8 def absolute_path @absolute_path end |
Instance Method Details
#each ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/spandx/core/data_file.rb', line 15 def each return unless exist? open_file(mode: 'rb') do |io| while (line = io.gets) yield CsvParser.parse(line) end end end |
#exist? ⇒ Boolean
44 45 46 |
# File 'lib/spandx/core/data_file.rb', line 44 def exist? absolute_path.exist? end |
#index ⇒ Object
55 56 57 |
# File 'lib/spandx/core/data_file.rb', line 55 def index @index ||= IndexFile.new(self) end |
#insert(name, version, licenses) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/spandx/core/data_file.rb', line 36 def insert(name, version, licenses) return if [name, version].any? { |x| x.nil? || x.empty? } open_file(mode: 'a') do |io| io.write(to_csv([name, version, licenses.join('-|-')])) end end |
#open_file(mode: 'rb') ⇒ Object
48 49 50 51 52 53 |
# File 'lib/spandx/core/data_file.rb', line 48 def open_file(mode: 'rb') absolute_path.open(mode) { |io| yield io } rescue Errno::ENOENT => error Spandx.logger.error(error) nil end |
#search(name:, version:) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/spandx/core/data_file.rb', line 25 def search(name:, version:) return if name.nil? || name.empty? return if version.nil? || name.empty? return unless absolute_path.exist? term = "#{name}-#{version}" index.search do |row| term <=> "#{row[0]}-#{row[1]}" end end |
#to_s ⇒ Object
59 60 61 |
# File 'lib/spandx/core/data_file.rb', line 59 def to_s absolute_path.to_s end |