Class: Makit::Data
- Inherits:
-
Object
- Object
- Makit::Data
- Defined in:
- lib/makit/data.rb
Overview
This class provide methods for managing persistent data for the makit gem
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#use_jsonks ⇒ Object
Returns the value of attribute use_jsonks.
Class Method Summary collapse
Instance Method Summary collapse
-
#clear ⇒ Object
remove all data.
- #get_content(_content_id) ⇒ Object
- #get_content_id(item) ⇒ Object
-
#initialize(attributes = {}) ⇒ Data
constructor
A new instance of Data.
-
#save(item) ⇒ Object
save the content data.save_content(git_version).
Constructor Details
#initialize(attributes = {}) ⇒ Data
Returns a new instance of Data.
10 11 12 13 14 15 16 |
# File 'lib/makit/data.rb', line 10 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 |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
8 9 10 |
# File 'lib/makit/data.rb', line 8 def directory @directory end |
#use_jsonks ⇒ Object
Returns the value of attribute use_jsonks.
8 9 10 |
# File 'lib/makit/data.rb', line 8 def use_jsonks @use_jsonks end |
Class Method Details
.gem_db_filename ⇒ Object
45 46 47 |
# File 'lib/makit/data.rb', line 45 def self.gem_db_filename File.join(Dir.home, ".makit", "makit.db") end |
Instance Method Details
#clear ⇒ Object
remove all data
19 20 21 22 |
# File 'lib/makit/data.rb', line 19 def clear # remove the data directory FileUtils.rm_rf(@directory) end |
#get_content(_content_id) ⇒ Object
41 42 43 |
# File 'lib/makit/data.rb', line 41 def get_content(_content_id) nil end |
#get_content_id(item) ⇒ Object
24 25 26 27 28 |
# File 'lib/makit/data.rb', line 24 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(item) ⇒ Object
save the content data.save_content(git_version)
32 33 34 35 36 37 38 39 |
# File 'lib/makit/data.rb', line 32 def save(item) content_id = get_content_id(item) # save the content return unless @use_json # save the content to a json file File.write(File.join(@directory, "#{content_id}.json"), item.to_json) end |