aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/module/delegation.rb
Commit message (Collapse)AuthorAgeFilesLines
* split DELEGATION_RESERVED_METHOD_NAMES in halfToshimaru2016-12-091-4/+5
|
* Fix typo in Delegation#delegate_missing_to doc [skip ci]Anton Davydov2016-08-271-1/+1
|
* applies new string literal convention in activesupport/libXavier Noria2016-08-061-8/+8
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* systematic revision of =~ usage in ASXavier Noria2016-07-221-2/+3
| | | | | Where appropriate prefer the more concise Regexp#match?, String#include?, String#start_with?, and String#end_with?
* Replace Kernel#caller by the faster Kernel#caller_locationsJean Boussier2016-06-101-2/+2
|
* Don't delegate to private methods of the targerRafael Mendonça França2016-05-241-1/+5
| | | | And make sure that it doesn't even try to call the method in the target.
* Merge pull request #23930 from gsamokovarov/module-delegate-missing-toRafael Mendonça França2016-05-241-1/+60
|\ | | | | | | Introduce Module#delegate_missing_to
| * Introduce Module#delegate_missing_toGenadi Samokovarov2016-02-271-1/+60
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When building decorators, a common pattern may emerge: class Partition def initialize(first_event) @events = [ first_event ] end def people if @events.first.detail.people.any? @events.collect { |e| Array(e.detail.people) }.flatten.uniq else @events.collect(&:creator).uniq end end private def respond_to_missing?(name, include_private = false) @events.respond_to?(name, include_private) end def method_missing(method, *args, &block) @events.send(method, *args, &block) end end With `Module#delegate_missing_to`, the above is condensed to: class Partition delegate_missing_to :@events def initialize(first_event) @events = [ first_event ] end def people if @events.first.detail.people.any? @events.collect { |e| Array(e.detail.people) }.flatten.uniq else @events.collect(&:creator).uniq end end end David suggested it in #23824.
* | Update delegate to use newer Ruby syntaxTodd Lynam2016-04-201-5/+2
|/ | | This commit updates `delegate` to use the keyword argument syntax added in Ruby 2. I left the `ArgumentError` when `to` is missing, because it better explains how to correctly use `delegate`. We could instead rely on the default `ArgumentError` that would be raised if `to` were a required keyword argument.
* Fixing issue when delegating to methods named "block", "args", or "arg"Jake Bell2015-08-191-5/+6
|
* Freeze string literals when not mutated.schneems2015-07-191-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I wrote a utility that helps find areas where you could optimize your program using a frozen string instead of a string literal, it's called [let_it_go](https://github.com/schneems/let_it_go). After going through the output and adding `.freeze` I was able to eliminate the creation of 1,114 string objects on EVERY request to [codetriage](codetriage.com). How does this impact execution? To look at memory: ```ruby require 'get_process_mem' mem = GetProcessMem.new GC.start GC.disable 1_114.times { " " } before = mem.mb after = mem.mb GC.enable puts "Diff: #{after - before} mb" ``` Creating 1,114 string objects results in `Diff: 0.03125 mb` of RAM allocated on every request. Or 1mb every 32 requests. To look at raw speed: ```ruby require 'benchmark/ips' number_of_objects_reduced = 1_114 Benchmark.ips do |x| x.report("freeze") { number_of_objects_reduced.times { " ".freeze } } x.report("no-freeze") { number_of_objects_reduced.times { " " } } end ``` We get the results ``` Calculating ------------------------------------- freeze 1.428k i/100ms no-freeze 609.000 i/100ms ------------------------------------------------- freeze 14.363k (± 8.5%) i/s - 71.400k no-freeze 6.084k (± 8.1%) i/s - 30.450k ``` Now we can do some maths: ```ruby ips = 6_226k # iterations / 1 second call_time_before = 1.0 / ips # seconds per iteration ips = 15_254 # iterations / 1 second call_time_after = 1.0 / ips # seconds per iteration diff = call_time_before - call_time_after number_of_objects_reduced * diff * 100 # => 0.4530373333993266 miliseconds saved per request ``` So we're shaving off 1 second of execution time for every 220 requests. Is this going to be an insane speed boost to any Rails app: nope. Should we merge it: yep. p.s. If you know of a method call that doesn't modify a string input such as [String#gsub](https://github.com/schneems/let_it_go/blob/b0e2da69f0cca87ab581022baa43291cdf48638c/lib/let_it_go/core_ext/string.rb#L37) please [give me a pull request to the appropriate file](https://github.com/schneems/let_it_go/blob/b0e2da69f0cca87ab581022baa43291cdf48638c/lib/let_it_go/core_ext/string.rb#L37), or open an issue in LetItGo so we can track and freeze more strings. Keep those strings Frozen ![](https://www.dropbox.com/s/z4dj9fdsv213r4v/let-it-go.gif?dl=1)
* Reduce allocated memory for Module#delegate.Guo Xiang Tan2015-03-011-1/+1
|
* Revert 88d08f2ec9f89ba431cba8d0c06ac9ebc204bbbbRafael Mendonça França2015-02-111-12/+24
| | | | | | | This caused a performance regression since we were decided to do the nil check in run time not in the load time. See https://github.com/rails/rails/pull/15187#issuecomment-71760058
* English fix [ci skip]George Millo2014-12-121-1/+1
|
* Delegation works with reserved words passed to `:to`Agis-2014-09-191-1/+9
| | | | Fixes #16956.
* Fix confusing exception in ActiveSupport delegationVladimir Yarotsky2014-05-201-25/+13
|
* define the delegate methods on one line. fixes #13724Aaron Patterson2014-01-161-20/+22
| | | | sup haters
* Revert "methods are defined right after the module_eval, so we don't need to do"Piotr Sarnacki2014-01-161-2/+2
| | | | | | | | | This change breaks tests in activesupport/test/core_ext/module_test.rb: * test_delegation_exception_backtrace * test_delegation_exception_backtrace_with_allow_nil This reverts commit 0167765e3f84260522bc2f32d926c1f5dd44957c.
* methods are defined right after the module_eval, so we don't need to doAaron Patterson2014-01-151-2/+2
| | | | any line number maths
* Improve Module#delegate documentation to tell that delegate don't work with ↵Lauro Caetano2013-11-291-0/+2
| | | | private or protected methods. [ci skip]
* Rely on NoMethodError#name when deciding to raise DelegationError.Federico Ravasio2013-08-121-2/+1
| | | | | | | | | | Different Ruby implementations present backtraces differently, as it should be an information consumed by humans. A better implementation should use data from the error, in this case returned by NoMethodError#name. Fixes issues with Rubinius, which presents backtraces differently from MRI.
* Merge branch 'master' of github.com:rails/docrailsVijay Dev2013-07-211-0/+5
|\
| * Enhancing readability of options for delegate methodThiago Pinto2013-07-161-0/+5
| |
* | Only raise DelegationError if it's is the source of the exceptionAndrew White2013-07-111-10/+11
|/ | | | | | | | | This fixes situations where nested NoMethodError exceptions are masked by delegations. This would cause confusion especially where there was a problem in the Rails booting process because of a delegation in the routes reloading code. Fixes #10559
* Move delegation error constant to inside ModuleCarlos Antonio da Silva2013-06-261-2/+4
|
* Merge pull request #10828 from southpolesteve/delegation_error_classYves Senn2013-06-261-1/+3
|\ | | | | Create DelegationError class
| * Add DelegationError class. Rasied by delegation to a nil objectSteve Faulkner2013-06-261-1/+3
| |
* | Fix some typosVipul A M2013-06-071-1/+1
|/
* Use new hash syntax in module delegation docs [ci skip]Carlos Antonio da Silva2013-04-281-1/+1
|
* Module#delegate checks nilness rather that falsehood if :allow_nil is true, ↵Xavier Noria2013-04-261-30/+42
| | | | | | | | | | | | | | and avoids multiple evaluation of the target method Notes: 1) I hope nilness is a word. 2) See rationale for avoiding multiple evaluation in a comment in the patch, credit goes to @jeremy for pointing out this gotcha in the existing implementation. 3) Embeds a little joke dedicated to @pixeltrix (it could be worse! :D). References #10347.
* Delegation method bugLi Ellis Gallardo2013-04-251-0/+14
| | | | | | Add documentation and test to delegation method that make sure we're aware that when a delegated object is not nil or false and doesn't respond to the method it will still raise a NoMethodError exception.
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2012-09-211-1/+0
|\ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Conflicts: actionmailer/lib/action_mailer/base.rb activesupport/lib/active_support/configurable.rb activesupport/lib/active_support/core_ext/module/deprecation.rb guides/source/action_controller_overview.md guides/source/active_support_core_extensions.md guides/source/ajax_on_rails.textile guides/source/association_basics.textile guides/source/upgrading_ruby_on_rails.md While resolving conflicts, I have chosen to ignore changes done in docrails at some places - these will be most likely 1.9 hash syntax changes.
| * update some AS code examples to 1.9 hash syntax [ci skip]Francesco Rodriguez2012-09-121-10/+9
| |
* | Update delegate docs with new hash syntax [ci skip]Carlos Antonio da Silva2012-09-201-10/+10
| |
* | Add changelog entry and docs about class delegation using the symbol :classCarlos Antonio da Silva2012-09-201-0/+12
| | | | | | | | See #7613. [ci skip]
* | Nice and easy delegation to the classMarc-Andre Lafortune2012-09-111-0/+3
|/
* no need to to_s here. Both String and Symbol can be interpolated into StringAkira Matsuda2012-06-061-3/+0
|
* String quotes and trailing spacesAlexey Gaziev2012-04-291-8/+8
|
* AS core_ext refactoring pt.2Alexey Gaziev2012-04-291-1/+1
|
* Remove Module#delegate!Daniel Schierbeck2012-04-121-52/+0
|
* Add back the old `deprecate` method as `deprecate!`Daniel Schierbeck2012-04-121-0/+52
|
* Change API docs regarding delegation to non-public methodsDaniel Schierbeck2012-04-121-1/+1
|
* Optimize the performance of #delegateDaniel Schierbeck2012-04-121-9/+13
| | | | | | Remove the use of #__send__ in order to boost performance. This also means that you can no longer delegate to private methods on the target object.
* Remove deprecation from AS::Deprecation behavior, some minor cleanupsCarlos Antonio da Silva2012-03-161-7/+9
| | | | | | | | | * Refactor log subscriber, use select! to avoid a new object * Remove deprecation messages related to AS::Deprecation behavior This was added about 2 years ago for Rails 3: https://github.com/rails/rails/commit/d4c7d3fd94e5a885a6366eaeb3b908bb58ffd4db * Remove some not used requires * Refactor delegate to avoid string conversions and if statements inside each block
* Fix indentation in code example of DelegationTomasz Zurkowski2012-01-201-19/+19
|
* Revert all the stuff to do with disallowing non-public methods for ↵Jon Leighton2011-08-251-6/+2
| | | | Module#delegate
* prefer ends_with? over slicingXavier Noria2011-08-161-1/+2
|
* Add a test for delegating a method ending in '=' as this is a special case.Jon Leighton2011-08-151-0/+1
|
* Need to include public_sendArun Agrawal2011-08-151-0/+1
|
* Split up the definitions in Module#delegate depending on :allow_nil, and ↵Jon Leighton2011-08-151-17/+22
| | | | don't use exceptions for flow control in the :allow_nil => true case.