treequel 1.8.6 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -149,64 +149,11 @@ module Treequel
149
149
  ### Add logging to a Treequel class. Including classes get #log and #log_debug methods.
150
150
  module Loggable
151
151
 
152
- ### A logging proxy class that wraps calls to the logger into calls that include
153
- ### the name of the calling class.
154
- class ClassNameProxy
155
-
156
- ### Create a new proxy for the given +klass+.
157
- def initialize( klass, force_debug=false )
158
- @classname = klass.name
159
- @force_debug = force_debug
160
- end
161
-
162
- ### Delegate debug messages to the global logger with the appropriate class name.
163
- def debug( msg=nil, &block )
164
- Treequel.logger.add( Logger::DEBUG, msg, @classname, &block )
165
- end
166
-
167
- ### Delegate info messages to the global logger with the appropriate class name.
168
- def info( msg=nil, &block )
169
- return self.debug( msg, &block ) if @force_debug
170
- Treequel.logger.add( Logger::INFO, msg, @classname, &block )
171
- end
172
-
173
- ### Delegate warn messages to the global logger with the appropriate class name.
174
- def warn( msg=nil, &block )
175
- return self.debug( msg, &block ) if @force_debug
176
- Treequel.logger.add( Logger::WARN, msg, @classname, &block )
177
- end
178
-
179
- ### Delegate error messages to the global logger with the appropriate class name.
180
- def error( msg=nil, &block )
181
- return self.debug( msg, &block ) if @force_debug
182
- Treequel.logger.add( Logger::ERROR, msg, @classname, &block )
183
- end
184
-
185
- ### Delegate fatal messages to the global logger with the appropriate class name.
186
- def fatal( msg=nil, &block )
187
- Treequel.logger.add( Logger::FATAL, msg, @classname, &block )
188
- end
189
-
190
- end # ClassNameProxy
191
-
192
- #########
193
- protected
194
- #########
195
-
196
- ### Copy constructor -- clear the original's log proxy.
197
- def initialize_copy( original )
198
- @log_proxy = @log_debug_proxy = nil
199
- super
200
- end
201
-
202
- ### Return the proxied logger.
203
- def log
204
- @log_proxy ||= ClassNameProxy.new( self.class )
205
- end
206
-
207
- ### Return a proxied "debug" logger that ignores other level specification.
208
- def log_debug
209
- @log_debug_proxy ||= ClassNameProxy.new( self.class, true )
152
+ ### Inclusion callback -- extend including modules with Loggability instead for
153
+ ### backward-compatibility.
154
+ def self::included( mod )
155
+ mod.extend( Loggability )
156
+ mod.log_to( :treequel )
210
157
  end
211
158
 
212
159
  end # module Loggable
@@ -10,17 +10,22 @@ require 'treequel/branchset'
10
10
 
11
11
  # An object interface to LDAP entries.
12
12
  class Treequel::Model < Treequel::Branch
13
+ extend Loggability
14
+
13
15
  require 'treequel/model/objectclass'
14
16
  require 'treequel/model/errors'
15
17
  require 'treequel/model/schemavalidations'
16
18
 
17
- include Treequel::Loggable,
18
- Treequel::Constants,
19
+ include Treequel::Constants,
19
20
  Treequel::Normalization,
20
21
  Treequel::Constants::Patterns,
21
22
  Treequel::Model::SchemaValidations
22
23
 
23
24
 
25
+ # Loggability API -- Log to the Treequel module's logger
26
+ log_to :treequel
27
+
28
+
24
29
  # A prototype Hash that autovivifies its members as Sets, for use in
25
30
  # the objectclass_registry and the base_registry
26
31
  SET_HASH = Hash.new {|h,k| h[k] = Set.new }
@@ -33,8 +33,12 @@ require 'treequel/constants'
33
33
  # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
34
  #
35
35
  class Treequel::Model::Errors < ::Hash
36
- include Treequel::HashUtilities,
37
- Treequel::Loggable
36
+ extend Loggability
37
+ include Treequel::HashUtilities
38
+
39
+ # Loggability API -- Log to the Treequel module's logger
40
+ log_to :treequel
41
+
38
42
 
39
43
  # The word to use between attributes in error messages
40
44
  ATTRIBUTE_CONJUNCTION = ' and '
@@ -22,8 +22,11 @@ require 'treequel/mixins'
22
22
  # Please see the file LICENSE in the base directory for licensing details.
23
23
  #
24
24
  class Treequel::Schema
25
- include Treequel::Loggable,
26
- Treequel::Constants::Patterns
25
+ extend Loggability
26
+ include Treequel::Constants::Patterns
27
+
28
+ # Loggability API -- Log to the Treequel module's logger
29
+ log_to :treequel
27
30
 
