module Rdoba

Author

Malo Skrylevo <majioa@yandex.ru>

License

MIT

TODO add enum of options hash to convert values to symbols TODO make common format, and format for each of methods >, >>, +, -, %, * TODO add syntax redefinition ability for the methods >, >>, +, -, %, * TODO add multiple output (to more than only the IO)

Constants

VERSION

Public Class Methods

debug(options = {}) click to toggle source
# File lib/rdoba/debug.rb, line 9
def self.debug options = {}
   Rdoba.log options ; end
gemroot(name = nil, path = '') click to toggle source
# File lib/rdoba.rb, line 22
def self.gemroot name = nil, path = ''
   if !gem( name )
      raise "Invalid gem named as #{name.inspect}" ; end
   g = Gem::Specification.find_by_name( name )
   File.join g.full_gem_path, path
end
log(options = {}) click to toggle source

Adds a Log instance to the specified object as a variable or directly into the object itself. It returns the rdoba logger object.

The argument accepted are only options as a Hash of keys and values.

The options keys are accepted the following: as, in, functions, prefix, and io.

Option functions defines the function list that can be used in it. Undeclared in the list functions just do nothing. It should be provided as an Array of function descriptions of String or Symbol class. The function list is the following: info, warn, basic, extended, leave, enter, compat. If omitted it is defaulting to enter, and leave functions.

The function info provides just info along the code, of course it just can be shewn by puts, but it also can be disabled by settings. The call method is :*. Example:

class A
   rdoba :log => { :functions => [ :info ] }
   def a
      log * "Some Info"
   end
end

The function warn provide justs warn message along the code, of course it just can be shewn by puts, or p, but it also can be disabled by settings. The call method is :%. Example:

class A
   rdoba :log => { :functions => [ :warn ] }
   def a
      log % "Some Info"
   end
end

The function basic provides just basic debug message, it also can be disabled by settings. The call method is :>. Example:

class A
   rdoba :log => { :functions => [ :basic ] }
   def a
      v = 123434
      log > { :v => v }
   end
end

The function extended provides extended debug message, class inspect dumps can be used for messages of this function, it also can be disabled by settings. The call method is :>>. Example:

class A
   rdoba :log => { :functions => [ :extended ] }
   def a
      v = ObjectSpace.new
      vv = Object.new
      log >> { :v => v, :vv => vv }
   end
end

The function enter provides just debug message on function entry, it also can be disabled by settings. The call method is :+. Example:

class A
   rdoba :log => { :functions => [ :enter ] }
   def a *args
      log + { :args => args }
   end
end

The function leave provides just debug message on function leave, it also can be disabled by settings. It accepts the just a argument, also returns the provided argument, so it can be used in chain. The call method is :-. Example:

class A
   rdoba :log => { :functions => [ :leave ] }
   def a
      log - 1
   end
end

A.new.a # >> 1

The function compat provides old style log strings dbgXX just for compatibility, and will be removed.

Option prefix defines the prefix that will be shewn before the message text. The following prefix features are available: timestamp, pid, function_name, function_line, function. The full format is the following:

[<timestamp>]{pid}(<function module>:<function name>.<function line>)
<log type> <text>

Here is the function module, function name, function line represents function at whole.

The log types are of the functions previously described, and can be: >, >>, ***, %%%, +++, —.

Option io defines the IO to output the debug message to. It must contain all IO methods required. It is defaulting to $stdout. Also StringIO object is allowed.

class A
   rdoba :log => { :io => $stderr }
   def a
      log - 1
   end
end

Option as defines the name of a method to apply the log functions to. It is defaulting to :log, but when you've specified :self the log functions is being embedded into the caller class instance directly. It should be provided as a Symbol or String. Example:

class A
   rdoba :log => { :as => :self, :functions => [ :basic ] }
   def a
      self > "Debug"
   end
end

Option in defines the name of a target class or a namespace to log implement to. For toplevel it is defaulting to Kernel namespace, for in-class is defaulting to the self class. It should be provided as a constant of the class/module. Example:

class A
   def a
      self > "Debug"
   end
end

