Module: DuckPuncher::JSONStorage

Defined in:
lib/duck_puncher/json_storage.rb

Class Method Summary collapse

Class Method Details

.dir_nameObject



5
6
7
# File 'lib/duck_puncher/json_storage.rb', line 5

def self.dir_name
  Pathname.new('.duck_puncher')
end

.read(file_name) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/duck_puncher/json_storage.rb', line 20

def self.read(file_name)
  if File.exists?(dir_name.join file_name)
    JSON.parse File.read(dir_name.join file_name), symbolize_names: true
  else
    {}
  end
end

.write(file_name, key, load_path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/duck_puncher/json_storage.rb', line 9

def self.write(file_name, key, load_path)
  FileUtils.mkdir(dir_name) unless File.exists?(dir_name)
  data = read(file_name)
  key = key.to_sym
  data[key] ||= {}
  data[key][:require_with] ||= key.to_s.tr('-', '/')
  data[key][:load_paths] ||= []
  data[key][:load_paths] << load_path.to_s unless data[key][:load_paths].include?(load_path.to_s)
  File.open(dir_name.join(file_name), 'wb') { |f| f << data.to_json }
end