Class: PomPomPom::Config

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

Constant Summary collapse

REPOSITORIES =
%w(http://repo1.maven.org/maven2)
TARGET_DIR =
'lib'
CACHE_DIR =
File.expand_path('~/.pompompom')
CONFIG_FILE =
File.expand_path('~/.pompompomrc')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



13
14
15
16
17
18
19
# File 'lib/pompompom/config.rb', line 13

def initialize(options={})
  @options_set  = options.keys
  @repositories = options[:repositories] || REPOSITORIES
  @target_dir   = options[:target_dir]   || TARGET_DIR
  @cache_dir    = options[:cache_dir]    || CACHE_DIR
  @config_file  = options[:config_file]  || CONFIG_FILE
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir.



11
12
13
# File 'lib/pompompom/config.rb', line 11

def cache_dir
  @cache_dir
end

#config_fileObject (readonly)

Returns the value of attribute config_file.



11
12
13
# File 'lib/pompompom/config.rb', line 11

def config_file
  @config_file
end

#repositoriesObject (readonly)

Returns the value of attribute repositories.



11
12
13
# File 'lib/pompompom/config.rb', line 11

def repositories
  @repositories
end

#target_dirObject (readonly)

Returns the value of attribute target_dir.



11
12
13
# File 'lib/pompompom/config.rb', line 11

def target_dir
  @target_dir
end

Instance Method Details

#create_file!Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/pompompom/config.rb', line 33

def create_file!
  return if file_exists?
  File.open(@config_file, 'w') do |f| 
    f.write(YAML.dump(
      'repositories' => @repositories,
      'cache_dir' => @cache_dir,
      'target_dir' => @target_dir
    ))
  end
end

#file_exists?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/pompompom/config.rb', line 29

def file_exists?
  File.exists?(@config_file)
end

#load!Object



21
22
23
24
25
26
27
# File 'lib/pompompom/config.rb', line 21

def load!
  return unless file_exists?
  options = symbolize_keys(YAML.load(File.read(@config_file)))
  @repositories = options[:repositories] || @repositories unless @options_set.include?(:repositories)
  @target_dir   = options[:target_dir]   || @target_dir   unless @options_set.include?(:target_dir)
  @cache_dir    = options[:cache_dir]    || @cache_dir    unless @options_set.include?(:cache_dir)
end

#symbolize_keys(h) ⇒ Object



44
45
46
47
48
49
# File 'lib/pompompom/config.rb', line 44

def symbolize_keys(h)
  h.keys.inject({}) do |acc, k|
    acc[k.to_sym] = if Hash === h[k] then symbolize_keys(h[k]) else h[k] end
    acc
  end
end