Class: UserSpace

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

Constant Summary collapse

VERSION =
'3.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, ext: parser.to_s.downcase, appname: File.basename($0), xdgbases: ['CACHE', 'CONFIG', 'DATA'], appdir: UserSpace.appdir, config: 'config') ⇒ UserSpace

Returns a new instance of UserSpace.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/user_space.rb', line 27

def initialize(
  parser,
  ext: parser.to_s.downcase,
  appname: File.basename($0),
  xdgbases: ['CACHE', 'CONFIG', 'DATA'],
  appdir: UserSpace.appdir,
  config: 'config'
)
  @parser,@ext,@appname,@xdgbases,@appdir,@config = parser,ext,appname,xdgbases,appdir,config
  install(false) # install with no overwrite
end

Instance Attribute Details

#appdirObject (readonly)

Returns the value of attribute appdir.



26
27
28
# File 'lib/user_space.rb', line 26

def appdir
  @appdir
end

#appnameObject (readonly)

Returns the value of attribute appname.



26
27
28
# File 'lib/user_space.rb', line 26

def appname
  @appname
end

#configObject

Reads config



94
95
96
# File 'lib/user_space.rb', line 94

def config
  @config
end

#extObject (readonly)

Returns the value of attribute ext.



26
27
28
# File 'lib/user_space.rb', line 26

def ext
  @ext
end

#parserObject (readonly)

Returns the value of attribute parser.



26
27
28
# File 'lib/user_space.rb', line 26

def parser
  @parser
end

#xdgbasesObject (readonly)

Returns the value of attribute xdgbases.



26
27
28
# File 'lib/user_space.rb', line 26

def xdgbases
  @xdgbases
end

Class Method Details

.appdirObject



20
21
22
23
24
# File 'lib/user_space.rb', line 20

def self.appdir
  appdir = File.dirname File.dirname caller_locations(1,1)[0].path
  appdir = File.dirname appdir if File.basename(appdir)=='lib'
  File.expand_path appdir
end

Instance Method Details

#cachedirObject



67
68
69
# File 'lib/user_space.rb', line 67

def cachedir
  File.join XDG['CACHE'].to_s, @appname
end

#config?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/user_space.rb', line 89

def config?
  File.exist?(config_file_name)
end

#config_file_nameObject

Not really for public use.



80
81
82
# File 'lib/user_space.rb', line 80

def config_file_name
  File.join XDG['CONFIG'].to_s, @appname, "#{@config}.#{@ext}"
end

#configdirObject



71
72
73
# File 'lib/user_space.rb', line 71

def configdir
  File.join XDG['CONFIG'].to_s, @appname
end

#configures(hash) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/user_space.rb', line 108

def configures(hash)
  if self.config? # file exists
    self.config.each{|opt, value| hash[opt.to_sym] = value}
  else
    $stderr.puts config_file_name if $VERBOSE
    self.config = hash
  end
end

#datadirObject



75
76
77
# File 'lib/user_space.rb', line 75

def datadir
  File.join XDG['DATA'].to_s, @appname
end

#install(overwrite = true) ⇒ Object

Note that initialize will not overwrite anything. This overwrites the user’s data directory with a fresh install. App should consider being nice about this, like warn the user or something.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/user_space.rb', line 52

def install(overwrite=true)
  xdg_pairs do |basedir, userdir|
    if File.exist?(userdir)
      # Sanity check
      raise "Not a directory: #{userdir}" unless File.directory?(userdir)
      # Pre-existing directory.
      # Return unless user wants to overwrite.
      next unless overwrite
    else
      Dir.mkdir(userdir, 0700)
    end
    FileUtils.user_space_cpr(Dir.glob("#{basedir}/*"), userdir) if File.directory? basedir
  end
end

#versionObject

This reads the data directory’s version file



122
123
124
125
126
127
128
# File 'lib/user_space.rb', line 122

def version
  File.read(version_file_name).strip
rescue
  $stderr.puts $!.message   if $VERBOSE
  $stderr.puts $!.backtrace if $DEBUG
  nil
end

#version=(v) ⇒ Object



130
131
132
# File 'lib/user_space.rb', line 130

def version=(v)
  File.open(version_file_name, 'w', 0600){|fh| fh.puts v}
end

#version?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/user_space.rb', line 117

def version?
  File.exist?(version_file_name)
end

#version_file_nameObject

Not really for public use.



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

def version_file_name
  File.join XDG['DATA'].to_s, @appname, 'VERSION'
end

#xdg_pairsObject



39
40
41
42
43
44
45
46
# File 'lib/user_space.rb', line 39

def xdg_pairs
  @xdgbases.each do |base|
    xdg = XDG[base].to_s
    userdir = File.join(xdg, @appname)
    basedir = File.join @appdir, base.downcase
    yield basedir, userdir
  end
end