lib/xdg.rb in xdg-0.4.0 vs lib/xdg.rb in xdg-0.5.0

- old
+ new

@@ -1,10 +1,5 @@ -# :Title: XDG -# :Author: &trans; -# :Copyright: (c)2008 Tiger Ops -# :License: GPLv3 - # = XDG Base Directory Standard # # This provides a conveient library for conforming to the # XDG Base Directory Standard. # @@ -26,17 +21,18 @@ # The cache location stores files that could judt as well be deleted # and everyihtng still works find. This is for variable and temporary # files. Like var/ in FHS. # # This module returns paths as strings. - +# module XDG extend self #module_function + # Returns user's home directory. def home - ENV['HOME'] || File.expand_path('~') + File.expand_path('~') # ENV['HOME'] end # Location of user's personal config directory. def config_home File.expand_path( @@ -75,37 +71,61 @@ ENV['XDG_CACHE_HOME'] || File.join(home, '.cache') ) end # Find a file or directory in data dirs. - def data_find(*glob_and_flags) - data_glob(*glob_and_flags).first + # + # See: +data_select+. + # + def data_find(*glob_and_flags, &block) + data_select(*glob_and_flags, &block).first end - def data_glob(*glob_and_flags) + # Return array of matching files or directories + # in any of the data locations. + # + # This starts with the user's home directory + # and then searches system directories. + # + # String parameters are joined into a pathname + # while Integers and Symbols treated as flags. + # + # For example, the following are equivalent: + # + # XDG.data_select('stick/units', File::FNM_CASEFOLD) + # + # XDG.data_select('stick', 'uits', :casefold) + # + def data_select(*glob_and_flags, &block) glob, flags = *glob_and_flags.partition{ |e| String===e } + glob = ['**/*'] if glob.empty? flag = flags.inject(0) do |m, f| if Symbol === f m + File::const_get("FNM_#{f.to_s.upcase}") else m + f.to_i end end find = [] [data_home, *data_dirs].each do |dir| path = File.join(dir, *glob) - find.concat(Dir.glob(path, flag)) + if block_given? + find.concat(Dir.glob(path, flag).select(&block)) + else + find.concat(Dir.glob(path, flag)) + end end find end # Return the fist matching file or directory # from the config locations. # - # See +config_glog+. - def config_find(*glob_and_flags) - config_glob(*glob_and_flags).first + # See: +config_select+. + # + def config_find(*glob_and_flags, &block) + config_select(*glob_and_flags, &block).first end # Return array of matching files or directories # in any of the config locations. # @@ -115,62 +135,73 @@ # String parameters are joined into a pathname # while Integers and Symbols treated as flags. # # For example, the following are equivalent: # - # XDG.config_glob('sow/plugins', File::FNM_CASEFOLD) + # XDG.config_select('sow/plugins', File::FNM_CASEFOLD) # - # XDG.config_glob('sow', 'plugins', :casefold) + # XDG.config_select('sow', 'plugins', :casefold) # - def config_glob(*glob_and_flags) + def config_select(*glob_and_flags) glob, flags = *glob_and_flags.partition{ |e| String===e } + glob = ['**/*'] if glob.empty? flag = flags.inject(0) do |m, f| if Symbol === f m + File::const_get("FNM_#{f.to_s.upcase}") else m + f.to_i end end find = [] [config_home, *config_dirs].each do |dir| path = File.join(dir, *glob) - find.concat(Dir.glob(path, flag)) + if block_given? + find.concat(Dir.glob(path, flag).select(&block)) + else + find.concat(Dir.glob(path, flag)) + end end find end # Return the fist matching file or directory # in the cache directory. # - # See +cache_glog+. - def cache_find(*glob_and_flags) - cache_glob(*glob_and_flags).first + # See: +cache_select+. + # + def cache_find(*glob_and_flags, &block) + cache_select(*glob_and_flags, &block).first end # Return array of matching files or directories # from the cache directory. # # String parameters are joined into a pathname # while Integers and Symbols treated as flags. # # For example, the following are equivalent: # - # XDG.cache_glob('sow/tmp', File::FNM_CASEFOLD) + # XDG.cache_select('sow/tmp', File::FNM_CASEFOLD) # - # XDG.cache_glob('sow', 'tmp', :casefold) + # XDG.cache_select('sow', 'tmp', :casefold) # - def cache_glob(*glob_and_flags) + def cache_select(*glob_and_flags) glob, flags = *glob_and_flags.partition{ |e| String===e } + glob = ['**/*'] if glob.empty? flag = flags.inject(0) do |m, f| if Symbol === f m + File::const_get("FNM_#{f.to_s.upcase}") else m + f.to_i end end path = File.join(cache_home,*glob) - Dir.glob(path, flag) + if block_given? + Dir.glob(path, flag).select(&block) + else + Dir.glob(path, flag) + end end #-- # The following are not strictly XDG spec, # but are useful in a similar respect. @@ -196,6 +227,11 @@ File.join(Dir.pwd, '.cache') ) end end # module XDG + +# :Title: XDG +# :Author: &trans; +# :Copyright: (c)2008 Tiger Ops +# :License: GPLv3