lib/cuts/cut.rb in cuts-0.0.1 vs lib/cuts/cut.rb in cuts-0.0.4

- old
+ new

@@ -1,37 +1,17 @@ -# TITLE: +# cut.rb +# Copyright (c) 2005,2008 Thomas Sawyer # -# Cut +# Ruby License # -# SUMMARY: +# This module is free software. You may use, modify, and/or redistribute this +# software under the same terms as Ruby. # -# By definition, a Cut is a *transparent* subclass. -# Thay serve as the basis of Cut-based AOP, by providing -# a clean wrapping mechinism. -# -# COPYRIGHT: -# -# Copyright (c) 2005 Thomas Sawyer -# -# LICENSE: -# -# Ruby License -# -# This module is free software. You may use, modify, and/or redistribute this -# software under the same terms as Ruby. -# -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. -# -# AUTHORS: -# -# - Thomas Sawyer +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. -require 'facets/module/name' -require 'facets/kernel/silence' - # = Cut # # Cuts are transparent subclasses. Thay are the basis of # Cut-based AOP. The general idea of Cut-based AOP is that # the Cut can serve a clean container for customized advice on @@ -96,12 +76,11 @@ # This is simple and relatvely robust, but not 100% transparent. # So we add some redirection methods to the cut to improve the # transparency. # # Due to limitation in meta-programming Ruby as this level, the -# transparency isn't perfect, but it's fairly close, and we continue -# to improve it. +# transparency isn't perfect, but it's fairly close. class Cut def self.new(klass, &block) cut = Class.new(klass, &block) # <-- This is the actual cut. @@ -109,14 +88,14 @@ #cut.class_eval(&block) cut.send(:include, Transparency) cut.extend MetaTransparency - # TODO Shutdown warning - silence_warnings do - klass.modspace::const_set(klass.basename, cut) - end + v = $VERBOSE + $VERBOSE = false + klass.modspace::const_set(klass.basename, cut) + $VERBOSE = v return cut end # These methods are needed to emulate full transparancy as @@ -183,8 +162,51 @@ #klass.modspace::const_set(klass.basename, cut) mod = (Module === self ? self : Object) mod.const_set(cutname, cut) # <<- this is what we don't have in Cut.new return cut + end +end + +class Module + # Returns the root name of the module/class. + # + # module Example + # class Demo + # end + # end + # + # Demo.name #=> "Example::Demo" + # Demo.basename #=> "Demo" + # + # For anonymous modules this will provide a basename + # based on Module#inspect. + # + # m = Module.new + # m.inspect #=> "#<Module:0xb7bb0434>" + # m.basename #=> "Module_0xb7bb0434" + # + def basename + if name and not name.empty? + name.gsub(/^.*::/, '') + else + nil #inspect.gsub('#<','').gsub('>','').sub(':', '_') + end + end + + # Returns the module's container module. + # + # module Example + # class Demo + # end + # end + # + # Example::Demo.modspace #=> Example + # + # See also Module#basename. + # + def modspace + space = name[ 0...(name.rindex( '::' ) || 0)] + space.empty? ? Object : eval(space) end end