activesupport 1.4.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- data/CHANGELOG +263 -7
- data/lib/active_support.rb +9 -4
- data/lib/active_support/basic_object.rb +5 -0
- data/lib/active_support/buffered_logger.rb +107 -0
- data/lib/active_support/clean_logger.rb +94 -5
- data/lib/active_support/core_ext.rb +4 -1
- data/lib/active_support/core_ext/array.rb +8 -2
- data/lib/active_support/core_ext/array/access.rb +28 -0
- data/lib/active_support/core_ext/array/conversions.rb +28 -15
- data/lib/active_support/core_ext/array/extract_options.rb +19 -0
- data/lib/active_support/core_ext/array/grouping.rb +20 -7
- data/lib/active_support/core_ext/array/random_access.rb +12 -0
- data/lib/active_support/core_ext/bigdecimal.rb +1 -2
- data/lib/active_support/core_ext/bigdecimal/{formatting.rb → conversions.rb} +1 -2
- data/lib/active_support/core_ext/blank.rb +2 -8
- data/lib/active_support/core_ext/cgi.rb +2 -2
- data/lib/active_support/core_ext/class.rb +4 -3
- data/lib/active_support/core_ext/class/attribute_accessors.rb +1 -1
- data/lib/active_support/core_ext/class/delegating_attributes.rb +40 -0
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +3 -3
- data/lib/active_support/core_ext/class/removal.rb +2 -2
- data/lib/active_support/core_ext/date.rb +5 -1
- data/lib/active_support/core_ext/date/behavior.rb +13 -0
- data/lib/active_support/core_ext/date/calculations.rb +188 -0
- data/lib/active_support/core_ext/date/conversions.rb +69 -13
- data/lib/active_support/core_ext/date_time.rb +10 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +77 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +54 -0
- data/lib/active_support/core_ext/duplicable.rb +37 -0
- data/lib/active_support/core_ext/enumerable.rb +1 -0
- data/lib/active_support/core_ext/exception.rb +2 -2
- data/lib/active_support/core_ext/file.rb +21 -0
- data/lib/active_support/core_ext/float.rb +5 -0
- data/lib/active_support/core_ext/float/rounding.rb +24 -0
- data/lib/active_support/core_ext/hash.rb +5 -5
- data/lib/active_support/core_ext/hash/conversions.rb +86 -34
- data/lib/active_support/core_ext/hash/diff.rb +8 -0
- data/lib/active_support/core_ext/hash/except.rb +24 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +15 -2
- data/lib/active_support/core_ext/hash/keys.rb +10 -3
- data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -2
- data/lib/active_support/core_ext/hash/slice.rb +28 -0
- data/lib/active_support/core_ext/integer.rb +2 -2
- data/lib/active_support/core_ext/kernel.rb +5 -4
- data/lib/active_support/core_ext/kernel/debugger.rb +13 -0
- data/lib/active_support/core_ext/module.rb +8 -7
- data/lib/active_support/core_ext/module/aliasing.rb +17 -5
- data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +31 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +1 -1
- data/lib/active_support/core_ext/module/delegation.rb +21 -0
- data/lib/active_support/core_ext/name_error.rb +2 -2
- data/lib/active_support/core_ext/numeric.rb +2 -2
- data/lib/active_support/core_ext/numeric/time.rb +30 -11
- data/lib/active_support/core_ext/object.rb +3 -2
- data/lib/active_support/core_ext/object/extending.rb +40 -29
- data/lib/active_support/core_ext/object/instance_variables.rb +22 -0
- data/lib/active_support/core_ext/object/misc.rb +29 -4
- data/lib/active_support/core_ext/pathname.rb +1 -1
- data/lib/active_support/core_ext/range.rb +7 -1
- data/lib/active_support/core_ext/range/blockless_step.rb +22 -0
- data/lib/active_support/core_ext/range/conversions.rb +8 -6
- data/lib/active_support/core_ext/range/include_range.rb +22 -0
- data/lib/active_support/core_ext/range/overlaps.rb +12 -0
- data/lib/active_support/core_ext/string.rb +10 -7
- data/lib/active_support/core_ext/string/conversions.rb +5 -1
- data/lib/active_support/core_ext/string/unicode.rb +2 -2
- data/lib/active_support/core_ext/string/xchar.rb +11 -0
- data/lib/active_support/core_ext/symbol.rb +12 -10
- data/lib/active_support/core_ext/test.rb +1 -0
- data/lib/active_support/core_ext/test/unit/assertions.rb +62 -0
- data/lib/active_support/core_ext/time.rb +4 -2
- data/lib/active_support/core_ext/time/behavior.rb +13 -0
- data/lib/active_support/core_ext/time/calculations.rb +87 -54
- data/lib/active_support/core_ext/time/conversions.rb +71 -10
- data/lib/active_support/dependencies.rb +25 -24
- data/lib/active_support/deprecation.rb +4 -2
- data/lib/active_support/duration.rb +86 -0
- data/lib/active_support/inflections.rb +2 -1
- data/lib/active_support/inflector.rb +13 -6
- data/lib/active_support/json.rb +22 -39
- data/lib/active_support/json/decoding.rb +60 -0
- data/lib/active_support/json/encoders/date.rb +5 -0
- data/lib/active_support/json/encoders/date_time.rb +5 -0
- data/lib/active_support/json/encoders/enumerable.rb +12 -0
- data/lib/active_support/json/encoders/false_class.rb +5 -0
- data/lib/active_support/json/encoders/hash.rb +50 -0
- data/lib/active_support/json/encoders/nil_class.rb +5 -0
- data/lib/active_support/json/encoders/numeric.rb +5 -0
- data/lib/active_support/json/encoders/object.rb +6 -0
- data/lib/active_support/json/encoders/regexp.rb +5 -0
- data/lib/active_support/json/encoders/string.rb +30 -0
- data/lib/active_support/json/encoders/symbol.rb +5 -0
- data/lib/active_support/json/encoders/time.rb +5 -0
- data/lib/active_support/json/encoders/true_class.rb +5 -0
- data/lib/active_support/json/encoding.rb +38 -0
- data/lib/active_support/json/variable.rb +10 -0
- data/lib/active_support/multibyte.rb +7 -5
- data/lib/active_support/multibyte/chars.rb +6 -0
- data/lib/active_support/multibyte/handlers/utf8_handler.rb +115 -5
- data/lib/active_support/option_merger.rb +7 -7
- data/lib/active_support/ordered_options.rb +22 -17
- data/lib/active_support/test_case.rb +5 -0
- data/lib/active_support/testing.rb +1 -0
- data/lib/active_support/testing/default.rb +12 -0
- data/lib/active_support/values/time_zone.rb +3 -3
- data/lib/active_support/vendor.rb +14 -0
- data/lib/active_support/vendor/builder-2.1.2/blankslate.rb +113 -0
- data/lib/active_support/vendor/{builder.rb → builder-2.1.2/builder.rb} +0 -0
- data/lib/active_support/vendor/builder-2.1.2/builder/blankslate.rb +20 -0
- data/lib/active_support/vendor/builder-2.1.2/builder/css.rb +250 -0
- data/lib/active_support/vendor/{builder → builder-2.1.2/builder}/xchar.rb +11 -8
- data/lib/active_support/vendor/{builder → builder-2.1.2/builder}/xmlbase.rb +38 -44
- data/lib/active_support/vendor/{builder → builder-2.1.2/builder}/xmlevents.rb +1 -1
- data/lib/active_support/vendor/{builder → builder-2.1.2/builder}/xmlmarkup.rb +40 -39
- data/lib/active_support/vendor/{xml_simple.rb → xml-simple-1.0.11/xmlsimple.rb} +3 -3
- data/lib/active_support/version.rb +3 -3
- data/lib/active_support/whiny_nil.rb +12 -12
- data/lib/activesupport.rb +1 -0
- metadata +69 -17
- data/lib/active_support/binding_of_caller.rb +0 -84
- data/lib/active_support/breakpoint.rb +0 -528
- data/lib/active_support/caching_tools.rb +0 -62
- data/lib/active_support/json/encoders.rb +0 -25
- data/lib/active_support/json/encoders/core.rb +0 -70
- data/lib/active_support/reloadable.rb +0 -60
- data/lib/active_support/vendor/builder/blankslate.rb +0 -63
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'active_support/core_ext/integer/even_odd'
|
2
|
+
require 'active_support/core_ext/integer/inflections'
|
3
3
|
|
4
4
|
class Integer #:nodoc:
|
5
5
|
include ActiveSupport::CoreExtensions::Integer::EvenOdd
|
@@ -1,4 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require 'active_support/core_ext/kernel/daemonizing'
|
2
|
+
require 'active_support/core_ext/kernel/reporting'
|
3
|
+
require 'active_support/core_ext/kernel/agnostics'
|
4
|
+
require 'active_support/core_ext/kernel/requires'
|
5
|
+
require 'active_support/core_ext/kernel/debugger'
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Kernel
|
2
|
+
unless respond_to?(:debugger)
|
3
|
+
# Starts a debugging session if ruby-debug has been loaded (call script/server --debugger to do load it).
|
4
|
+
def debugger
|
5
|
+
RAILS_DEFAULT_LOGGER.info "\n***** Debugger requested, but was not available: Start server with --debugger to enable *****\n"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def breakpoint
|
10
|
+
RAILS_DEFAULT_LOGGER.info "\n***** The 'breakpoint' command has been renamed 'debugger' -- please change *****\n"
|
11
|
+
debugger
|
12
|
+
end
|
13
|
+
end
|
@@ -1,7 +1,8 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
1
|
+
require 'active_support/core_ext/module/inclusion'
|
2
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
3
|
+
require 'active_support/core_ext/module/attr_internal'
|
4
|
+
require 'active_support/core_ext/module/attr_accessor_with_default'
|
5
|
+
require 'active_support/core_ext/module/delegation'
|
6
|
+
require 'active_support/core_ext/module/introspection'
|
7
|
+
require 'active_support/core_ext/module/loading'
|
8
|
+
require 'active_support/core_ext/module/aliasing'
|
@@ -25,8 +25,20 @@ class Module
|
|
25
25
|
# e.g. target?_without_feature is not a valid method name.
|
26
26
|
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
|
27
27
|
yield(aliased_target, punctuation) if block_given?
|
28
|
-
|
29
|
-
|
28
|
+
|
29
|
+
with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
|
30
|
+
|
31
|
+
alias_method without_method, target
|
32
|
+
alias_method target, with_method
|
33
|
+
|
34
|
+
case
|
35
|
+
when public_method_defined?(without_method)
|
36
|
+
public target
|
37
|
+
when protected_method_defined?(without_method)
|
38
|
+
protected target
|
39
|
+
when private_method_defined?(without_method)
|
40
|
+
private target
|
41
|
+
end
|
30
42
|
end
|
31
43
|
|
32
44
|
# Allows you to make aliases for attributes, which includes
|
@@ -38,7 +50,7 @@ class Module
|
|
38
50
|
# # has a title attribute
|
39
51
|
# end
|
40
52
|
#
|
41
|
-
# class Email <
|
53
|
+
# class Email < Content
|
42
54
|
# alias_attribute :subject, :title
|
43
55
|
# end
|
44
56
|
#
|
@@ -50,8 +62,8 @@ class Module
|
|
50
62
|
# e.title # => "Megastars"
|
51
63
|
def alias_attribute(new_name, old_name)
|
52
64
|
module_eval <<-STR, __FILE__, __LINE__+1
|
53
|
-
def #{new_name};
|
54
|
-
def #{new_name}?;
|
65
|
+
def #{new_name}; self.#{old_name}; end
|
66
|
+
def #{new_name}?; self.#{old_name}?; end
|
55
67
|
def #{new_name}=(v); self.#{old_name} = v; end
|
56
68
|
STR
|
57
69
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Module
|
2
|
+
# Declare an attribute accessor with an initial default return value.
|
3
|
+
#
|
4
|
+
# To give attribute <tt>:age</tt> the initial value <tt>25</tt>:
|
5
|
+
#
|
6
|
+
# class Person
|
7
|
+
# attr_accessor_with_default :age, 25
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# some_person.age
|
11
|
+
# => 25
|
12
|
+
# some_person.age = 26
|
13
|
+
# some_person.age
|
14
|
+
# => 26
|
15
|
+
#
|
16
|
+
# To give attribute <tt>:element_name</tt> a dynamic default value, evaluated
|
17
|
+
# in scope of self:
|
18
|
+
#
|
19
|
+
# attr_accessor_with_default(:element_name) { name.underscore }
|
20
|
+
#
|
21
|
+
def attr_accessor_with_default(sym, default = nil, &block)
|
22
|
+
raise 'Default value or block required' unless !default.nil? || block
|
23
|
+
define_method(sym, block_given? ? block : Proc.new { default })
|
24
|
+
module_eval(<<-EVAL, __FILE__, __LINE__)
|
25
|
+
def #{sym}=(value)
|
26
|
+
class << self; attr_reader :#{sym} end
|
27
|
+
@#{sym} = value
|
28
|
+
end
|
29
|
+
EVAL
|
30
|
+
end
|
31
|
+
end
|
@@ -5,6 +5,7 @@ class Module
|
|
5
5
|
# or string). At least one method and the :to option are required.
|
6
6
|
#
|
7
7
|
# Delegation is particularly useful with Active Record associations:
|
8
|
+
#
|
8
9
|
# class Greeter < ActiveRecord::Base
|
9
10
|
# def hello() "hello" end
|
10
11
|
# def goodbye() "goodbye" end
|
@@ -20,10 +21,30 @@ class Module
|
|
20
21
|
#
|
21
22
|
# Multiple delegates to the same target are allowed:
|
22
23
|
# class Foo < ActiveRecord::Base
|
24
|
+
# belongs_to :greeter
|
23
25
|
# delegate :hello, :goodbye, :to => :greeter
|
24
26
|
# end
|
25
27
|
#
|
26
28
|
# Foo.new.goodbye # => "goodbye"
|
29
|
+
#
|
30
|
+
# Methods can be delegated to instance variables, class variables, or constants
|
31
|
+
# by providing the variable as a symbol:
|
32
|
+
# class Foo
|
33
|
+
# CONSTANT_ARRAY = [0,1,2,3]
|
34
|
+
# @@class_array = [4,5,6,7]
|
35
|
+
#
|
36
|
+
# def initialize
|
37
|
+
# @instance_array = [8,9,10,11]
|
38
|
+
# end
|
39
|
+
# delegate :sum, :to => :CONSTANT_ARRAY
|
40
|
+
# delegate :min, :to => :@@class_array
|
41
|
+
# delegate :max, :to => :@instance_array
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# Foo.new.sum # => 6
|
45
|
+
# Foo.new.min # => 4
|
46
|
+
# Foo.new.max # => 11
|
47
|
+
#
|
27
48
|
def delegate(*methods)
|
28
49
|
options = methods.pop
|
29
50
|
unless options.is_a?(Hash) && to = options[:to]
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# Add a +missing_name+ method to NameError instances.
|
2
|
-
class NameError
|
2
|
+
class NameError #:nodoc:
|
3
3
|
# Add a method to obtain the missing name from a NameError.
|
4
4
|
def missing_name
|
5
5
|
$1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ message
|
@@ -14,4 +14,4 @@ class NameError < StandardError #:nodoc:
|
|
14
14
|
missing_name == name.to_s
|
15
15
|
end
|
16
16
|
end
|
17
|
-
end
|
17
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'active_support/core_ext/numeric/time'
|
2
|
+
require 'active_support/core_ext/numeric/bytes'
|
3
3
|
|
4
4
|
class Numeric #:nodoc:
|
5
5
|
include ActiveSupport::CoreExtensions::Numeric::Time
|
@@ -3,51 +3,70 @@ module ActiveSupport #:nodoc:
|
|
3
3
|
module Numeric #:nodoc:
|
4
4
|
# Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
|
5
5
|
#
|
6
|
-
#
|
7
|
-
#
|
6
|
+
# These methods use Time#advance for precise date calculations when using from_now, ago, etc.
|
7
|
+
# as well as adding or subtracting their results from a Time object. For example:
|
8
|
+
#
|
9
|
+
# # equivalent to Time.now.advance(:months => 1)
|
10
|
+
# 1.month.from_now
|
11
|
+
#
|
12
|
+
# # equivalent to Time.now.advance(:years => 2)
|
13
|
+
# 2.years.from_now
|
14
|
+
#
|
15
|
+
# # equivalent to Time.now.advance(:months => 4, :years => 5)
|
16
|
+
# (4.months + 5.years).from_now
|
8
17
|
#
|
9
|
-
#
|
18
|
+
# While these methods provide precise calculation when used as in the examples above, care
|
19
|
+
# should be taken to note that this is not true if the result of `months', `years', etc is
|
20
|
+
# converted before use:
|
21
|
+
#
|
22
|
+
# # equivalent to 30.days.to_i.from_now
|
23
|
+
# 1.month.to_i.from_now
|
24
|
+
#
|
25
|
+
# # equivalent to 365.25.days.to_f.from_now
|
26
|
+
# 1.year.to_f.from_now
|
27
|
+
#
|
28
|
+
# In such cases, Ruby's core
|
10
29
|
# Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and
|
11
30
|
# Time[http://stdlib.rubyonrails.org/libdoc/time/rdoc/index.html] should be used for precision
|
12
31
|
# date and time arithmetic
|
13
32
|
module Time
|
14
33
|
def seconds
|
15
|
-
self
|
34
|
+
ActiveSupport::Duration.new(self, [[:seconds, self]])
|
16
35
|
end
|
17
36
|
alias :second :seconds
|
18
37
|
|
19
38
|
def minutes
|
20
|
-
self * 60
|
39
|
+
ActiveSupport::Duration.new(self * 60, [[:seconds, self * 60]])
|
21
40
|
end
|
22
41
|
alias :minute :minutes
|
23
42
|
|
24
43
|
def hours
|
25
|
-
self *
|
44
|
+
ActiveSupport::Duration.new(self * 3600, [[:seconds, self * 3600]])
|
26
45
|
end
|
27
46
|
alias :hour :hours
|
28
47
|
|
29
48
|
def days
|
30
|
-
self * 24.hours
|
49
|
+
ActiveSupport::Duration.new(self * 24.hours, [[:days, self]])
|
31
50
|
end
|
32
51
|
alias :day :days
|
33
52
|
|
34
53
|
def weeks
|
35
|
-
self * 7.days
|
54
|
+
ActiveSupport::Duration.new(self * 7.days, [[:days, self * 7]])
|
36
55
|
end
|
37
56
|
alias :week :weeks
|
38
57
|
|
39
58
|
def fortnights
|
40
|
-
self * 2.weeks
|
59
|
+
ActiveSupport::Duration.new(self * 2.weeks, [[:days, self * 14]])
|
41
60
|
end
|
42
61
|
alias :fortnight :fortnights
|
43
62
|
|
44
63
|
def months
|
45
|
-
self * 30.days
|
64
|
+
ActiveSupport::Duration.new(self * 30.days, [[:months, self]])
|
46
65
|
end
|
47
66
|
alias :month :months
|
48
67
|
|
49
68
|
def years
|
50
|
-
(self * 365.25.days)
|
69
|
+
ActiveSupport::Duration.new(self * 365.25.days, [[:years, self]])
|
51
70
|
end
|
52
71
|
alias :year :years
|
53
72
|
|
@@ -1,2 +1,3 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'active_support/core_ext/object/extending'
|
2
|
+
require 'active_support/core_ext/object/instance_variables'
|
3
|
+
require 'active_support/core_ext/object/misc'
|
@@ -1,47 +1,58 @@
|
|
1
|
-
class Object
|
2
|
-
def remove_subclasses_of(*superclasses)
|
1
|
+
class Object
|
2
|
+
def remove_subclasses_of(*superclasses) #:nodoc:
|
3
3
|
Class.remove_class(*subclasses_of(*superclasses))
|
4
4
|
end
|
5
5
|
|
6
|
-
def subclasses_of(*superclasses)
|
6
|
+
def subclasses_of(*superclasses) #:nodoc:
|
7
7
|
subclasses = []
|
8
|
+
|
9
|
+
# Exclude this class unless it's a subclass of our supers and is defined.
|
10
|
+
# We check defined? in case we find a removed class that has yet to be
|
11
|
+
# garbage collected. This also fails for anonymous classes -- please
|
12
|
+
# submit a patch if you have a workaround.
|
8
13
|
ObjectSpace.each_object(Class) do |k|
|
9
|
-
|
10
|
-
(k.
|
11
|
-
|
12
|
-
|
13
|
-
eval("::#{k}").object_id != k.object_id
|
14
|
-
subclasses << k
|
14
|
+
if superclasses.any? { |superclass| k < superclass } &&
|
15
|
+
(k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id"))
|
16
|
+
subclasses << k
|
17
|
+
end
|
15
18
|
end
|
19
|
+
|
16
20
|
subclasses
|
17
21
|
end
|
18
|
-
|
19
|
-
def extended_by
|
22
|
+
|
23
|
+
def extended_by #:nodoc:
|
20
24
|
ancestors = class << self; ancestors end
|
21
25
|
ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
|
22
26
|
end
|
23
|
-
|
24
|
-
def
|
25
|
-
exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables
|
26
|
-
|
27
|
-
instance_variables = object.instance_variables - exclude.map { |name| name.to_s }
|
28
|
-
instance_variables.each { |name| instance_variable_set(name, object.instance_variable_get(name)) }
|
29
|
-
end
|
30
|
-
|
31
|
-
def extend_with_included_modules_from(object)
|
27
|
+
|
28
|
+
def extend_with_included_modules_from(object) #:nodoc:
|
32
29
|
object.extended_by.each { |mod| extend mod }
|
33
30
|
end
|
34
31
|
|
35
|
-
def instance_values
|
36
|
-
instance_variables.inject({}) do |values, name|
|
37
|
-
values[name[1..-1]] = instance_variable_get(name)
|
38
|
-
values
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
32
|
unless defined? instance_exec # 1.9
|
43
|
-
|
44
|
-
|
33
|
+
module InstanceExecMethods #:nodoc:
|
34
|
+
end
|
35
|
+
include InstanceExecMethods
|
36
|
+
|
37
|
+
# Evaluate the block with the given arguments within the context of
|
38
|
+
# this object, so self is set to the method receiver.
|
39
|
+
#
|
40
|
+
# From Mauricio's http://eigenclass.org/hiki/bounded+space+instance_exec
|
41
|
+
def instance_exec(*args, &block)
|
42
|
+
begin
|
43
|
+
old_critical, Thread.critical = Thread.critical, true
|
44
|
+
n = 0
|
45
|
+
n += 1 while respond_to?(method_name = "__instance_exec#{n}")
|
46
|
+
InstanceExecMethods.module_eval { define_method(method_name, &block) }
|
47
|
+
ensure
|
48
|
+
Thread.critical = old_critical
|
49
|
+
end
|
50
|
+
|
51
|
+
begin
|
52
|
+
send(method_name, *args)
|
53
|
+
ensure
|
54
|
+
InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
|
55
|
+
end
|
45
56
|
end
|
46
57
|
end
|
47
58
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Object
|
2
|
+
# Available in 1.8.6 and later.
|
3
|
+
unless respond_to?(:instance_variable_defined?)
|
4
|
+
def instance_variable_defined?(variable)
|
5
|
+
instance_variables.include?(variable.to_s)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def instance_values #:nodoc:
|
10
|
+
instance_variables.inject({}) do |values, name|
|
11
|
+
values[name.to_s[1..-1]] = instance_variable_get(name)
|
12
|
+
values
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def copy_instance_variables_from(object, exclude = []) #:nodoc:
|
17
|
+
exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables
|
18
|
+
|
19
|
+
vars = object.instance_variables.map(&:to_s) - exclude.map(&:to_s)
|
20
|
+
vars.each { |name| instance_variable_set(name, object.instance_variable_get(name)) }
|
21
|
+
end
|
22
|
+
end
|
@@ -1,4 +1,9 @@
|
|
1
|
-
class Object
|
1
|
+
class Object
|
2
|
+
unless respond_to?(:send!)
|
3
|
+
# Anticipating Ruby 1.9 neutering send
|
4
|
+
alias send! send
|
5
|
+
end
|
6
|
+
|
2
7
|
# A Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.
|
3
8
|
#
|
4
9
|
# def foo
|
@@ -24,11 +29,31 @@ class Object #:nodoc:
|
|
24
29
|
value
|
25
30
|
end
|
26
31
|
|
32
|
+
# An elegant way to refactor out common options
|
33
|
+
#
|
34
|
+
# with_options :order => 'created_at', :class_name => 'Comment' do |post|
|
35
|
+
# post.has_many :comments, :conditions => ['approved = ?', true], :dependent => :delete_all
|
36
|
+
# post.has_many :unapproved_comments, :conditions => ['approved = ?', false]
|
37
|
+
# post.has_many :all_comments
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# Can also be used with an explicit receiver:
|
41
|
+
#
|
42
|
+
# map.with_options :controller => "people" do |people|
|
43
|
+
# people.connect "/people", :action => "index"
|
44
|
+
# people.connect "/people/:id", :action => "show"
|
45
|
+
# end
|
46
|
+
#
|
27
47
|
def with_options(options)
|
28
48
|
yield ActiveSupport::OptionMerger.new(self, options)
|
29
49
|
end
|
30
50
|
|
31
|
-
|
32
|
-
|
51
|
+
# A duck-type assistant method. For example, ActiveSupport extends Date
|
52
|
+
# to define an acts_like_date? method, and extends Time to define
|
53
|
+
# acts_like_time?. As a result, we can do "x.acts_like?(:time)" and
|
54
|
+
# "x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that
|
55
|
+
# we want to act like Time simply need to define an acts_like_time? method.
|
56
|
+
def acts_like?(duck)
|
57
|
+
respond_to? "acts_like_#{duck}?"
|
33
58
|
end
|
34
|
-
end
|
59
|
+
end
|