Class: Wlog::SysConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/wlog/domain/sys_config.rb

Overview

Author:

  • Simon Symeonidis

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ SysConfig

Returns a new instance of SysConfig.



11
12
13
14
# File 'lib/wlog/domain/sys_config.rb', line 11

def initialize(db)
  @db = db
  @key_value = KeyValue.new(@db)
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



62
63
64
# File 'lib/wlog/domain/sys_config.rb', line 62

def db
  @db
end

#key_valueObject (readonly)

Key value domain object / helper



98
99
100
# File 'lib/wlog/domain/sys_config.rb', line 98

def key_value
  @key_value
end

Class Method Details

.ansi!Object

SET THE SETTINGS TO ANSI!



37
38
39
# File 'lib/wlog/domain/sys_config.rb', line 37

def self.ansi!
  self.store_config('ansi', 'yes')
end

.ansi?Boolean

Are the settings set to ansi?

Returns:

  • (Boolean)


27
28
29
# File 'lib/wlog/domain/sys_config.rb', line 27

def self.ansi?
  self.read_attributes['ansi'] == 'yes'
end

.get_config(term) ⇒ Object

Get a term from the configuration file. Return nil if not found



49
50
51
# File 'lib/wlog/domain/sys_config.rb', line 49

def self.get_config(term)
  self.read_attributes[term]
end

.not_ansi!Object

Oh no! The settings are not ansi!



32
33
34
# File 'lib/wlog/domain/sys_config.rb', line 32

def self.not_ansi!
  self.store_config('ansi', 'no')
end

.read_attributesObject

Load a hash from a text file.

See Also:

  • selfself.write_attributes


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wlog/domain/sys_config.rb', line 81

def self.read_attributes
  include StaticConfigurations
  FileUtils.touch ConfigFile
  lines = File.open(ConfigFile, 'r').read.split(/#{$/}/)
  terms = lines.map{|e| e.split(':')}
  values = Hash.new(nil)
  terms.each do |term_tuple| # [term, value]
    values[term_tuple[0]] = term_tuple[1]
  end
  values 
rescue Errno::ENOENT
  $stderr.puts "#{self.class.name}: Problem opening file #{ConfigFile}"
  # Minimum guarantee: disable ansi colors 
{'ansi' => 'no'}
end

.store_config(term, value) ⇒ Object

Store a term into the configuration file



42
43
44
45
46
# File 'lib/wlog/domain/sys_config.rb', line 42

def self.store_config(term,value)
  values = self.read_attributes
  values[term] = value
  self.write_attributes(values)
end

.string_decoratorObject

Get the string decorator.



54
55
56
57
58
59
60
# File 'lib/wlog/domain/sys_config.rb', line 54

def self.string_decorator
  if self.ansi?
    WlogString
  else
    UncoloredString
  end
end

.write_attributes(terms) ⇒ Object

terms is a hash -> => :b, :c => :d write each key value to a file like this:

a:b
c:d
...


69
70
71
72
73
74
75
76
77
# File 'lib/wlog/domain/sys_config.rb', line 69

def self.write_attributes(terms)
  include StaticConfigurations
  str = terms.inject(""){|str,e| str += "#{e[0]}:#{e[1]}#{$/}"}
  fh = File.open(ConfigFile, 'w')
  fh.write(str)
  fh.close
rescue Errno::ENOENT
  $stderr.puts "#{self.class.name}: Problem opening file #{ConfigFile}"
end

Instance Method Details

#last_focusObject

load the last focused issue



17
18
19
# File 'lib/wlog/domain/sys_config.rb', line 17

def last_focus
  @key_value.get('last_focus')
end

#last_focus=(issue) ⇒ Object

store the last focused issue



22
23
24
# File 'lib/wlog/domain/sys_config.rb', line 22

def last_focus=(issue)
  @key_value.put!('last_focus', "#{issue}")
end