Class: Gitload::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/gitload/config.rb

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



11
12
13
14
15
16
17
# File 'lib/gitload/config.rb', line 11

def initialize options = {}
  @root = options[:root]
  @cache = options.cache
  @clear_cache = options.clear_cache
  @dry_run = options.dry_run
  @clone_url_type = options[:clone_url_type]
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



6
7
8
# File 'lib/gitload/config.rb', line 6

def cache
  @cache
end

#clear_cacheObject

Returns the value of attribute clear_cache.



7
8
9
# File 'lib/gitload/config.rb', line 7

def clear_cache
  @clear_cache
end

#clone_url_typeObject

Returns the value of attribute clone_url_type.



9
10
11
# File 'lib/gitload/config.rb', line 9

def clone_url_type
  @clone_url_type
end

#dry_runObject

Returns the value of attribute dry_run.



8
9
10
# File 'lib/gitload/config.rb', line 8

def dry_run
  @dry_run
end

#rootObject

Returns the value of attribute root.



5
6
7
# File 'lib/gitload/config.rb', line 5

def root
  @root
end

Instance Method Details

#applyObject



19
20
21
22
# File 'lib/gitload/config.rb', line 19

def apply
  filename = File.expand_path '~/.gitload.rb'
  DSL.new(self).instance_eval File.read(filename), filename
end

#clone_optionsObject



24
25
26
27
28
29
# File 'lib/gitload/config.rb', line 24

def clone_options
  {
    dry_run: @dry_run,
    clone_url_type: @clone_url_type
  }
end

#delete(relative_path) ⇒ Object



60
61
62
# File 'lib/gitload/config.rb', line 60

def delete relative_path
  FileUtils.rm_f data_file(relative_path)
end

#load(relative_path) ⇒ Object



49
50
51
52
# File 'lib/gitload/config.rb', line 49

def load relative_path
  file = data_file relative_path
  File.exist?(file) ? JSON.parse(File.read(file)) : nil
end

#load_or_cache_data(relative_path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gitload/config.rb', line 31

def load_or_cache_data relative_path
  delete relative_path if @clear_cache

  if @cache
    data = self.load relative_path
  end

  unless data
    data = yield
  end

  if @cache
    save relative_path, data
  end

  data
end

#save(relative_path, contents) ⇒ Object



54
55
56
57
58
# File 'lib/gitload/config.rb', line 54

def save relative_path, contents
  file = data_file relative_path
  FileUtils.mkdir_p File.dirname(file)
  File.open(file, 'w'){ |f| f.write JSON.dump(contents) }
end