Class: UserSpace
- Inherits:
-
Object
- Object
- UserSpace
- Defined in:
- lib/user_space.rb
Overview
‘ruby`
Constant Summary collapse
- VERSION =
'5.2.230101'
- XDG =
{ 'cache' => ENV['XDG_CACHE_HOME'] || File.('~/.cache'), 'config' => ENV['XDG_CONFIG_HOME'] || File.('~/.config'), 'data' => ENV['XDG_DATA_HOME'] || File.('~/.local/share'), }
- APPDIR =
lambda{ File.dirname(File.(_1)).sub(%r([/\\]lib$),'')}
Instance Attribute Summary collapse
-
#appdir ⇒ Object
readonly
Returns the value of attribute appdir.
-
#appname ⇒ Object
readonly
Returns the value of attribute appname.
-
#cfg ⇒ Object
readonly
Returns the value of attribute cfg.
-
#ext ⇒ Object
readonly
Returns the value of attribute ext.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#xdgbases ⇒ Object
readonly
Returns the value of attribute xdgbases.
Class Method Summary collapse
Instance Method Summary collapse
- #cachedir ⇒ Object
-
#config ⇒ Object
Reads config.
-
#config=(obj) ⇒ Object
Saves config.
- #config? ⇒ Boolean
- #config_file_name ⇒ Object
- #configdir ⇒ Object
- #configures(hash) ⇒ Object
- #datadir ⇒ Object
-
#initialize(parser:, appdir: UserSpace.appdir, ext: parser.to_s.downcase, appname: File.basename($0), xdgbases: ['cache', 'config', 'data'], config: 'config') ⇒ UserSpace
constructor
A new instance of UserSpace.
-
#install ⇒ Object
Will not overwrite anything.
- #xdg_pairs ⇒ Object
Constructor Details
#initialize(parser:, appdir: UserSpace.appdir, ext: parser.to_s.downcase, appname: File.basename($0), xdgbases: ['cache', 'config', 'data'], config: 'config') ⇒ UserSpace
Returns a new instance of UserSpace.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/user_space.rb', line 21 def initialize( parser:, appdir: UserSpace.appdir, ext: parser.to_s.downcase, appname: File.basename($0), xdgbases: ['cache', 'config', 'data'], config: 'config') @parser,@ext,@appname,@xdgbases,@appdir,@cfg = parser,ext,appname,xdgbases,appdir,config install end |
Instance Attribute Details
#appdir ⇒ Object (readonly)
Returns the value of attribute appdir.
20 21 22 |
# File 'lib/user_space.rb', line 20 def appdir @appdir end |
#appname ⇒ Object (readonly)
Returns the value of attribute appname.
20 21 22 |
# File 'lib/user_space.rb', line 20 def appname @appname end |
#cfg ⇒ Object (readonly)
Returns the value of attribute cfg.
20 21 22 |
# File 'lib/user_space.rb', line 20 def cfg @cfg end |
#ext ⇒ Object (readonly)
Returns the value of attribute ext.
20 21 22 |
# File 'lib/user_space.rb', line 20 def ext @ext end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
20 21 22 |
# File 'lib/user_space.rb', line 20 def parser @parser end |
#xdgbases ⇒ Object (readonly)
Returns the value of attribute xdgbases.
20 21 22 |
# File 'lib/user_space.rb', line 20 def xdgbases @xdgbases end |
Class Method Details
Instance Method Details
#cachedir ⇒ Object
69 70 71 |
# File 'lib/user_space.rb', line 69 def cachedir File.join XDG['cache'], @appname end |
#config ⇒ Object
Reads config
90 91 92 93 94 95 96 |
# File 'lib/user_space.rb', line 90 def config @parser.load File.read(config_file_name) rescue $stderr.puts $!. if $VERBOSE $stderr.puts $!.backtrace if $DEBUG nil end |
#config=(obj) ⇒ Object
Saves config
99 100 101 102 103 |
# File 'lib/user_space.rb', line 99 def config=(obj) dump = (@parser.respond_to?(:pretty_generate))? @parser.pretty_generate(obj) : @parser.dump(obj) File.open(config_file_name, 'w', 0600){|fh|fh.puts dump} end |
#config? ⇒ Boolean
85 86 87 |
# File 'lib/user_space.rb', line 85 def config? File.exist?(config_file_name) end |
#config_file_name ⇒ Object
81 82 83 |
# File 'lib/user_space.rb', line 81 def config_file_name File.join XDG['config'], @appname, "#{@cfg}.#{@ext}" end |
#configdir ⇒ Object
73 74 75 |
# File 'lib/user_space.rb', line 73 def configdir File.join XDG['config'], @appname end |
#configures(hash) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/user_space.rb', line 105 def configures(hash) if config? # file exists hash.merge! config else $stderr.puts config_file_name if $VERBOSE self.config = hash end end |
#datadir ⇒ Object
77 78 79 |
# File 'lib/user_space.rb', line 77 def datadir File.join XDG['data'], @appname end |
#install ⇒ Object
Will not overwrite anything. Only copies over missing directories and files. Verifies directory expectations.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/user_space.rb', line 42 def install xdg_pairs do |basedir, userdir| if File.exist?(userdir) # Sanity check assert_directory(userdir) else Dir.mkdir(userdir, 0700) end if File.directory? basedir Dir.glob("#{basedir}/**/*").each do |src| dest = src.sub(basedir, userdir) if File.exist? dest # Sanity checks assert_directory(dest) if File.directory? src else if File.directory? src Dir.mkdir dest else FileUtils.cp src, dest end FileUtils.chmod('u+rwX,go-rwx', dest) end end end end end |
#xdg_pairs ⇒ Object
32 33 34 35 36 37 |
# File 'lib/user_space.rb', line 32 def xdg_pairs @xdgbases.each do |base| # yield basedir, userdir yield File.join(@appdir, base), File.join(XDG[base], @appname) end end |