Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Silenced warnings running in VERBOSE mode
Browse files Browse the repository at this point in the history
* IMHO libraries should run with warnings turned on and not emit any
  warnings.
  • Loading branch information
Dan Kubb committed Mar 2, 2009
1 parent e2e318c commit 74c7e26
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 106 deletions.
8 changes: 3 additions & 5 deletions lib/extlib.rb
@@ -1,11 +1,8 @@
require 'pathname'
require 'rubygems'

__DIR__ = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(__DIR__) unless $LOAD_PATH.include?(__DIR__)

# for Pathname /
require File.expand_path(File.join(__DIR__, 'extlib', 'pathname'))
require File.expand_path(File.join(File.dirname(__FILE__), 'extlib', 'pathname'))

dir = Pathname(__FILE__).dirname.expand_path / 'extlib'

Expand Down Expand Up @@ -51,7 +48,8 @@ def self.exiting= bool
end

def self.exiting
@exiting
return @exiting if defined?(@exiting)
@exiting = false
end

end
6 changes: 4 additions & 2 deletions lib/extlib/class.rb
Expand Up @@ -115,12 +115,14 @@ def class_inheritable_reader(*ivars)
ivars.each do |ivar|
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{ivar}
return @#{ivar} if self.object_id == #{self.object_id} || defined?(@#{ivar})
return @#{ivar} if defined?(@#{ivar})
return nil if self.object_id == #{self.object_id}
ivar = superclass.#{ivar}
return nil if ivar.nil? && !#{self}.instance_variable_defined?("@#{ivar}")
return nil if ivar.nil?
@#{ivar} = ivar.try_dup
end
RUBY

unless instance_reader == false
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{ivar}
Expand Down
2 changes: 2 additions & 0 deletions lib/extlib/datetime.rb
Expand Up @@ -9,6 +9,7 @@ class DateTime
# @return [Time] Time object representing the same moment as receiver
#
# @api public
remove_method :to_time if method_defined?(:to_time) || private_method_defined?(:to_time)
def to_time
Time.parse self.to_s
end
Expand All @@ -21,6 +22,7 @@ def to_time
# @return [DateTime] Receiver
#
# @api public
remove_method :to_datetime if method_defined?(:to_datetime) || private_method_defined?(:to_datetime)
def to_datetime
self
end
Expand Down
8 changes: 4 additions & 4 deletions lib/extlib/hash.rb
Expand Up @@ -131,11 +131,11 @@ def normalize_param(key, value)
end

stack.each do |parent, hash|
hash.each do |key, value|
if value.is_a?(Hash)
stack << ["#{parent}[#{key}]", value]
hash.each do |k, v|
if v.is_a?(Hash)
stack << ["#{parent}[#{k}]", v]
else
param << normalize_param("#{parent}[#{key}]", value)
param << normalize_param("#{parent}[#{k}]", v)
end
end
end
Expand Down
40 changes: 28 additions & 12 deletions lib/extlib/hook.rb
Expand Up @@ -235,21 +235,26 @@ def define_hook_stack_execution_methods(target_method, scope)
after_hooks = hooks[target_method][:after]
after_hooks = after_hooks.map{ |info| inline_call(info, scope) }.join("\n")

