Class: Jdt::Config

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

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Config

Returns a new instance of Config.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jdt/config.rb', line 7

def initialize(file)

  # ensure that folder exists
  if (not File.exists?(directory))
    FileUtils.makedirs(directory)
  end

  @file = file

  # ensure that file exist
  if (not File.exists?(yaml_file))
    File.open(yaml_file, "w") do |f|
      f << "\#YAML file for storing configuration information"
    end
  end
end

Instance Method Details

#directoryObject



24
25
26
# File 'lib/jdt/config.rb', line 24

def directory
  "#{ENV['HOME']}/.jdt"
end

#update {|props| ... } ⇒ Object

Yields:

  • (props)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jdt/config.rb', line 41

def update
  # read
  props = yaml_props

  # change
  yield props

  # write
  File.open(yaml_file, "w") do |f|
    f << props.to_yaml
  end
end

#yaml_fileObject



28
29
30
# File 'lib/jdt/config.rb', line 28

def yaml_file
  "#{directory}/#{@file}"
end

#yaml_propsObject



32
33
34
35
36
37
38
39
# File 'lib/jdt/config.rb', line 32

def yaml_props
  props = YAML.load_file(yaml_file)
  if (props)
    props
  else
    Hash.new
  end
end