utilrb 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/utilrb/common.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Utilrb
2
2
  unless defined? Utilrb::VERSION
3
- VERSION = "1.5.0"
3
+ VERSION = "1.6.0"
4
4
  RUBY_IS_19 = (RUBY_VERSION >= "1.9.2")
5
5
  RUBY_IS_191 = (RUBY_VERSION >= "1.9") && (RUBY_VERSION < "1.9.2")
6
6
  end
@@ -91,12 +91,20 @@ module Kernel
91
91
  end
92
92
  end
93
93
 
94
- def eval_dsl_file_content(file, file_content, proxied_object, context, full_backtrace, *exceptions, &block)
94
+ def eval_dsl(text, proxied_object, context, full_backtrace, *exceptions)
95
+ eval_dsl_file_content(nil, text, proxied_object, context, full_backtrace, *exceptions)
96
+ end
97
+
98
+ def eval_dsl_file_content(file, file_content, proxied_object, context, full_backtrace, *exceptions)
95
99
  code = with_module(*context) do
96
100
  code = <<-EOD
97
101
  Proc.new { #{file_content} }
98
102
  EOD
99
- eval code, binding, file, 1
103
+ if file
104
+ eval code, binding, file, 1
105
+ else
106
+ eval code, binding
107
+ end
100
108
  end
101
109
 
102
110
  dsl_exec_common(file, proxied_object, context, full_backtrace, *exceptions, &code)
@@ -125,8 +133,13 @@ module Kernel
125
133
  return false
126
134
  end
127
135
 
128
- eval_dsl_file(file, *args, &block)
129
136
  $LOADED_FEATURES << file
137
+ begin
138
+ eval_dsl_file(file, *args, &block)
139
+ rescue Exception
140
+ $LOADED_FEATURES.delete(file)
141
+ raise
142
+ end
130
143
  true
131
144
  end
132
145
 
@@ -16,7 +16,20 @@ module Kernel
16
16
  # filter_options(option, array) -> known, unknown
17
17
  # filter_options(nil, known_options) -> default_options, {}
18
18
  #
19
- def filter_options(options, option_spec)
19
+ def filter_options(options, *user_option_spec)
20
+ option_spec = Hash.new
21
+ user_option_spec.each do |opt|
22
+ if opt.respond_to?(:to_hash)
23
+ option_spec.merge!(opt)
24
+ elsif opt.respond_to?(:to_ary)
25
+ opt.each do |key|
26
+ option_spec[key] = nil
27
+ end
28
+ else
29
+ option_spec[opt] = nil
30
+ end
31
+ end
32
+
20
33
  unknown_options = Hash.new
21
34
  options = options.dup
22
35
  options.delete_if do |key, value|
@@ -52,9 +65,9 @@ module Kernel
52
65
  # keys. +option_hash+ keys shall be in +known_array+. +nil+ is treated
53
66
  # as an empty option hash, all keys are converted into symbols.
54
67
  #
55
- def validate_options(options, known_options)
68
+ def validate_options(options, *known_options)
56
69
  options ||= Hash.new
57
- opt, unknown = Kernel.filter_options(options.to_hash, known_options)
70
+ opt, unknown = Kernel.filter_options(options.to_hash, *known_options)
58
71
  unless unknown.empty?
59
72
  not_valid = unknown.keys.map { |m| "'#{m}'" }.join(" ")
60
73
  raise ArgumentError, "unknown options #{not_valid}", caller(1)
@@ -9,29 +9,36 @@ class Logger
9
9
  # It defines the #logger accessor which either returns the logger
10
10
  # attribute of the module, if one is defined, or its parent logger
11
11
  # attribute.
12
+ #
13
+ # This module is usually used in conjunction with the Logger::Root method:
12
14
  #
13
- # module First
14
- # include Hierarchy
15
- # self.logger = Logger.new
15
+ # module First
16
+ # extend Logger.Root("First", :INFO)
16
17
  #
17
- # module Second
18
- # include Hierarchy
18
+ # module Second
19
+ # extend Hierarchy
20
+ # end
19
21
  # end
20
- # end
21
22
  #
22
- # Second.logger will return First.logger. If we do
23
- # Second.logger = Logger.new, then this one would
24
- # be returned.
23
+ # Second.logger will return First.logger. If we do Second.make_own_logger,
24
+ # then a different object will be returned.
25
+ #
26
+ # "extend Hierarchy" will also add the Forward support if the parent module
27
+ # has it.
25
28
  module Hierarchy
29
+ # Allows to change the logger object at this level of the hierarchy
30
+ #
31
+ # This is usually not used directly: a new logger can be created with
32
+ # Hierarchy#make_own_logger and removed with Hierarchy#reset_own_logger
26
33
  attr_writer :logger
27
34
 
28
- def self.included(obj)
35
+ def self.included(obj) # :nodoc:
29
36
  if obj.singleton_class.ancestors.include?(Logger::Forward)
30
37
  obj.send(:include, Logger::Forward)
31
38
  end
32
39
  end
33
40
 
34
- def self.extended(obj)
41
+ def self.extended(obj) # :nodoc:
35
42
  if obj.kind_of?(Module)
36
43
  parent_module = constant(obj.spacename)
37
44
  if (parent_module.singleton_class.ancestors.include?(Logger::Forward))
@@ -40,10 +47,15 @@ class Logger
40
47
  end
41
48
  end
42
49
 
50
+ # Returns true if the local module has its own logger, and false if it
51
+ # returns the logger of the parent
43
52
  def has_own_logger?
44
53
  defined?(@logger) && @logger
45
54
  end
46
55
 
56
+ # Makes it so that this level of the module hierarchy has its own
57
+ # logger. If +new_progname+ and/or +new_level+ are nil, the associated
58
+ # value are taken from the parent's logger.
47
59
  def make_own_logger(new_progname = nil, new_level = nil)
48
60
  if !has_own_logger?
49
61
  @logger = self.logger.dup
@@ -57,10 +69,14 @@ class Logger
57
69
  @logger
58
70
  end
59
71
 
72
+ # Removes a logger defined at this level of the module hierarchy. The
73
+ # logging methods will now access the parent's module logger.
60
74
  def reset_own_logger
61
75
  @logger = nil
62
76
  end
63
77
 
78
+ # Returns the logger object that should be used to log at this level of
79
+ # the module hierarchy
64
80
  def logger
65
81
  if defined?(@logger) && @logger
66
82
  return @logger
@@ -1,3 +1,4 @@
1
+ require 'utilrb/common'
1
2
  class Module
2
3
  if Utilrb::RUBY_IS_191 || Utilrb::RUBY_IS_19
3
4
  def const_defined_here?(name)
@@ -13,7 +13,13 @@ class Module
13
13
  # ClassExtension in the target, it is created)
14
14
  # * if it is included in a Class, the ClassExtension module
15
15
  # extends the class.
16
- def include(mod)
16
+ def include(*mods)
17
+ mods.each do |mod|
18
+ __include_single_module(mod)
19
+ end
20
+ end
21
+
22
+ def __include_single_module(mod)
17
23
  if mod.const_defined?(:ModuleExtension)
18
24
  if is_a?(Module)
19
25
  unless const_defined?(:ModuleExtension)
data/test/test_hash.rb CHANGED
@@ -27,6 +27,14 @@ class TC_Hash < Test::Unit::TestCase
27
27
  assert_equal(["1 => 2", "2 => 3", "3 => ..."].to_set, values.to_set)
28
28
  end
29
29
 
30
+ def test_map_key
31
+ base = { 1 => 'a', 2 => 'b' }
32
+ result = base.map_key { |k, v| k += 1 }
33
+
34
+ assert_equal({ 1 => 'a', 2 => 'b' }, base)
35
+ assert_equal({ 2 => 'a', 3 => 'b' }, result)
36
+ end
37
+
30
38
  def test_map_value
31
39
  base = { 'a' => 1, 'b' => 2 }
32
40
  result = base.map_value { |k, v| v += 1 }
data/test/test_kernel.rb CHANGED
@@ -203,6 +203,47 @@ class TC_Kernel < Test::Unit::TestCase
203
203
  assert(methods.include?('my_method'), "the 'class K' statement did not refer to the already defined class")
204
204
  end
205
205
 
206
+ def test_load_dsl_file_loaded_features_behaviour
207
+ eval_context = Class.new do
208
+ attr_reader :real_method_call_count
209
+ def initialize
210
+ @real_method_call_count = 0
211
+ end
212
+ def real_method
213
+ @real_method_call_count += 1
214
+ end
215
+ end
216
+
217
+ Tempfile.open('test_eval_dsl_file') do |io|
218
+ io.puts <<-EOD
219
+ real_method
220
+ EOD
221
+ io.flush
222
+
223
+ obj = eval_context.new
224
+ assert(Kernel.load_dsl_file(io.path, obj, [], false))
225
+ assert_equal(1, obj.real_method_call_count)
226
+ assert($LOADED_FEATURES.include?(io.path))
227
+ assert(!Kernel.load_dsl_file(io.path, obj, [], false))
228
+ assert_equal(1, obj.real_method_call_count)
229
+
230
+ $LOADED_FEATURES.delete(io.path)
231
+ end
232
+
233
+ Tempfile.open('test_eval_dsl_file') do |io|
234
+ io.puts <<-EOD
235
+ raise
236
+ EOD
237
+ io.flush
238
+
239
+ obj = eval_context.new
240
+ assert(!$LOADED_FEATURES.include?(io.path))
241
+ assert_raises(RuntimeError) { Kernel.load_dsl_file(io.path, obj, [], false) }
242
+ assert(!$LOADED_FEATURES.include?(io.path))
243
+ assert_equal(0, obj.real_method_call_count)
244
+ end
245
+ end
246
+
206
247
  Utilrb.require_ext('is_singleton?') do
207
248
  def test_is_singleton
208
249
  klass = Class.new
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: utilrb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 5
8
+ - 6
9
9
  - 0
10
- version: 1.5.0
10
+ version: 1.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sylvain Joyeux
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-03 00:00:00 Z
18
+ date: 2012-03-31 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: facets
@@ -229,7 +229,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
229
229
  requirements: []
230
230
 
231
231
  rubyforge_project: utilrb
232
- rubygems_version: 1.8.10
232
+ rubygems_version: 1.8.15
233
233
  signing_key:
234
234
  specification_version: 3
235
235
  summary: Yet another Ruby toolkit