activesupport 3.1.0 → 5.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +798 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +13 -7
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +38 -34
- data/lib/active_support/benchmarkable.rb +17 -28
- data/lib/active_support/cache/file_store.rb +85 -70
- data/lib/active_support/cache/mem_cache_store.rb +75 -66
- data/lib/active_support/cache/memory_store.rb +31 -23
- data/lib/active_support/cache/null_store.rb +41 -0
- data/lib/active_support/cache/strategy/local_cache.rb +73 -70
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
- data/lib/active_support/cache.rb +360 -294
- data/lib/active_support/callbacks.rb +563 -393
- data/lib/active_support/concern.rb +42 -34
- data/lib/active_support/concurrency/latch.rb +19 -0
- data/lib/active_support/concurrency/share_lock.rb +186 -0
- data/lib/active_support/configurable.rb +70 -12
- data/lib/active_support/core_ext/array/access.rb +53 -9
- data/lib/active_support/core_ext/array/conversions.rb +109 -62
- data/lib/active_support/core_ext/array/extract_options.rb +2 -2
- data/lib/active_support/core_ext/array/grouping.rb +39 -32
- data/lib/active_support/core_ext/array/inquiry.rb +17 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
- data/lib/active_support/core_ext/array/wrap.rb +16 -18
- data/lib/active_support/core_ext/array.rb +2 -2
- data/lib/active_support/core_ext/benchmark.rb +7 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -36
- data/lib/active_support/core_ext/class/attribute.rb +47 -34
- data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -79
- data/lib/active_support/core_ext/class/subclasses.rb +12 -7
- data/lib/active_support/core_ext/class.rb +0 -3
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +57 -167
- data/lib/active_support/core_ext/date/conversions.rb +31 -42
- data/lib/active_support/core_ext/date/zones.rb +2 -10
- data/lib/active_support/core_ext/date.rb +5 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +1 -0
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +132 -65
- data/lib/active_support/core_ext/date_time/compatibility.rb +5 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +36 -34
- data/lib/active_support/core_ext/date_time.rb +5 -0
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +81 -74
- data/lib/active_support/core_ext/file/atomic.rb +53 -26
- data/lib/active_support/core_ext/file.rb +0 -1
- data/lib/active_support/core_ext/hash/compact.rb +20 -0
- data/lib/active_support/core_ext/hash/conversions.rb +175 -70
- data/lib/active_support/core_ext/hash/deep_merge.rb +30 -8
- data/lib/active_support/core_ext/hash/except.rb +11 -12
- data/lib/active_support/core_ext/hash/indifferent_access.rb +7 -8
- data/lib/active_support/core_ext/hash/keys.rb +147 -24
- data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
- data/lib/active_support/core_ext/hash/slice.rb +22 -14
- data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
- data/lib/active_support/core_ext/hash.rb +2 -2
- data/lib/active_support/core_ext/integer/inflections.rb +13 -1
- data/lib/active_support/core_ext/integer/multiple.rb +4 -0
- data/lib/active_support/core_ext/integer/time.rb +12 -22
- data/lib/active_support/core_ext/kernel/agnostics.rb +2 -2
- data/lib/active_support/core_ext/kernel/concern.rb +12 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +2 -15
- data/lib/active_support/core_ext/kernel/reporting.rb +12 -62
- data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
- data/lib/active_support/core_ext/kernel.rb +2 -3
- data/lib/active_support/core_ext/load_error.rb +14 -7
- data/lib/active_support/core_ext/marshal.rb +22 -0
- data/lib/active_support/core_ext/module/aliasing.rb +16 -12
- data/lib/active_support/core_ext/module/anonymous.rb +12 -8
- data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
- data/lib/active_support/core_ext/module/attribute_accessors.rb +165 -13
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
- data/lib/active_support/core_ext/module/concerning.rb +135 -0
- data/lib/active_support/core_ext/module/delegation.rb +141 -68
- data/lib/active_support/core_ext/module/deprecation.rb +17 -3
- data/lib/active_support/core_ext/module/introspection.rb +9 -31
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
- data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
- data/lib/active_support/core_ext/module/reachable.rb +1 -3
- data/lib/active_support/core_ext/module/remove_method.rb +24 -5
- data/lib/active_support/core_ext/module.rb +3 -3
- data/lib/active_support/core_ext/name_error.rb +15 -2
- data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +31 -36
- data/lib/active_support/core_ext/numeric.rb +2 -0
- data/lib/active_support/core_ext/object/acts_like.rb +4 -4
- data/lib/active_support/core_ext/object/blank.rb +52 -18
- data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
- data/lib/active_support/core_ext/object/duplicable.rb +12 -20
- data/lib/active_support/core_ext/object/inclusion.rb +13 -1
- data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
- data/lib/active_support/core_ext/object/json.rb +205 -0
- data/lib/active_support/core_ext/object/to_param.rb +1 -55
- data/lib/active_support/core_ext/object/to_query.rb +66 -9
- data/lib/active_support/core_ext/object/try.rb +124 -33
- data/lib/active_support/core_ext/object/with_options.rb +37 -11
- data/lib/active_support/core_ext/object.rb +2 -1
- data/lib/active_support/core_ext/range/conversions.rb +17 -7
- data/lib/active_support/core_ext/range/each.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +20 -18
- data/lib/active_support/core_ext/range/overlaps.rb +1 -1
- data/lib/active_support/core_ext/range.rb +1 -2
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string/access.rb +95 -90
- data/lib/active_support/core_ext/string/behavior.rb +1 -1
- data/lib/active_support/core_ext/string/conversions.rb +41 -38
- data/lib/active_support/core_ext/string/exclude.rb +6 -1
- data/lib/active_support/core_ext/string/filters.rb +70 -17
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +139 -59
- data/lib/active_support/core_ext/string/inquiry.rb +2 -2
- data/lib/active_support/core_ext/string/multibyte.rb +46 -65
- data/lib/active_support/core_ext/string/output_safety.rb +153 -56
- data/lib/active_support/core_ext/string/strip.rb +3 -6
- data/lib/active_support/core_ext/string/zones.rb +14 -0
- data/lib/active_support/core_ext/string.rb +2 -3
- data/lib/active_support/core_ext/struct.rb +3 -0
- data/lib/active_support/core_ext/time/calculations.rb +173 -173
- data/lib/active_support/core_ext/time/compatibility.rb +5 -0
- data/lib/active_support/core_ext/time/conversions.rb +33 -29
- data/lib/active_support/core_ext/time/marshal.rb +2 -56
- data/lib/active_support/core_ext/time/zones.rb +57 -32
- data/lib/active_support/core_ext/time.rb +5 -0
- data/lib/active_support/core_ext/uri.rb +13 -19
- data/lib/active_support/core_ext.rb +3 -2
- data/lib/active_support/dependencies/autoload.rb +47 -20
- data/lib/active_support/dependencies/interlock.rb +51 -0
- data/lib/active_support/dependencies.rb +315 -265
- data/lib/active_support/deprecation/behaviors.rb +71 -30
- data/lib/active_support/deprecation/instance_delegator.rb +24 -0
- data/lib/active_support/deprecation/method_wrappers.rb +59 -18
- data/lib/active_support/deprecation/proxy_wrappers.rb +82 -14
- data/lib/active_support/deprecation/reporting.rb +61 -14
- data/lib/active_support/deprecation.rb +38 -13
- data/lib/active_support/descendants_tracker.rb +34 -19
- data/lib/active_support/duration/iso8601_parser.rb +122 -0
- data/lib/active_support/duration/iso8601_serializer.rb +51 -0
- data/lib/active_support/duration.rb +85 -14
- data/lib/active_support/evented_file_update_checker.rb +194 -0
- data/lib/active_support/execution_wrapper.rb +117 -0
- data/lib/active_support/executor.rb +6 -0
- data/lib/active_support/file_update_checker.rb +138 -17
- data/lib/active_support/gem_version.rb +15 -0
- data/lib/active_support/gzip.rb +11 -5
- data/lib/active_support/hash_with_indifferent_access.rb +199 -49
- data/lib/active_support/i18n.rb +6 -2
- data/lib/active_support/i18n_railtie.rb +40 -21
- data/lib/active_support/inflections.rb +22 -13
- data/lib/active_support/inflector/inflections.rb +175 -144
- data/lib/active_support/inflector/methods.rb +328 -91
- data/lib/active_support/inflector/transliterate.rb +51 -37
- data/lib/active_support/json/decoding.rb +31 -22
- data/lib/active_support/json/encoding.rb +88 -248
- data/lib/active_support/key_generator.rb +71 -0
- data/lib/active_support/lazy_load_hooks.rb +27 -25
- data/lib/active_support/locale/en.yml +102 -3
- data/lib/active_support/log_subscriber/test_helper.rb +24 -21
- data/lib/active_support/log_subscriber.rb +36 -49
- data/lib/active_support/logger.rb +106 -0
- data/lib/active_support/logger_silence.rb +28 -0
- data/lib/active_support/logger_thread_safe_level.rb +31 -0
- data/lib/active_support/message_encryptor.rb +72 -36
- data/lib/active_support/message_verifier.rb +96 -24
- data/lib/active_support/multibyte/chars.rb +88 -333
- data/lib/active_support/multibyte/unicode.rb +156 -136
- data/lib/active_support/multibyte.rb +5 -28
- data/lib/active_support/notifications/fanout.rb +115 -19
- data/lib/active_support/notifications/instrumenter.rb +52 -15
- data/lib/active_support/notifications.rb +168 -33
- data/lib/active_support/number_helper/number_converter.rb +182 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
- data/lib/active_support/number_helper.rb +368 -0
- data/lib/active_support/option_merger.rb +1 -1
- data/lib/active_support/ordered_hash.rb +18 -183
- data/lib/active_support/ordered_options.rb +44 -24
- data/lib/active_support/per_thread_registry.rb +58 -0
- data/lib/active_support/proxy_object.rb +13 -0
- data/lib/active_support/rails.rb +27 -0
- data/lib/active_support/railtie.rb +25 -34
- data/lib/active_support/reloader.rb +129 -0
- data/lib/active_support/rescuable.rb +98 -48
- data/lib/active_support/security_utils.rb +27 -0
- data/lib/active_support/string_inquirer.rb +14 -9
- data/lib/active_support/subscriber.rb +120 -0
- data/lib/active_support/tagged_logging.rb +78 -0
- data/lib/active_support/test_case.rb +69 -17
- data/lib/active_support/testing/assertions.rb +43 -41
- data/lib/active_support/testing/autorun.rb +12 -0
- data/lib/active_support/testing/constant_lookup.rb +50 -0
- data/lib/active_support/testing/declarative.rb +7 -21
- data/lib/active_support/testing/deprecation.rb +14 -33
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +53 -95
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/setup_and_teardown.rb +21 -82
- data/lib/active_support/testing/stream.rb +42 -0
- data/lib/active_support/testing/tagged_logging.rb +25 -0
- data/lib/active_support/testing/time_helpers.rb +134 -0
- data/lib/active_support/time.rb +6 -23
- data/lib/active_support/time_with_zone.rb +239 -92
- data/lib/active_support/values/time_zone.rb +236 -160
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +5 -7
- data/lib/active_support/xml_mini/jdom.rb +19 -13
- data/lib/active_support/xml_mini/libxml.rb +3 -4
- data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
- data/lib/active_support/xml_mini/nokogiri.rb +3 -4
- data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
- data/lib/active_support/xml_mini/rexml.rb +8 -10
- data/lib/active_support/xml_mini.rb +66 -34
- data/lib/active_support.rb +40 -23
- metadata +185 -134
- data/CHANGELOG +0 -1534
- data/lib/active_support/base64.rb +0 -42
- data/lib/active_support/basic_object.rb +0 -21
- data/lib/active_support/buffered_logger.rb +0 -137
- data/lib/active_support/cache/compressed_mem_cache_store.rb +0 -13
- data/lib/active_support/cache/synchronized_memory_store.rb +0 -11
- data/lib/active_support/core_ext/array/random_access.rb +0 -30
- data/lib/active_support/core_ext/array/uniq_by.rb +0 -16
- data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -44
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +0 -178
- data/lib/active_support/core_ext/date/freeze.rb +0 -31
- data/lib/active_support/core_ext/date_time/zones.rb +0 -21
- data/lib/active_support/core_ext/exception.rb +0 -3
- data/lib/active_support/core_ext/file/path.rb +0 -5
- data/lib/active_support/core_ext/float/rounding.rb +0 -19
- data/lib/active_support/core_ext/float.rb +0 -1
- data/lib/active_support/core_ext/hash/deep_dup.rb +0 -11
- data/lib/active_support/core_ext/hash/diff.rb +0 -13
- data/lib/active_support/core_ext/kernel/requires.rb +0 -28
- data/lib/active_support/core_ext/logger.rb +0 -81
- data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +0 -31
- data/lib/active_support/core_ext/module/method_names.rb +0 -14
- data/lib/active_support/core_ext/module/synchronization.rb +0 -43
- data/lib/active_support/core_ext/object/to_json.rb +0 -19
- data/lib/active_support/core_ext/proc.rb +0 -14
- data/lib/active_support/core_ext/process/daemon.rb +0 -23
- data/lib/active_support/core_ext/process.rb +0 -1
- data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
- data/lib/active_support/core_ext/range/cover.rb +0 -3
- data/lib/active_support/core_ext/rexml.rb +0 -46
- data/lib/active_support/core_ext/string/encoding.rb +0 -11
- data/lib/active_support/core_ext/string/interpolation.rb +0 -2
- data/lib/active_support/core_ext/string/xchar.rb +0 -18
- data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
- data/lib/active_support/file_watcher.rb +0 -36
- data/lib/active_support/json/variable.rb +0 -9
- data/lib/active_support/memoizable.rb +0 -105
- data/lib/active_support/multibyte/exceptions.rb +0 -8
- data/lib/active_support/multibyte/utils.rb +0 -60
- data/lib/active_support/ruby/shim.rb +0 -22
- data/lib/active_support/secure_random.rb +0 -6
- data/lib/active_support/testing/mochaing.rb +0 -7
- data/lib/active_support/testing/pending.rb +0 -52
- data/lib/active_support/testing/performance/jruby.rb +0 -115
- data/lib/active_support/testing/performance/rubinius.rb +0 -113
- data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
- data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
- data/lib/active_support/testing/performance/ruby.rb +0 -152
- data/lib/active_support/testing/performance.rb +0 -317
- data/lib/active_support/time/autoload.rb +0 -5
- data/lib/active_support/whiny_nil.rb +0 -60
@@ -1,9 +1,59 @@
|
|
1
1
|
require 'active_support/core_ext/array/extract_options'
|
2
2
|
|
3
|
+
# Extends the module object with class/module and instance accessors for
|
4
|
+
# class/module attributes, just like the native attr* accessors for instance
|
5
|
+
# attributes.
|
3
6
|
class Module
|
7
|
+
# Defines a class attribute and creates a class and instance reader methods.
|
8
|
+
# The underlying class variable is set to +nil+, if it is not previously
|
9
|
+
# defined.
|
10
|
+
#
|
11
|
+
# module HairColors
|
12
|
+
# mattr_reader :hair_colors
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# HairColors.hair_colors # => nil
|
16
|
+
# HairColors.class_variable_set("@@hair_colors", [:brown, :black])
|
17
|
+
# HairColors.hair_colors # => [:brown, :black]
|
18
|
+
#
|
19
|
+
# The attribute name must be a valid method name in Ruby.
|
20
|
+
#
|
21
|
+
# module Foo
|
22
|
+
# mattr_reader :"1_Badname"
|
23
|
+
# end
|
24
|
+
# # => NameError: invalid attribute name: 1_Badname
|
25
|
+
#
|
26
|
+
# If you want to opt out the creation on the instance reader method, pass
|
27
|
+
# <tt>instance_reader: false</tt> or <tt>instance_accessor: false</tt>.
|
28
|
+
#
|
29
|
+
# module HairColors
|
30
|
+
# mattr_reader :hair_colors, instance_reader: false
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# class Person
|
34
|
+
# include HairColors
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# Person.new.hair_colors # => NoMethodError
|
38
|
+
#
|
39
|
+
#
|
40
|
+
# Also, you can pass a block to set up the attribute with a default value.
|
41
|
+
#
|
42
|
+
# module HairColors
|
43
|
+
# mattr_reader :hair_colors do
|
44
|
+
# [:brown, :black, :blonde, :red]
|
45
|
+
# end
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# class Person
|
49
|
+
# include HairColors
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# Person.new.hair_colors # => [:brown, :black, :blonde, :red]
|
4
53
|
def mattr_reader(*syms)
|
5
54
|
options = syms.extract_options!
|
6
55
|
syms.each do |sym|
|
56
|
+
raise NameError.new("invalid attribute name: #{sym}") unless sym =~ /\A[_A-Za-z]\w*\z/
|
7
57
|
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
8
58
|
@@#{sym} = nil unless defined? @@#{sym}
|
9
59
|
|
@@ -12,49 +62,151 @@ class Module
|
|
12
62
|
end
|
13
63
|
EOS
|
14
64
|
|
15
|
-
unless options[:instance_reader] == false
|
65
|
+
unless options[:instance_reader] == false || options[:instance_accessor] == false
|
16
66
|
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
17
67
|
def #{sym}
|
18
68
|
@@#{sym}
|
19
69
|
end
|
20
70
|
EOS
|
21
71
|
end
|
72
|
+
class_variable_set("@@#{sym}", yield) if block_given?
|
22
73
|
end
|
23
74
|
end
|
75
|
+
alias :cattr_reader :mattr_reader
|
24
76
|
|
77
|
+
# Defines a class attribute and creates a class and instance writer methods to
|
78
|
+
# allow assignment to the attribute.
|
79
|
+
#
|
80
|
+
# module HairColors
|
81
|
+
# mattr_writer :hair_colors
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# class Person
|
85
|
+
# include HairColors
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# HairColors.hair_colors = [:brown, :black]
|
89
|
+
# Person.class_variable_get("@@hair_colors") # => [:brown, :black]
|
90
|
+
# Person.new.hair_colors = [:blonde, :red]
|
91
|
+
# HairColors.class_variable_get("@@hair_colors") # => [:blonde, :red]
|
92
|
+
#
|
93
|
+
# If you want to opt out the instance writer method, pass
|
94
|
+
# <tt>instance_writer: false</tt> or <tt>instance_accessor: false</tt>.
|
95
|
+
#
|
96
|
+
# module HairColors
|
97
|
+
# mattr_writer :hair_colors, instance_writer: false
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# class Person
|
101
|
+
# include HairColors
|
102
|
+
# end
|
103
|
+
#
|
104
|
+
# Person.new.hair_colors = [:blonde, :red] # => NoMethodError
|
105
|
+
#
|
106
|
+
# Also, you can pass a block to set up the attribute with a default value.
|
107
|
+
#
|
108
|
+
# module HairColors
|
109
|
+
# mattr_writer :hair_colors do
|
110
|
+
# [:brown, :black, :blonde, :red]
|
111
|
+
# end
|
112
|
+
# end
|
113
|
+
#
|
114
|
+
# class Person
|
115
|
+
# include HairColors
|
116
|
+
# end
|
117
|
+
#
|
118
|
+
# Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
|
25
119
|
def mattr_writer(*syms)
|
26
120
|
options = syms.extract_options!
|
27
121
|
syms.each do |sym|
|
122
|
+
raise NameError.new("invalid attribute name: #{sym}") unless sym =~ /\A[_A-Za-z]\w*\z/
|
28
123
|
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
124
|
+
@@#{sym} = nil unless defined? @@#{sym}
|
125
|
+
|
29
126
|
def self.#{sym}=(obj)
|
30
127
|
@@#{sym} = obj
|
31
128
|
end
|
32
129
|
EOS
|
33
130
|
|
34
|
-
unless options[:instance_writer] == false
|
131
|
+
unless options[:instance_writer] == false || options[:instance_accessor] == false
|
35
132
|
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
36
133
|
def #{sym}=(obj)
|
37
134
|
@@#{sym} = obj
|
38
135
|
end
|
39
136
|
EOS
|
40
137
|
end
|
138
|
+
send("#{sym}=", yield) if block_given?
|
41
139
|
end
|
42
140
|
end
|
141
|
+
alias :cattr_writer :mattr_writer
|
43
142
|
|
44
|
-
#
|
45
|
-
#
|
143
|
+
# Defines both class and instance accessors for class attributes.
|
144
|
+
#
|
145
|
+
# module HairColors
|
146
|
+
# mattr_accessor :hair_colors
|
147
|
+
# end
|
148
|
+
#
|
149
|
+
# class Person
|
150
|
+
# include HairColors
|
151
|
+
# end
|
152
|
+
#
|
153
|
+
# HairColors.hair_colors = [:brown, :black, :blonde, :red]
|
154
|
+
# HairColors.hair_colors # => [:brown, :black, :blonde, :red]
|
155
|
+
# Person.new.hair_colors # => [:brown, :black, :blonde, :red]
|
156
|
+
#
|
157
|
+
# If a subclass changes the value then that would also change the value for
|
158
|
+
# parent class. Similarly if parent class changes the value then that would
|
159
|
+
# change the value of subclasses too.
|
160
|
+
#
|
161
|
+
# class Male < Person
|
162
|
+
# end
|
163
|
+
#
|
164
|
+
# Male.new.hair_colors << :blue
|
165
|
+
# Person.new.hair_colors # => [:brown, :black, :blonde, :red, :blue]
|
166
|
+
#
|
167
|
+
# To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
|
168
|
+
# To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
|
169
|
+
#
|
170
|
+
# module HairColors
|
171
|
+
# mattr_accessor :hair_colors, instance_writer: false, instance_reader: false
|
172
|
+
# end
|
173
|
+
#
|
174
|
+
# class Person
|
175
|
+
# include HairColors
|
176
|
+
# end
|
177
|
+
#
|
178
|
+
# Person.new.hair_colors = [:brown] # => NoMethodError
|
179
|
+
# Person.new.hair_colors # => NoMethodError
|
180
|
+
#
|
181
|
+
# Or pass <tt>instance_accessor: false</tt>, to opt out both instance methods.
|
182
|
+
#
|
183
|
+
# module HairColors
|
184
|
+
# mattr_accessor :hair_colors, instance_accessor: false
|
185
|
+
# end
|
186
|
+
#
|
187
|
+
# class Person
|
188
|
+
# include HairColors
|
189
|
+
# end
|
190
|
+
#
|
191
|
+
# Person.new.hair_colors = [:brown] # => NoMethodError
|
192
|
+
# Person.new.hair_colors # => NoMethodError
|
193
|
+
#
|
194
|
+
# Also you can pass a block to set up the attribute with a default value.
|
46
195
|
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
196
|
+
# module HairColors
|
197
|
+
# mattr_accessor :hair_colors do
|
198
|
+
# [:brown, :black, :blonde, :red]
|
199
|
+
# end
|
200
|
+
# end
|
50
201
|
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
202
|
+
# class Person
|
203
|
+
# include HairColors
|
204
|
+
# end
|
54
205
|
#
|
55
|
-
#
|
56
|
-
def mattr_accessor(*syms)
|
57
|
-
mattr_reader(*syms)
|
206
|
+
# Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
|
207
|
+
def mattr_accessor(*syms, &blk)
|
208
|
+
mattr_reader(*syms, &blk)
|
58
209
|
mattr_writer(*syms)
|
59
210
|
end
|
211
|
+
alias :cattr_accessor :mattr_accessor
|
60
212
|
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'active_support/core_ext/array/extract_options'
|
2
|
+
|
3
|
+
# Extends the module object with class/module and instance accessors for
|
4
|
+
# class/module attributes, just like the native attr* accessors for instance
|
5
|
+
# attributes, but does so on a per-thread basis.
|
6
|
+
#
|
7
|
+
# So the values are scoped within the Thread.current space under the class name
|
8
|
+
# of the module.
|
9
|
+
class Module
|
10
|
+
# Defines a per-thread class attribute and creates class and instance reader methods.
|
11
|
+
# The underlying per-thread class variable is set to +nil+, if it is not previously defined.
|
12
|
+
#
|
13
|
+
# module Current
|
14
|
+
# thread_mattr_reader :user
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# Current.user # => nil
|
18
|
+
# Thread.current[:attr_Current_user] = "DHH"
|
19
|
+
# Current.user # => "DHH"
|
20
|
+
#
|
21
|
+
# The attribute name must be a valid method name in Ruby.
|
22
|
+
#
|
23
|
+
# module Foo
|
24
|
+
# thread_mattr_reader :"1_Badname"
|
25
|
+
# end
|
26
|
+
# # => NameError: invalid attribute name: 1_Badname
|
27
|
+
#
|
28
|
+
# If you want to opt out the creation on the instance reader method, pass
|
29
|
+
# <tt>instance_reader: false</tt> or <tt>instance_accessor: false</tt>.
|
30
|
+
#
|
31
|
+
# class Current
|
32
|
+
# thread_mattr_reader :user, instance_reader: false
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# Current.new.user # => NoMethodError
|
36
|
+
def thread_mattr_reader(*syms)
|
37
|
+
options = syms.extract_options!
|
38
|
+
|
39
|
+
syms.each do |sym|
|
40
|
+
raise NameError.new("invalid attribute name: #{sym}") unless sym =~ /^[_A-Za-z]\w*$/
|
41
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
42
|
+
def self.#{sym}
|
43
|
+
Thread.current[:"attr_#{name}_#{sym}"]
|
44
|
+
end
|
45
|
+
EOS
|
46
|
+
|
47
|
+
unless options[:instance_reader] == false || options[:instance_accessor] == false
|
48
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
49
|
+
def #{sym}
|
50
|
+
Thread.current[:"attr_#{name}_#{sym}"]
|
51
|
+
end
|
52
|
+
EOS
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
alias :thread_cattr_reader :thread_mattr_reader
|
57
|
+
|
58
|
+
# Defines a per-thread class attribute and creates a class and instance writer methods to
|
59
|
+
# allow assignment to the attribute.
|
60
|
+
#
|
61
|
+
# module Current
|
62
|
+
# thread_mattr_writer :user
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# Current.user = "DHH"
|
66
|
+
# Thread.current[:attr_Current_user] # => "DHH"
|
67
|
+
#
|
68
|
+
# If you want to opt out the instance writer method, pass
|
69
|
+
# <tt>instance_writer: false</tt> or <tt>instance_accessor: false</tt>.
|
70
|
+
#
|
71
|
+
# class Current
|
72
|
+
# thread_mattr_writer :user, instance_writer: false
|
73
|
+
# end
|
74
|
+
#
|
75
|
+
# Current.new.user = "DHH" # => NoMethodError
|
76
|
+
def thread_mattr_writer(*syms)
|
77
|
+
options = syms.extract_options!
|
78
|
+
syms.each do |sym|
|
79
|
+
raise NameError.new("invalid attribute name: #{sym}") unless sym =~ /^[_A-Za-z]\w*$/
|
80
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
81
|
+
def self.#{sym}=(obj)
|
82
|
+
Thread.current[:"attr_#{name}_#{sym}"] = obj
|
83
|
+
end
|
84
|
+
EOS
|
85
|
+
|
86
|
+
unless options[:instance_writer] == false || options[:instance_accessor] == false
|
87
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
88
|
+
def #{sym}=(obj)
|
89
|
+
Thread.current[:"attr_#{name}_#{sym}"] = obj
|
90
|
+
end
|
91
|
+
EOS
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
alias :thread_cattr_writer :thread_mattr_writer
|
96
|
+
|
97
|
+
# Defines both class and instance accessors for class attributes.
|
98
|
+
#
|
99
|
+
# class Account
|
100
|
+
# thread_mattr_accessor :user
|
101
|
+
# end
|
102
|
+
#
|
103
|
+
# Account.user = "DHH"
|
104
|
+
# Account.user # => "DHH"
|
105
|
+
# Account.new.user # => "DHH"
|
106
|
+
#
|
107
|
+
# If a subclass changes the value, the parent class' value is not changed.
|
108
|
+
# Similarly, if the parent class changes the value, the value of subclasses
|
109
|
+
# is not changed.
|
110
|
+
#
|
111
|
+
# class Customer < Account
|
112
|
+
# end
|
113
|
+
#
|
114
|
+
# Customer.user = "Rafael"
|
115
|
+
# Customer.user # => "Rafael"
|
116
|
+
# Account.user # => "DHH"
|
117
|
+
#
|
118
|
+
# To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
|
119
|
+
# To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
|
120
|
+
#
|
121
|
+
# class Current
|
122
|
+
# thread_mattr_accessor :user, instance_writer: false, instance_reader: false
|
123
|
+
# end
|
124
|
+
#
|
125
|
+
# Current.new.user = "DHH" # => NoMethodError
|
126
|
+
# Current.new.user # => NoMethodError
|
127
|
+
#
|
128
|
+
# Or pass <tt>instance_accessor: false</tt>, to opt out both instance methods.
|
129
|
+
#
|
130
|
+
# class Current
|
131
|
+
# mattr_accessor :user, instance_accessor: false
|
132
|
+
# end
|
133
|
+
#
|
134
|
+
# Current.new.user = "DHH" # => NoMethodError
|
135
|
+
# Current.new.user # => NoMethodError
|
136
|
+
def thread_mattr_accessor(*syms, &blk)
|
137
|
+
thread_mattr_reader(*syms, &blk)
|
138
|
+
thread_mattr_writer(*syms, &blk)
|
139
|
+
end
|
140
|
+
alias :thread_cattr_accessor :thread_mattr_accessor
|
141
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
class Module
|
4
|
+
# = Bite-sized separation of concerns
|
5
|
+
#
|
6
|
+
# We often find ourselves with a medium-sized chunk of behavior that we'd
|
7
|
+
# like to extract, but only mix in to a single class.
|
8
|
+
#
|
9
|
+
# Extracting a plain old Ruby object to encapsulate it and collaborate or
|
10
|
+
# delegate to the original object is often a good choice, but when there's
|
11
|
+
# no additional state to encapsulate or we're making DSL-style declarations
|
12
|
+
# about the parent class, introducing new collaborators can obfuscate rather
|
13
|
+
# than simplify.
|
14
|
+
#
|
15
|
+
# The typical route is to just dump everything in a monolithic class, perhaps
|
16
|
+
# with a comment, as a least-bad alternative. Using modules in separate files
|
17
|
+
# means tedious sifting to get a big-picture view.
|
18
|
+
#
|
19
|
+
# = Dissatisfying ways to separate small concerns
|
20
|
+
#
|
21
|
+
# == Using comments:
|
22
|
+
#
|
23
|
+
# class Todo
|
24
|
+
# # Other todo implementation
|
25
|
+
# # ...
|
26
|
+
#
|
27
|
+
# ## Event tracking
|
28
|
+
# has_many :events
|
29
|
+
#
|
30
|
+
# before_create :track_creation
|
31
|
+
# after_destroy :track_deletion
|
32
|
+
#
|
33
|
+
# private
|
34
|
+
# def track_creation
|
35
|
+
# # ...
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# == With an inline module:
|
40
|
+
#
|
41
|
+
# Noisy syntax.
|
42
|
+
#
|
43
|
+
# class Todo
|
44
|
+
# # Other todo implementation
|
45
|
+
# # ...
|
46
|
+
#
|
47
|
+
# module EventTracking
|
48
|
+
# extend ActiveSupport::Concern
|
49
|
+
#
|
50
|
+
# included do
|
51
|
+
# has_many :events
|
52
|
+
# before_create :track_creation
|
53
|
+
# after_destroy :track_deletion
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# private
|
57
|
+
# def track_creation
|
58
|
+
# # ...
|
59
|
+
# end
|
60
|
+
# end
|
61
|
+
# include EventTracking
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# == Mix-in noise exiled to its own file:
|
65
|
+
#
|
66
|
+
# Once our chunk of behavior starts pushing the scroll-to-understand-it
|
67
|
+
# boundary, we give in and move it to a separate file. At this size, the
|
68
|
+
# increased overhead can be a reasonable tradeoff even if it reduces our
|
69
|
+
# at-a-glance perception of how things work.
|
70
|
+
#
|
71
|
+
# class Todo
|
72
|
+
# # Other todo implementation
|
73
|
+
# # ...
|
74
|
+
#
|
75
|
+
# include TodoEventTracking
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# = Introducing Module#concerning
|
79
|
+
#
|
80
|
+
# By quieting the mix-in noise, we arrive at a natural, low-ceremony way to
|
81
|
+
# separate bite-sized concerns.
|
82
|
+
#
|
83
|
+
# class Todo
|
84
|
+
# # Other todo implementation
|
85
|
+
# # ...
|
86
|
+
#
|
87
|
+
# concerning :EventTracking do
|
88
|
+
# included do
|
89
|
+
# has_many :events
|
90
|
+
# before_create :track_creation
|
91
|
+
# after_destroy :track_deletion
|
92
|
+
# end
|
93
|
+
#
|
94
|
+
# private
|
95
|
+
# def track_creation
|
96
|
+
# # ...
|
97
|
+
# end
|
98
|
+
# end
|
99
|
+
# end
|
100
|
+
#
|
101
|
+
# Todo.ancestors
|
102
|
+
# # => [Todo, Todo::EventTracking, Object]
|
103
|
+
#
|
104
|
+
# This small step has some wonderful ripple effects. We can
|
105
|
+
# * grok the behavior of our class in one glance,
|
106
|
+
# * clean up monolithic junk-drawer classes by separating their concerns, and
|
107
|
+
# * stop leaning on protected/private for crude "this is internal stuff" modularity.
|
108
|
+
module Concerning
|
109
|
+
# Define a new concern and mix it in.
|
110
|
+
def concerning(topic, &block)
|
111
|
+
include concern(topic, &block)
|
112
|
+
end
|
113
|
+
|
114
|
+
# A low-cruft shortcut to define a concern.
|
115
|
+
#
|
116
|
+
# concern :EventTracking do
|
117
|
+
# ...
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# is equivalent to
|
121
|
+
#
|
122
|
+
# module EventTracking
|
123
|
+
# extend ActiveSupport::Concern
|
124
|
+
#
|
125
|
+
# ...
|
126
|
+
# end
|
127
|
+
def concern(topic, &module_definition)
|
128
|
+
const_set topic, Module.new {
|
129
|
+
extend ::ActiveSupport::Concern
|
130
|
+
module_eval(&module_definition)
|
131
|
+
}
|
132
|
+
end
|
133
|
+
end
|
134
|
+
include Concerning
|
135
|
+
end
|