lib/makit/data.rb in makit-0.0.56 vs lib/makit/data.rb in makit-0.0.57

- old
+ new

@@ -1,50 +1,50 @@ -# frozen_string_literal: true -require "digest" -# This module provides classes for the Makit gem. -module Makit - # This class provide methods for managing persistent data for the makit gem - class Data - attr_accessor :directory, :use_jsonks - - def initialize(attributes = {}) - @directory = attributes[:directory] || File.join(Dir.home, ".makit") - @use_json = false - attributes.each do |key, value| - instance_variable_set("@#{key}", value) if self.class.method_defined?("#{key}=") - end - end - - # remove all data - def clear - # remove the data directory - FileUtils.rm_rf(@directory) if Dir.exist?(@directory) - end - - def get_content_id(item) - # get the content id for the item - bytes_string = item.to_proto.bytes.pack("C*") # Convert the byte array to a string - Digest::SHA256.hexdigest(bytes_string) - end - - # save the content - # data.save_content(git_version) - def save(item) - content_id = get_content_id(item) - # save the content - if @use_json - # save the content to a json file - File.open(File.join(@directory, "#{content_id}.json"), "w") do |file| - file.write(item.to_json) - end - end - end - - def get_content(content_id) - nil - end - - def self.gem_db_filename - gem_data_directory = File.join(Dir.home, ".makit", "makit.db") - end - end -end +# frozen_string_literal: true +require "digest" +# This module provides classes for the Makit gem. +module Makit + # This class provide methods for managing persistent data for the makit gem + class Data + attr_accessor :directory, :use_jsonks + + def initialize(attributes = {}) + @directory = attributes[:directory] || File.join(Dir.home, ".makit") + @use_json = false + attributes.each do |key, value| + instance_variable_set("@#{key}", value) if self.class.method_defined?("#{key}=") + end + end + + # remove all data + def clear + # remove the data directory + FileUtils.rm_rf(@directory) if Dir.exist?(@directory) + end + + def get_content_id(item) + # get the content id for the item + bytes_string = item.to_proto.bytes.pack("C*") # Convert the byte array to a string + Digest::SHA256.hexdigest(bytes_string) + end + + # save the content + # data.save_content(git_version) + def save(item) + content_id = get_content_id(item) + # save the content + if @use_json + # save the content to a json file + File.open(File.join(@directory, "#{content_id}.json"), "w") do |file| + file.write(item.to_json) + end + end + end + + def get_content(content_id) + nil + end + + def self.gem_db_filename + gem_data_directory = File.join(Dir.home, ".makit", "makit.db") + end + end +end