Module: ASE
- Defined in:
- lib/as-extensions/fs.rb,
lib/as-extensions.rb,
lib/as-extensions/log.rb,
lib/as-extensions/net.rb,
lib/as-extensions/deep.rb,
lib/as-extensions/enum.rb,
lib/as-extensions/need.rb,
lib/as-extensions/test.rb,
lib/as-extensions/time.rb,
lib/as-extensions/extra-ext.rb,
lib/as-extensions/ext-logic/io.rb,
lib/as-extensions/ext-logic/string.rb
Overview
– Copyright © 2010-2011 Moodstocks SAS
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
www.apache.org/licenses/LICENSE-2.0
or in the LICENSE file which you should have received as part of this distribution.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ++
Defined Under Namespace
Modules: IO, String, Test, Time Classes: Enum
Constant Summary collapse
- LOGGER =
Initialize logger
Logger.new(STDERR)
- NEEDED =
To store gems already need()ed
{}
- EXTRA_EXT =
%w{ libxml }
Class Method Summary collapse
-
.deepcompact(x) ⇒ Object
Remove nil values in a recursive data structure composed of hashes and arrays.
-
.download(uri, path) ⇒ Object
Download a file to a path and return it.
-
.ensure_dir_exists(dir, *a) ⇒ Object
Make sure a directory exists; create it if it is not the case.
-
.enum(hash) ⇒ Object
Instantiate a new Enum from a Hash.
- .load_extra_ext(ext) ⇒ Object
-
.log(s, level = (:warn)) ⇒ Object
Print to the logs.
- .log_level=(level = (:warn)) ⇒ Object
-
.need(ext) ⇒ Object
Require a gem / gems on the fly.
-
.require_ext(ext, dir = nil) ⇒ Object
Require an extension / extensions of a library.
-
.require_part(part, dir = nil) ⇒ Object
Require a part / parts of a library.
-
.select_file(*files) ⇒ Object
Return the first file that exists in a list of files.
-
.silently ⇒ Object
Run a block with logging disabled.
Class Method Details
.deepcompact(x) ⇒ Object
Remove nil values in a recursive data structure composed of hashes and arrays.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/as-extensions/deep.rb', line 24 def deepcompact(x) if x.is_a?(::Array) x.reject{ |x| x.nil? }.map{ |x| deepcompact(x) } elsif x.is_a?(::Hash) ret = {} x.each_pair do |k,v| ret[k] = deepcompact(v) if !v.nil? end ret else x end end |
.download(uri, path) ⇒ Object
Download a file to a path and return it
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/as-extensions/net.rb', line 25 def download(uri, path) File.open(path, "wb") do |o_f| open(uri) do |i_f| while (blk = i_f.read(65536)) o_f.write(blk) end end end path end |
.ensure_dir_exists(dir, *a) ⇒ Object
Make sure a directory exists; create it if it is not the case.
26 27 28 29 30 |
# File 'lib/as-extensions/fs.rb', line 26 def ensure_dir_exists(dir, *a) dir = File.join(File.(dir), *a) FileUtils.mkdir_p dir unless File.exist?(dir) dir end |
.enum(hash) ⇒ Object
Instantiate a new Enum from a Hash. Usage example:
Numbers = enum( :ONE => 1, :TWO => 2, :THREE => 3 )
assert (1 == Numbers::ONE)
57 58 59 60 |
# File 'lib/as-extensions/enum.rb', line 57 def enum(hash) k = Class.new(Enum) k.init(hash) end |
.load_extra_ext(ext) ⇒ Object
26 27 28 |
# File 'lib/as-extensions/extra-ext.rb', line 26 def load_extra_ext(ext) require_part(ext) if EXTRA_EXT.include?(ext) end |
.log(s, level = (:warn)) ⇒ Object
Print to the logs. Exit if logging a fatal error. Level can be one of: :debug, :info, :warn, :error, :fatal. Returns nil.
31 32 33 34 35 |
# File 'lib/as-extensions/log.rb', line 31 def log(s, level=(:warn)) LOGGER.send(level, s) exit! if level == :fatal nil end |
.log_level=(level = (:warn)) ⇒ Object
37 38 39 |
# File 'lib/as-extensions/log.rb', line 37 def log_level=(level=(:warn)) LOGGER.level = level.is_a?(Symbol) ? Logger.const_get(level.to_s.upcase) : level end |
.need(ext) ⇒ Object
Require a gem / gems on the fly. Raises an exception if the load fails.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/as-extensions/need.rb', line 29 def need(ext) if ext.is_a?(::Array) ext.each do |e| need(e) end else return true if NEEDED[ext] begin require ext rescue Exception ASE::log("Couldn't load \"#{ext}\".", :error) raise end load_extra_ext(ext) NEEDED[ext] = true end end |
.require_ext(ext, dir = nil) ⇒ Object
Require an extension / extensions of a library
39 40 41 42 43 44 45 46 47 |
# File 'lib/as-extensions.rb', line 39 def require_ext(ext,dir=nil) orig = caller(1).first.split(':').first dir ||= File.join( File.dirname(File.dirname(orig)),"ext") if ext.is_a?(::Array) ext.each{|e| require_ext(e,dir)} else require File.join(dir,ext) end end |
.require_part(part, dir = nil) ⇒ Object
Require a part / parts of a library
28 29 30 31 32 33 34 35 36 |
# File 'lib/as-extensions.rb', line 28 def require_part(part,dir=nil) orig = caller(1).first.split(':').first dir ||= File.join(File.dirname(orig),File.basename(orig,".rb")) if part.is_a?(::Array) part.each{|p| require_part(p,dir)} else require File.join(dir,part) end end |
.select_file(*files) ⇒ Object
Return the first file that exists in a list of files
33 34 35 36 37 38 |
# File 'lib/as-extensions/fs.rb', line 33 def select_file(*files) files.each do |file| if File.exists?(file) then return file end end return nil end |