source = %{
before_hook_name = hook_method_name(target_method, 'execute_before', 'hook_stack')
after_hook_name = hook_method_name(target_method, 'execute_after', 'hook_stack')

hooks[target_method][:in].class_eval <<-RUBY, __FILE__, __LINE__ + 1
#{scope == :class ? 'class << self' : ''}
private
def #{hook_method_name(target_method, 'execute_before', 'hook_stack')}(*args)
remove_method :#{before_hook_name} if method_defined?(:#{before_hook_name}) || private_method_defined?(:#{before_hook_name})
def #{before_hook_name}(*args)
#{before_hooks}
end
def #{hook_method_name(target_method, 'execute_after', 'hook_stack')}(*args)
remove_method :#{after_hook_name} if method_defined?(:#{after_hook_name}) || private_method_defined?(:#{after_hook_name})
def #{after_hook_name}(*args)
#{after_hooks}
end
}
source = %{class << self\n#{source}\nend} if scope == :class

hooks[target_method][:in].class_eval(source, __FILE__, __LINE__ - 12)
#{scope == :class ? 'end' : ''}
RUBY
end

# Returns ruby code that will invoke the hook. It checks the arity of the hook method
Expand Down Expand Up @@ -321,14 +326,25 @@ def install_hook(type, target_method, method_sym, scope, &block)
#if this hook is previously declared in a sibling or cousin we must move the :in class
#to the common ancestor to get both hooks to run.
if !(hooks[target_method][:in] <=> self)
hooks[target_method][:in].class_eval(
%{def #{hook_method_name(target_method, 'execute_before', 'hook_stack')}(*args);super;end\n} +
%{def #{hook_method_name(target_method, 'execute_after', 'hook_stack')}(*args);super;end},
__FILE__,__LINE__ - 2
)
before_hook_name = hook_method_name(target_method, 'execute_before', 'hook_stack')
after_hook_name = hook_method_name(target_method, 'execute_after', 'hook_stack')

hooks[target_method][:in].class_eval <<-RUBY, __FILE__, __LINE__ + 1
remove_method :#{before_hook_name} if method_defined?(:#{before_hook_name}) || private_method_defined?(:#{before_hook_name})
def #{before_hook_name}(*args)
super
end
remove_method :#{after_hook_name} if method_defined?(:#{after_hook_name}) || private_method_defined?(:#{after_hook_name})
def #{after_hook_name}(*args)
super
end
RUBY

while !(hooks[target_method][:in] <=> self) do
hooks[target_method][:in] = hooks[target_method][:in].superclass
end

define_hook_stack_execution_methods(target_method, scope)
hooks[target_method][:in].class_eval{define_advised_method(target_method, scope)}
end
Expand Down
124 changes: 60 additions & 64 deletions lib/extlib/lazy_array.rb
@@ -1,23 +1,6 @@
class LazyArray # borrowed partially from StrokeDB
instance_methods.each { |m| undef_method m unless %w[ __id__ __send__ send class dup object_id kind_of? respond_to? equal? assert_kind_of should should_not instance_variable_set instance_variable_get extend ].include?(m.to_s) }

# add proxies for all Array and Enumerable methods
(Array.instance_methods(false) | Enumerable.instance_methods(false)).each do |method|
target = if method.to_s[-1, 1] == '='
"send(:#{method}, *args, &block)"
else
"#{method}(*args, &block)"
end

class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(*args, &block) # def []=(*args, &block)
lazy_load # lazy_load
results = @array.#{target} # results = @array.send(:[]=, *args, &block)
results.equal?(@array) ? self : results # results.equal?(@array) ? self : results
end # end
RUBY
end

def first(*args)
if lazy_possible?(@head, *args)
@head.first(*args)
Expand Down Expand Up @@ -265,7 +248,6 @@ def delete_if(&block)
lazy_load
@array.delete_if(&block)
else
@reapers ||= []
@reapers << block
@head.delete_if(&block)
@tail.delete_if(&block)
Expand Down Expand Up @@ -326,62 +308,31 @@ def frozen?
@frozen == true
end

def equal?(other)
object_id == other.object_id
end

def eql?(other)
return true if equal?(other)
return false unless other.class.equal?(self.class)

unless loaded?
# compare the head against the beginning of other. start at index
# 0 and incrementally compare each entry. if other is a LazyArray
# this has a lesser likelyhood of triggering a lazy load
0.upto(@head.size - 1) do |i|
return false unless @head[i].eql?(other[i])
end

# compare the tail against the end of other. start at index
# -1 and decrementally compare each entry. if other is a LazyArray
# this has a lesser likelyhood of triggering a lazy load
-1.downto(@tail.size * -1) do |i|
return false unless @tail[i].eql?(other[i])
end

lazy_load
def ==(other)
if equal?(other)
return true
end

# convert other to an Array before checking equality
@array.eql?(other.to_ary)
end

def ==(other)
return true if equal?(other)
return false unless other.respond_to?(:to_ary)
unless other.respond_to?(:to_ary)
return false
end

# if necessary, convert to something that can be compared
other = other.to_ary unless other.respond_to?(:[])

unless loaded?
# compare the head against the beginning of other. start at index
# 0 and incrementally compare each entry. if other is a LazyArray
# this has a lesser likelyhood of triggering a lazy load
0.upto(@head.size - 1) do |i|
return false unless @head[i] == other[i]
end
cmp?(other, :==)
end

# compare the tail against the end of other. start at index
# -1 and decrementally compare each entry. if other is a LazyArray
# this has a lesser likelyhood of triggering a lazy load
-1.downto(@tail.size * -1) do |i|
return false unless @tail[i] == other[i]
end
def eql?(other)
if equal?(other)
return true
end

lazy_load
unless other.class.equal?(self.class)
return false
end

@array == other
cmp?(other, :eql?)
end

protected
Expand All @@ -395,10 +346,13 @@ def lazy_possible?(list, need_length = 1)
private

def initialize
@frozen = false
@loaded = false
@load_with_proc = lambda { |v| v }
@head = []
@tail = []
@array = []
@reapers = []
end

def initialize_copy(original)
Expand Down Expand Up @@ -468,4 +422,46 @@ def method_missing(method, *args, &block)
super
end
end

def cmp?(other, operator)
unless loaded?
# compare the head against the beginning of other. start at index
# 0 and incrementally compare each entry. if other is a LazyArray
# this has a lesser likelyhood of triggering a lazy load
0.upto(@head.size - 1) do |i|
return false unless @head[i].send(operator, other[i])
end

# compare the tail against the end of other. start at index
# -1 and decrementally compare each entry. if other is a LazyArray
# this has a lesser likelyhood of triggering a lazy load
-1.downto(@tail.size * -1) do |i|
return false unless @tail[i].send(operator, other[i])
end

lazy_load
end

@array.send(operator, other.to_ary)
end

# add proxies for all remaining Array and Enumerable methods
(Array.public_instance_methods(false) | Enumerable.public_instance_methods(false)).each do |method|
next if public_method_defined?(method)

target = if method.to_s[-1, 1] == '='
"send(:#{method}, *args, &block)"
else
"#{method}(*args, &block)"
end

class_eval <<-RUBY, __FILE__, __LINE__ + 1
public
def #{method}(*args, &block) # def []=(*args, &block)
lazy_load # lazy_load
results = @array.#{target} # results = @array.send(:[]=, *args, &block)
results.equal?(@array) ? self : results # results.equal?(@array) ? self : results
end # end
RUBY
end
end
2 changes: 1 addition & 1 deletion lib/extlib/object.rb
Expand Up @@ -97,7 +97,7 @@ def make_module(str)
current_module = self
mod.each do |x|
unless current_module.const_defined?(x)
current_module.class_eval "module #{x}; end"
current_module.class_eval "module #{x}; end", __FILE__, __LINE__
end
current_module = current_module.const_get(x)
end
Expand Down
10 changes: 5 additions & 5 deletions lib/extlib/pooling.rb
Expand Up @@ -26,11 +26,11 @@ module Extlib
module Pooling

def self.scavenger?
@scavenger && @scavenger.alive?
defined?(@scavenger) && !@scavenger.nil? && @scavenger.alive?
end

def self.scavenger
if @scavenger.nil? || !@scavenger.alive?
unless scavenger?
@scavenger = Thread.new do
running = true
while running do
Expand Down Expand Up @@ -72,7 +72,7 @@ def self.append_pool(pool)
lock.synchronize do
pools << pool
end
Extlib::Pooling::scavenger
Extlib::Pooling.scavenger
end

def self.lock
Expand Down Expand Up @@ -136,7 +136,7 @@ def initialize(max_size, resource, args)

@available = []
@used = {}
Extlib::Pooling::append_pool(self)
Extlib::Pooling.append_pool(self)
end

def lock
Expand Down Expand Up @@ -213,7 +213,7 @@ def flush!
def dispose
flush!
@resource.__pools.delete(@args)
!Extlib::Pooling::pools.delete?(self).nil?
!Extlib::Pooling.pools.delete?(self).nil?
end

def expired?
Expand Down
2 changes: 2 additions & 0 deletions lib/extlib/time.rb
Expand Up @@ -23,6 +23,7 @@ def to_json(*)
# @return [Time] Receiver
#
# @api public
remove_method :to_time if method_defined?(:to_time) || private_method_defined?(:to_time)
def to_time
self
end
Expand All @@ -35,6 +36,7 @@ def to_time
# @return [DateTime] DateTime object representing the same moment as receiver
#
# @api public
remove_method :to_datetime if method_defined?(:to_datetime) || private_method_defined?(:to_datetime)
def to_datetime
DateTime.parse self.to_s
end
Expand Down

0 comments on commit 74c7e26

Please sign in to comment.