28
31
  require 'treequel/schema/table'
29
32
  require 'treequel/schema/objectclass'
@@ -22,11 +22,15 @@ require 'treequel/exceptions'
22
22
  # Please see the file LICENSE in the base directory for licensing details.
23
23
  #
24
24
  class Treequel::Schema::AttributeType
25
- include Treequel::Loggable,
26
- Treequel::Normalization,
25
+ include Treequel::Normalization,
27
26
  Treequel::Constants::Patterns
28
27
 
29
- extend Treequel::AttributeDeclarations
28
+ extend Loggability,
29
+ Treequel::AttributeDeclarations
30
+
31
+ # Loggability API -- Log to the Treequel module's logger
32
+ log_to :treequel
33
+
30
34
 
31
35
  # Regex for splitting a syntax OID from its length specifier
32
36
  OID_SPLIT_PATTERN = /
@@ -10,10 +10,13 @@ require 'treequel/exceptions'
10
10
 
11
11
  # This is a class for representing ldapSyntax declarations in a Treequel::Schema.
12
12
  class Treequel::Schema::LDAPSyntax
13
- include Treequel::Loggable,
14
- Treequel::Constants::Patterns
13
+ include Treequel::Constants::Patterns
14
+ extend Loggability,
15
+ Treequel::AttributeDeclarations
15
16
 
16
- extend Treequel::AttributeDeclarations
17
+
18
+ # Loggability API -- Log to the Treequel module's logger
19
+ log_to :treequel
17
20
 
18
21
 
19
22
  #############################################################
@@ -10,10 +10,13 @@ require 'treequel/exceptions'
10
10
 
11
11
  # This is a class for representing matchingRule declarations in a Treequel::Schema.
12
12
  class Treequel::Schema::MatchingRule
13
- include Treequel::Loggable,
14
- Treequel::Constants::Patterns
13
+ include Treequel::Constants::Patterns
14
+ extend Loggability,
15
+ Treequel::AttributeDeclarations
15
16
 
16
- extend Treequel::AttributeDeclarations
17
+
18
+ # Loggability API -- Log to the Treequel module's logger
19
+ log_to :treequel
17
20
 
18
21
 
19
22
  #############################################################
@@ -10,10 +10,14 @@ require 'treequel/exceptions'
10
10
 
11
11
  # This is a class for representing matchingRuleUse declarations in a Treequel::Schema.
12
12
  class Treequel::Schema::MatchingRuleUse
13
- include Treequel::Loggable,
14
- Treequel::Constants::Patterns
13
+ include Treequel::Constants::Patterns
15
14
 
16
- extend Treequel::AttributeDeclarations
15
+ extend Loggability,
16
+ Treequel::AttributeDeclarations
17
+
18
+
19
+ # Loggability API -- Log to the Treequel module's logger
20
+ log_to :treequel
17
21
 
18
22
 
19
23
  #############################################################
@@ -29,12 +29,17 @@ class Treequel::Schema
29
29
 
30
30
  ### objectClass entries in a Treequel::Schema.
31
31
  class ObjectClass
32
- include Treequel::Loggable,
33
- Treequel::Constants::Patterns
32
+ include Treequel::Constants::Patterns
34
33
 
35
- extend Treequel::AttributeDeclarations
34
+ extend Loggability,
35
+ Treequel::AttributeDeclarations
36
36
 
37
37
 
38
+ # Loggability API -- Log to the Treequel module's logger
39
+ log_to :treequel
40
+
41
+
42
+ # Hide the constructor
38
43
  private_class_method :new
39
44
 
40
45
  # The 'kind' of objectClasses which don't specify a 'kind' explicitly
@@ -12,12 +12,16 @@ require 'treequel/mixins'
12
12
  # This is an object that is used to store LDAP schema information in a
13
13
  # case-insensitive table.
14
14
  class Treequel::Schema::Table
15
- extend Forwardable
15
+ extend Forwardable,
16
+ Loggability
16
17
  include Enumerable,
17
- Treequel::Loggable,
18
18
  Treequel::Normalization,
19
19
  Treequel::Constants::Patterns
20
20
 
21
+ # Loggability API -- Log to the Treequel module's logger
22
+ log_to :treequel
23
+
24
+
21
25
  # The list of methods that should be delegated through the key-normalization
22
26
  # method.
