Module: StandardPath

Defined in:
lib/standard_path.rb

Overview


File : standard_path.rb Authors : Aoran Zeng <[email protected]> Created on : <2023-05-19> Last modified : <2023-05-29>

stardard_path:

Standard Path for corss-platform gems

Defined Under Namespace

Classes: NotSupportedOS, UnknownOS

Constant Summary collapse

VERSION =
"0.1.1"
OSes =
['Windows', 'macOS', 'Linux']

Class Method Summary collapse

Class Method Details

.app_cache(app, os = self.os) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/standard_path.rb', line 157

def app_cache(app, os = self.os)
  base = case os
  when 'Windows'
    File.join "~/AppData/Local", app, 'cache'
  when 'macOS'
    File.join "~/Library/Caches", app
  when 'Linux'
    File.join '~/.cache', app
  else
    raise NotSupportedOS, "#{self.name} doesn't know about #{__method__} path of '#{os}' you specify"
  end
  File.expand_path base
end

.app_config(app, os = self.os) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/standard_path.rb', line 142

def app_config(app, os = self.os)
  base = case os
  when 'Windows'
    File.join "~/AppData/Local", app
  when 'macOS'
    File.join "~/Library/Preferences", app
  when 'Linux'
    File.join '~/.config', app
  else
    raise NotSupportedOS, "#{self.name} doesn't know about #{__method__} path of '#{os}' you specify"
  end
  File.expand_path base
end

.app_data(app, os = self.os) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/standard_path.rb', line 112

def app_data(app, os = self.os)
  base = case os
  when 'Windows'
    File.join "~/AppData/Roaming", app
  when 'macOS'
    File.join "~/Library/Application Support", app
  when 'Linux'
    File.join "~/.local/share", app
  else
    raise NotSupportedOS, "#{self.name} doesn't know about #{__method__} path of '#{os}' you specify"
  end
  File.expand_path base
end

.app_local_data(app, os = self.os) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/standard_path.rb', line 127

def app_local_data(app, os = self.os)
  base = case os
  when 'Windows'
    File.join "~/AppData/Local", app
  when 'macOS'
    File.join "~/Library/Application Support", app
  when 'Linux'
    File.join "~/.local/share", app
  else
    raise NotSupportedOS, "#{self.name} doesn't know about #{__method__} path of '#{os}' you specify"
  end
  File.expand_path base
end

.app_temp(app, os = self.os) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/standard_path.rb', line 172

def app_temp(app, os = self.os)
  base = case os
  when 'Windows'
    File.join "~/AppData/Local/Temp", app
  when 'macOS'
    # NOTE: I'm not sure if this dir is the most proper for macOS
    #       Please affirm or fix it if you are sure.
    File.join "/tmp", app
  when 'Linux'
    File.join '/tmp', app
  else
    raise NotSupportedOS, "#{self.name} doesn't know about #{__method__} path of '#{os}' you specify"
  end
  File.expand_path base
end

.desktop(os = self.os) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/standard_path.rb', line 47

def desktop(os = self.os)
  case os
  when 'Windows', 'macOS', 'Linux'
    File.expand_path "~/Desktop"
  else
    raise NotSupportedOS, "#{name} doesn't know about #{__method__} path of '#{os}' you specify"
  end
end

.documents(os = self.os) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/standard_path.rb', line 57

def documents(os = self.os)
  case os
  when 'Windows', 'macOS', 'Linux'
    File.expand_path "~/Documents"
  else
    raise NotSupportedOS, "#{name} doesn't know about #{__method__} path of '#{os}' you specify"
  end
end

.downloads(os = self.os) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/standard_path.rb', line 67

def downloads(os = self.os)
  case os
  when 'Windows', 'macOS', 'Linux'
    File.expand_path "~/Downloads"
  else
    raise NotSupportedOS, "#{name} doesn't know about #{__method__} path of '#{os}' you specify"
  end
end

.movies(os = self.os) ⇒ Object Also known as: videos



97
98
99
100
101
102
103
104
105
106
# File 'lib/standard_path.rb', line 97

def movies(os = self.os)
  case os
  when 'Windows', 'Linux'
    File.expand_path '~/Videos'
  when 'macOS'
    File.expand_path '~/Movies'
  else
    raise NotSupportedOS, "#{name} doesn't know about #{__method__} path of '#{os}' you specify"
  end
end

.music(os = self.os) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/standard_path.rb', line 87

def music(os = self.os)
  case os
  when 'Windows', 'macOS', 'Linux'
    File.expand_path '~/Music'
  else
    raise NotSupportedOS, "#{name} doesn't know about #{__method__} path of '#{os}' you specify"
  end
end

.osObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/standard_path.rb', line 26

def os
  host_os = RbConfig::CONFIG['host_os']
  if Gem.win_platform?
    'Windows'
  elsif host_os =~ /darwin/
    'macOS'
  elsif host_os =~ /mac/
    # This condition is just for possible future compatibility only
    'macOS'
  elsif host_os =~ /linux/
    'Linux'
  elsif host_os =~ /freebsd/
    'FreeBSD'
    raise NotSupportedOS, "Currently #{name} doesn't support FreeBSD."
  else
    'Unknown'
    raise UnknownOS, "#{name} doesn't know about your OS type."
  end
end

.pictures(os = self.os) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/standard_path.rb', line 77

def pictures(os = self.os)
  case os
  when 'Windows', 'macOS', 'Linux'
    File.join Dir.home, 'Pictures'
  else
    raise NotSupportedOS, "#{name} doesn't know about #{__method__} path of '#{os}' you specify"
  end
end