Module: Share

Defined in:
lib/share-paths.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
[ 0, 1 ]

Class Method Summary collapse

Class Method Details

.appnameObject



11
12
13
14
# File 'lib/share-paths.rb', line 11

def appname
  @@appname ||= File.basename $0, '.rb'
  @@appname
end

.default_instance_pathObject



73
74
75
# File 'lib/share-paths.rb', line 73

def default_instance_path
  File.expand_path './share'
end

.default_system_pathObject



29
30
31
# File 'lib/share-paths.rb', line 29

def default_system_path
  "/var/share/#{appname}"
end

.default_user_pathObject



51
52
53
# File 'lib/share-paths.rb', line 51

def default_user_path
  File.expand_path "~/.local/share/#{appname}"
end

.find_share_file(name) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/share-paths.rb', line 150

def find_share_file name
  if String === name
    paths = share_paths
    paths.reverse_each do |path|
      full = File.expand_path name, path
      if File.exist? full
        return full
      end
    end
    nil
  else
    raise Error, "#{name.inspect} is not a valid name!", caller
  end
end

.get_share_file(name) ⇒ Object Also known as: []



165
166
167
168
169
# File 'lib/share-paths.rb', line 165

def get_share_file name
  @@found_files ||= {}
  @@found_files[name] ||= find_share_file name
  @@found_files[name]
end

.instance_pathObject



77
78
79
80
# File 'lib/share-paths.rb', line 77

def instance_path
  @@instance_path ||= default_instance_path
  @@instance_path
end

.register_vendor_path(*paths) ⇒ Object Also known as: <<



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/share-paths.rb', line 118

def register_vendor_path *paths
  if paths.length == 0
    return register_vendor_path share_by_src(caller_locations[0].path)
  end
  @@vendor_paths ||= []
  result = false
  paths.each do |pth|
    case pth
    when nil
      next
    when Gem::Specification
      full = share_by_gem pth
    when String
      full = File.expand_path pth
    else
      raise Error, "#{pth.inspect} is not a valid path!", caller
    end
    if !@@vendor_paths.include?(full)
      @@vendor_paths << full
      result = true
    end
  end
  result
end

.set_appname(name) ⇒ Object Also known as: appname=



16
17
18
19
20
21
22
23
24
25
# File 'lib/share-paths.rb', line 16

def set_appname name
  case name
  when nil
    @@appname = File.basename $0, '.rb'
  when String, Symbol
    @@appname = name.to_s
  else
    raise Error, "#{name.inspect} is not a valid application name!", caller
  end
end

.set_instance_path(path) ⇒ Object Also known as: instance_path=



82
83
84
85
86
87
88
89
90
91
# File 'lib/share-paths.rb', line 82

def set_instance_path path
  case path
  when nil
    @@instance_path = default_instance_path
  when String
    @@instance_path = File.expand_path path
  else
    raise Error, "#{path.inspect} is not a valid path!", caller
  end
end

.set_system_path(path) ⇒ Object Also known as: system_path=



38
39
40
41
42
43
44
45
46
47
# File 'lib/share-paths.rb', line 38

def set_system_path path
  case path
  when nil
    @@system_path = default_system_path
  when String
    @@system_path = File.expand_path path
  else
    raise Error, "#{path.inspect} is not a valid path!", caller
  end
end

.set_user_path(path) ⇒ Object Also known as: user_path=



60
61
62
63
64
65
66
67
68
69
# File 'lib/share-paths.rb', line 60

def set_user_path path
  case path
  when nil
    @@user_path = default_user_path
  when String
    @@user_path = File.expand_path path
  else
    raise Error, "#{path.inspect} is not a valid path!", caller
  end
end

.share_by_gem(spec) ⇒ Object



114
115
116
# File 'lib/share-paths.rb', line 114

def share_by_gem spec
  File.expand_path 'share', spec.gem_dir
end

.share_by_src(src) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/share-paths.rb', line 100

def share_by_src src
  spec = Gem::Specification.find do |spc|
    File.fnmatch File.join(spc.full_gem_path, '*'), src
  end
  if spec
    share_by_gem spec
  elsif src
    base = src[ /^\/.*\/(lib|bin)\// ]
    base && File.expand_path('../share', base)
  else
    nil
  end
end

.share_pathsObject Also known as: to_a



146
147
148
# File 'lib/share-paths.rb', line 146

def share_paths
  [ *vendor_paths, *system_path, *user_path, *instance_path ]
end

.system_pathObject



33
34
35
36
# File 'lib/share-paths.rb', line 33

def system_path
  @@system_path ||= default_system_path
  @@system_path
end

.user_pathObject



55
56
57
58
# File 'lib/share-paths.rb', line 55

def user_path
  @@user_path ||= default_user_path
  @@user_path
end

.vendor_pathsObject



95
96
97
98
# File 'lib/share-paths.rb', line 95

def vendor_paths
  @@vendor_paths ||= []
  @@vendor_paths
end