23
27
  KEYED_METHODS = [ :"[]", :"[]=", :delete, :fetch, :key?, :has_key?, :include?,
data/spec/lib/helpers.rb CHANGED
@@ -28,73 +28,13 @@ require 'treequel'
28
28
  require 'spec/lib/constants'
29
29
  require 'spec/lib/matchers'
30
30
 
31
- ### IRb.start_session, courtesy of Joel VanderWerf in [ruby-talk:42437].
32
- require 'irb'
33
- require 'irb/completion'
34
-
35
- module IRB # :nodoc:
36
- def self.start_session( obj )
37
- unless @__initialized
38
- args = ARGV
39
- ARGV.replace( ARGV.dup )
40
- IRB.setup( nil )
41
- ARGV.replace( args )
42
- @__initialized = true
43
- end
44
-
45
- workspace = WorkSpace.new( obj )
46
- irb = Irb.new( workspace )
47
-
48
- @CONF[:IRB_RC].call( irb.context ) if @CONF[:IRB_RC]
49
- @CONF[:MAIN_CONTEXT] = irb.context
50
-
51
- begin
52
- prevhandler = Signal.trap( 'INT' ) do
53
- irb.signal_handle
54
- end
55
-
56
- catch( :IRB_EXIT ) do
57
- irb.eval_input
58
- end
59
- ensure
60
- Signal.trap( 'INT', prevhandler )
61
- end
62
-
63
- end
64
- end
31
+ require 'loggability/spechelpers'
65
32
 
66
33
 
67
34
  ### RSpec helper functions.
68
35
  module Treequel::SpecHelpers
69
36
  include Treequel::TestConstants
70
37
 
71
- class ArrayLogger
72
- ### Create a new ArrayLogger that will append content to +array+.
73
- def initialize( array )
74
- @array = array
75
- end
76
-
77
- ### Write the specified +message+ to the array.
78
- def write( message )
79
- @array << message
80
- end
81
-
82
- ### No-op -- this is here just so Logger doesn't complain
83
- def close; end
84
-
85
- end # class ArrayLogger
86
-
87
-
88
- unless defined?( LEVEL )
89
- LEVEL = {
90
- :debug => Logger::DEBUG,
91
- :info => Logger::INFO,
92
- :warn => Logger::WARN,
93
- :error => Logger::ERROR,
94
- :fatal => Logger::FATAL,
95
- }
96
- end
97
-
98
38
  ###############
99
39
  module_function
100
40
  ###############
@@ -105,35 +45,6 @@ module Treequel::SpecHelpers
105
45
  end
106
46
 
107
47
 
108
- ### Reset the logging subsystem to its default state.
109
- def reset_logging
110
- Treequel.reset_logger
111
- end
112
-
113
-
114
- ### Alter the output of the default log formatter to be pretty in SpecMate output
115
- def setup_logging( level=Logger::FATAL )
116
-
117
- # Turn symbol-style level config into Logger's expected Fixnum level
118
- if Treequel::LOG_LEVELS.key?( level.to_s )
119
- level = Treequel::LOG_LEVELS[ level.to_s ]
120
- end
121
-
122
- logger = Logger.new( $stderr )
123
- Treequel.logger = logger
124
- Treequel.logger.level = level
125
-
126
- # Only do this when executing from a spec in TextMate
127
- if ENV['HTML_LOGGING'] || (ENV['TM_FILENAME'] && ENV['TM_FILENAME'] =~ /_spec\.rb/)
128
- Thread.current['logger-output'] = []
129
- logdevice = ArrayLogger.new( Thread.current['logger-output'] )
130
- Treequel.logger = Logger.new( logdevice )
131
- # Treequel.logger.level = level
132
- Treequel.logger.formatter = Treequel::HtmlLogFormatter.new( logger )
133
- end
134
- end
135
-
136
-
137
48
  ### Make a Treequel::Directory that will use the given +conn+ object as its
138
49
  ### LDAP connection. Also pre-loads the schema object and fixtures some other
139
50
  ### external data.
@@ -176,7 +87,8 @@ abort "You need a version of RSpec >= 2.6.0" unless defined?( RSpec )
176
87
 
177
88
  ### Mock with RSpec
178
89
  RSpec.configure do |c|
179
- include Treequel::TestConstants
90
+ include Treequel::TestConstants,
91
+ Loggability::SpecHelpers
180
92
 
181
93
  c.mock_with :rspec
182
94
 
@@ -185,6 +97,7 @@ RSpec.configure do |c|
185
97
  c.include( Treequel::TestConstants )
186
98
  c.include( Treequel::SpecHelpers )
187
99
  c.include( Treequel::Matchers )
100
+ c.include( Loggability::SpecHelpers )
188
101
 
189
102
  c.treat_symbols_as_metadata_keys_with_true_values = true
190
103
 
@@ -150,15 +150,8 @@ describe Treequel::Directory do
150
150
  describe "instances with a connection" do
151
151
 
152
152
  before( :each ) do
153
- @conn = mock( "ldap connection", :bound? => false )
154
-
155
- @dir = Treequel::Directory.new( @options )
153
+ @dir = Treequel.directory( TEST_LDAPURI )
156
154
  @dir.instance_variable_set( :@conn, @conn )
157
-
158
- @schema = mock( "Directory schema" )
159
- @conn.stub( :schema ).and_return( :the_schema )
160
- Treequel::Schema.stub( :new ).with( :the_schema ).and_return( @schema )
161
- @schema.stub( :attribute_types ).and_return({ :cn => :a_value, :ou => :a_value })
162
155
  end
163
156
 
164
157
  it "can bind with the given user DN and password" do
@@ -359,7 +352,7 @@ describe Treequel::Directory do
359
352
  @conn.stub( :search_ext2 ).and_raise( LDAP::ResultError.new("Can't contact LDAP server") )
360
353
 
361
354
  second_conn = mock( "LDAP connection", :set_option => true, :bound? => false )
362
- LDAP::Conn.should_receive( :new ).and_return( second_conn )
355
+ LDAP::SSLConn.should_receive( :new ).and_return( second_conn )
363
356
  second_conn.should_receive( :search_ext2 ).and_return([])
364
357
 
365
358
  already_tried_reconnect = false
@@ -374,7 +367,7 @@ describe Treequel::Directory do
374
367
  end
375
368
 
376
369
  it "re-raises an exception rescued during a reconnect as a RuntimeError" do
377
- LDAP::Conn.should_receive( :new ).
370
+ LDAP::SSLConn.should_receive( :new ).
378
371
  and_raise( LDAP::ResultError.new("Can't contact LDAP server") )
379
372
 
380
373
  expect {
@@ -384,6 +377,10 @@ describe Treequel::Directory do
384
377
 
385
378
 
386
379
  it "doesn't retain its connection when duplicated" do
380
+ LDAP::SSLConn.stub( :new ).and_return do
381
+ mock( "LDAP connection", :set_option => true, :bound? => false )
382
+ end
383
+
387
384
  @dir.dup.conn.should_not equal( @dir.conn )
388
385
  end
389
386
 
@@ -469,25 +466,15 @@ describe Treequel::Directory do
469
466
  end
470
467
 
471
468
  it "can fetch the server's schema" do
472
- @conn.should_receive( :schema ).and_return( :the_schema )
473
- Treequel::Schema.should_receive( :new ).with( :the_schema ).
474
- and_return( :the_parsed_schema )
475
- @dir.schema.should == :the_parsed_schema
469
+ @dir.schema.should be_a( Treequel::Schema )
476
470
  end
477
471
 
478
472
  it "creates branches for messages that match valid attributeType OIDs" do
479
- @schema.should_receive( :attribute_types ).
480
- and_return({ :cn => :a_value, :ou => :a_value })
481
-
482
- @dir.stub( :bound? ).and_return( false )
483
473
  rval = @dir.ou( :people )
484
474
  rval.dn.downcase.should == TEST_PEOPLE_DN.downcase
485
475
  end
486
476
 
487
477
  it "doesn't create branches for messages that don't match valid attributeType OIDs" do
488
- @schema.should_receive( :attribute_types ).
489
- and_return({ :cn => :a_value, :ou => :a_value })
490
-
491
478
  expect { @dir.void('sbc') }.to raise_error( NoMethodError )
492
479
  end
493
480
 
@@ -32,8 +32,9 @@ describe Treequel, "mixin" do
32
32
 
33
33
  describe Treequel::Loggable, "mixed into a class" do
34
34
  before(:each) do
35
- @logfile = StringIO.new('')
36
- Treequel.logger = Logger.new( @logfile )
35
+ @log_output = []
36
+ Treequel.logger.output_to( @log_output )
37
+ Treequel.logger.level = :debug
37
38
 
38
39
  @test_class = Class.new do
39
40
  include Treequel::Loggable
@@ -41,10 +42,6 @@ describe Treequel, "mixin" do
41
42
  def log_test_message( level, msg )
42
43
  self.log.send( level, msg )
43
44
  end
44
-
45
- def logdebug_test_message( msg )
46
- self.log_debug.debug( msg )
47
- end
48
45
  end
49
46
  @obj = @test_class.new
50
47
  end
@@ -52,14 +49,7 @@ describe Treequel, "mixin" do
52
49
 
53
50
  it "is able to output to the log via its #log method" do
54
51
  @obj.log_test_message( :debug, "debugging message" )
55
- @logfile.rewind
56
- @logfile.read.should =~ /debugging message/
57
- end
58
-
59
- it "is able to output to the log via its #log_debug method" do
60
- @obj.logdebug_test_message( "sexydrownwatch" )
61
- @logfile.rewind
62
- @logfile.read.should =~ /sexydrownwatch/
52
+ @log_output.last.should =~ /debugging message/i
63
53
  end
64
54
  end
65
55