rdoba :log => { :in => A, :functions => [ :basic ] }

To compeletly disable the debug messages for the specific class you can use either the RDOBA_LOG environment variable:

$ RDOBA_LOG=0

or redeclare the function list for the specific class to empty.

rdoba :log => { :in => MyClass, :functions => [] }
# File lib/rdoba/log.rb, line 161
def self.log options = {}
   Rdoba::Log.class_variable_set :@@options, options

   functions = [ options[ :functions ] ].flatten
   funcname = ( options[ :as ] ||= :log ).to_s.to_sym
   target = options[ :in ] || options[ :self ]

   if target.class == Object
      Rdoba::Log.log_instance_setup( TOPLEVEL_BINDING.eval 'self' )
   else
      Rdoba::Log.log_class_setup target ; end

   if funcname == :self
      Rdoba::Log.define_methods( target,
         [ :+, :-, :>, :>>, :*, :%, :>=, :<= ] )

      Rdoba::Log.try_define_compat( functions, target )
      target.__rdoba_log__
   else
      if target.class == Object
         Rdoba::Log.log_link_for :instance, target, funcname
      else
         Rdoba::Log.log_link_for :class, target, funcname ; end ; end ; end
mixin(options) click to toggle source
# File lib/rdoba/mixin.rb, line 274
def self.mixin options
   [ options[ :value ] ].flatten.each do |value|
      case value
      when :case
         Mixin::CaseString::Aliases.each do |k,v|
            ::String.send :alias_method, k, v ; end
         Mixin::CaseString::Fixups.each do |e|
            ::String.class_eval "def #{e}(*args);self.__rdoba_mixin_#{e}__(*args);end"
         end # trap NameError
         ::String.send :include, Mixin::CaseString
      when :reverse
         Mixin::ReverseString::Aliases.each do |k,v|
            ::String.send :alias_method, k, v ; end
         Mixin::ReverseString::Fixups.each do |e|
            ::String.class_eval "def #{e}(*args);self.__rdoba_mixin_#{e}__(*args);end"
         end # trap NameError
         String.send :include, Mixin::ReverseString
      when :compare
         String.send :include, Mixin::CompareString
      when :to_h
         if [].respond_to?( :to_h )
            m = Mixin::To_hArray.instance_method( :to_h )
            Array.send( :define_method, :to_h, m )
         else
            Array.send( :include, Mixin::To_hArray ) ; end
      when :time
         require_relative 'mixin/time'
         if File.respond_to?( :mtime )
            [ :mtime, :atime, :ctime ].each do |name|
               m = Mixin::Time.instance_method( name )
               ::File.send( :define_singleton_method, name, m ) ; end
         else
            ::File.send( :extend, Mixin::Time ) ; end
      when :wait_if
         Object.send :include, Mixin::Wait_ifKernel
      when :split_by
         Array.send :include, Mixin::Split_byArray
      when :try
         Object.send :include, Mixin::TryObject
      when :empty
         Object.send :include, Mixin::EmptyObject
         NilClass.send :include, Mixin::EmptyNilClass
      else
         raise( Mixin::InvalidOption, "Invalid rdoba-mixin options key: " +
               "#{value.to_s}" )
         end ; end ; end
os() click to toggle source
# File lib/rdoba.rb, line 29
def self.os
   @@os ||= (
      host_os = RbConfig::CONFIG['host_os']
      case host_os
      when /(mswin|msys|mingw|cygwin|bccwin|wince|emc)/
         plat = $1 == 'mswin' && 'native' || $1
         out = `ver`.encode( 'US-ASCII',
               :invalid => :replace, :undef => :replace )
         if out =~ /\[.* (\d+)\.([\d\.]+)\]/
            "windows-#{plat}-#{$1 == '5' && 'xp' || 'vista'}-#{$1}.#{$2}"
         else
            "windows-#{plat}" ; end
      when /darwin|mac os/
         'macosx'
      when /linux/
         'linux'
      when /(solaris|bsd)/
         "unix-#{$1}"
      else
         raise "unknown os: #{host_os.inspect}"
      end)
end