Class: IPlayer::Preferences

Inherits:
Object
  • Object
show all
Defined in:
lib/iplayer/preferences.rb

Instance Method Summary collapse

Constructor Details

#initialize(env = ENV, platform = RUBY_PLATFORM) ⇒ Preferences

Returns a new instance of Preferences.



6
7
8
9
10
11
12
# File 'lib/iplayer/preferences.rb', line 6

def initialize(env=ENV, platform=RUBY_PLATFORM)
  @env      = env
  @platform = platform
  @hash     = {}
  @dirty    = []
  reload
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg, *params) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/iplayer/preferences.rb', line 36

def method_missing(msg, *params)
  key = msg.to_s
  if m = key.match(/^([^=]+)=$/)
    key = m[1]
    value = params.first
    unless @hash[key] == value
      @hash[key] = params.first
      @dirty << key
    end
  else
    @hash[key]
  end
end

Instance Method Details

#inspect(*args) ⇒ Object



50
51
52
# File 'lib/iplayer/preferences.rb', line 50

def inspect(*args)
  "#<IPlayer::Preferences #{ @hash.map{ |k,v| "#{k}=#{v.inspect}" }.join(', ') }>"
end

#reloadObject



23
24
25
26
27
# File 'lib/iplayer/preferences.rb', line 23

def reload
  reset_defaults
  return unless File.exist?(filename)
  @hash.merge!( YAML.load(File.read(filename)) || {} )
end

#reset_defaultsObject



14
15
16
17
18
19
20
21
# File 'lib/iplayer/preferences.rb', line 14

def reset_defaults
  @hash = {
    'type_preference' => %w[default signed],
    'download_path'   => Dir.pwd,
    'http_proxy'      => @env['http_proxy'],
    'subdirs'         => false
  }
end

#saveObject



29
30
31
32
33
34
# File 'lib/iplayer/preferences.rb', line 29

def save
  return if @dirty.empty?
  File.open(filename, 'w') do |io|
    io << YAML.dump(dirty_subset)
  end
end