Class: Downloads::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/downloads/configuration.rb

Constant Summary collapse

DIRECTORY =
Pathname.new(File.join(ENV['HOME'], '.downloads'))
DEFAULTS =
{ 'remote_host' => 'downloads', 'remote_directory' => 'downloads', 'local_directory' => File.join(ENV['HOME'], 'Desktop') }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



11
12
13
14
15
16
17
18
19
# File 'lib/downloads/configuration.rb', line 11

def initialize
  DIRECTORY.mkdir unless DIRECTORY.directory?

  @values = DEFAULTS.dup
  @values.merge!(YAML.load_file(path('config'))) if path('config').file?

  @local_server  = build_local_server
  @remote_server = build_remote_server
end

Instance Attribute Details

#local_serverObject (readonly)

Returns the value of attribute local_server.



9
10
11
# File 'lib/downloads/configuration.rb', line 9

def local_server
  @local_server
end

#remote_serverObject (readonly)

Returns the value of attribute remote_server.



9
10
11
# File 'lib/downloads/configuration.rb', line 9

def remote_server
  @remote_server
end

Instance Method Details

#[](key) ⇒ Object



21
22
23
# File 'lib/downloads/configuration.rb', line 21

def [](key)
  @values[key.to_s]
end

#[]=(key, value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/downloads/configuration.rb', line 25

def []=(key, value)
  @values[key.to_s] = value
  File.open(path('config'), 'w') { |file| file.puts(to_yaml) }

  case key.to_s
  when /^remote/
    File.delete(path('remote_cache')) if path('remote_cache').file?
    @remote_server = build_remote_server
  when /^local/
    @local_server = build_local_server
  end
end

#pid_fileObject



38
39
40
# File 'lib/downloads/configuration.rb', line 38

def pid_file
  path('pid')
end

#to_yamlObject



42
43
44
45
# File 'lib/downloads/configuration.rb', line 42

def to_yaml
  # Marginally nicer than @values.to_yaml, as it avoids the leading '---'
  @values.map { |key, value| "#{key}: #{value}" }.join("\n")
end