Class: Pirka::Config

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

Constant Summary collapse

FILE_NAME =
"pirka.yaml"
XDG_CONFIG_HOME =
Pathname.new(".config")
CONFIG_FILE =
XDG_CONFIG_HOME/FILE_NAME
XDG_CONFIG_DIRS =
[Pathname.new("/etc/xdg")]
CONFIG_DIRS =
XDG_CONFIG_DIRS.collect {|dir| dir/FILE_NAME}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

TODO:

Consider login user

Returns a new instance of Config.



65
66
67
68
69
# File 'lib/pirka/config.rb', line 65

def initialize
  @data_home = nil
  @additional_directories = []
  @library_repositories = []
end

Class Attribute Details

.additional_directoriesObject

Returns the value of attribute additional_directories.



17
18
19
# File 'lib/pirka/config.rb', line 17

def additional_directories
  @additional_directories
end

.config_homeObject

Returns the value of attribute config_home.



17
18
19
# File 'lib/pirka/config.rb', line 17

def config_home
  @config_home
end

Instance Attribute Details

#additional_directoriesObject

Returns the value of attribute additional_directories.



62
63
64
# File 'lib/pirka/config.rb', line 62

def additional_directories
  @additional_directories
end

#data_homeObject

Returns the value of attribute data_home.



62
63
64
# File 'lib/pirka/config.rb', line 62

def data_home
  @data_home
end

#library_repositoriesObject

Returns the value of attribute library_repositories.



62
63
64
# File 'lib/pirka/config.rb', line 62

def library_repositories
  @library_repositories
end

Class Method Details

.config_directory(user = nil) ⇒ Object



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

def config_directory(user = nil)
  directories.first
end

.directories(user = nil) ⇒ Object



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

def directories(user = nil)
  config_dirs = ENV["XDG_CONFIG_DIRS"] ?
                  ENX["XDG_CONFIG_DIR"].split(":").collect {|dir| Pathname.new(dir)} :
                  CONFIG_DIRS
  config_home = ENV["XDG_CONFIG_HOME"] ?
                  Pathname.new(ENV["XDG_CONFIG_HOME"]) :
                  Pathname.new(Dir.home(user))/XDG_CONFIG_HOME
  ([@config_home, config_home] + @additional_directories + config_dirs).compact
end

.filepathObject



57
58
59
# File 'lib/pirka/config.rb', line 57

def filepath
  config_directory/FILE_NAME
end

.load_file(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pirka/config.rb', line 33

def load_file(path)
  load_hash(
    YAML.load_file(path).each_with_object({}) {|(key, value), h|
      h[key] = case key
               when "data_home"
                 Pathname(value)
               when "additional_directories"
                 value.collect {|val| Pathname(val)}
               when "library_repositories"
                 value.collect {|val| URI(val)}
               else
                 value
               end
    })
end

.load_hash(h) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/pirka/config.rb', line 49

def load_hash(h)
  config = new
  %w[data_home additional_directories library_repositories].each do |attr|
    config.__send__("#{attr}=", h[attr]) if h[attr]
  end
  config
end

Instance Method Details

#config_directory(user = nil) ⇒ Object



81
82
83
# File 'lib/pirka/config.rb', line 81

def config_directory(user = nil)
  self.class.config_directory(user)
end

#dirname_from_repository(repository_uri) ⇒ Object



77
78
79
# File 'lib/pirka/config.rb', line 77

def dirname_from_repository(repository_uri)
  repository_uri.host/repository_uri.path[1..-1].sub_ext("")
end

#filepathObject



85
86
87
# File 'lib/pirka/config.rb', line 85

def filepath
  self.class.filepath
end

#path_from_repository(repository_uri) ⇒ Object



72
73
74
# File 'lib/pirka/config.rb', line 72

def path_from_repository(repository_uri)
  Library.data_directory/dirname_from_repository(repository_uri)
end

#save(path = nil) ⇒ Object



89
90
91
92
93
94
# File 'lib/pirka/config.rb', line 89

def save(path = nil)
  path = Pathname(path || filepath)
  path.dirname.mkpath unless path.dirname.directory?
  path.write to_yaml
  path
end

#to_hObject



96
97
98
99
100
101
102
103
104
# File 'lib/pirka/config.rb', line 96

def to_h
  h = {}
  h["data_home"] = @data_home.to_path if @data_home
  %w[additional_directories library_repositories].each do |key|
    value = __send__(key)
    h[key] = value.collect(&:to_s) unless value.empty?
  end
  h
end

#to_yamlObject



106
107
108
# File 'lib/pirka/config.rb', line 106

def to_yaml
  to_h.to_yaml
end