Class: Fontcustom::Manifest

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/fontcustom/manifest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#behavior, #destination_root, #line_break, #methodize_hash, #options, #project_root, #say_changed, #say_message, #say_status, #shell, shell, #source_paths, #symbolize_hash, #write_file

Constructor Details

#initialize(manifest, cli_options = {}) ⇒ Manifest

Returns a new instance of Manifest.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fontcustom/manifest.rb', line 7

def initialize(manifest, cli_options = {})
  @manifest = manifest
  @cli_options = symbolize_hash cli_options
  if File.exists? @manifest
    reload
    if ! @cli_options.empty? && get(:options) != @cli_options
      set :options, @cli_options
    end
  else
    create_manifest @cli_options
  end
end

Instance Attribute Details

#manifestObject (readonly)

Returns the value of attribute manifest.



5
6
7
# File 'lib/fontcustom/manifest.rb', line 5

def manifest
  @manifest
end

Instance Method Details

#delete(key) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fontcustom/manifest.rb', line 47

def delete(key)
  files = get(key)
  return if files.empty?
  begin
    deleted = []
    files.each do |file|
      remove_file file, :verbose => false
      deleted << file
    end
  ensure
    set key, files - deleted
    say_changed :delete, deleted
  end
end

#get(key) ⇒ Object

TODO convert paths to absolute



21
22
23
# File 'lib/fontcustom/manifest.rb', line 21

def get(key)
  @data[key]
end

#reloadObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/fontcustom/manifest.rb', line 36

def reload
  begin
    json = File.read @manifest
    @data = JSON.parse json, :symbolize_names => true
  rescue JSON::ParserError
    raise Fontcustom::Error,
      "Couldn't parse `#{@manifest}`. Fix any invalid "\
      "JSON or delete the file to start from scratch."
  end
end

#set(key, value, status = nil) ⇒ Object

TODO convert paths to relative



26
27
28
29
30
31
32
33
34
# File 'lib/fontcustom/manifest.rb', line 26

def set(key, value, status = nil)
  if key == :all
    @data = value
  else
    @data[key] = value
  end
  json = JSON.pretty_generate @data
  write_file @manifest, json, status
end