From 15b1b2b778ce18ff23737b3a0674780d22605fdf Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 6 Sep 2008 18:45:52 -0700 Subject: Ruby 1.9 compat: use method_defined? instead of instance_methods.include? Don't encourage args abuse by flattening. --- .../lib/active_support/core_ext/module/synchronization.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/module/synchronization.rb b/activesupport/lib/active_support/core_ext/module/synchronization.rb index 6253594dfa..251606024e 100644 --- a/activesupport/lib/active_support/core_ext/module/synchronization.rb +++ b/activesupport/lib/active_support/core_ext/module/synchronization.rb @@ -18,11 +18,13 @@ class Module raise ArgumentError, "Synchronization needs a mutex. Supply an options hash with a :with key as the last argument (e.g. synchronize :hello, :with => :@mutex)." end - methods.flatten.each do |method| + methods.each do |method| aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1 - if instance_methods.include?("#{aliased_method}_without_synchronization#{punctuation}") + + if method_defined?("#{aliased_method}_without_synchronization#{punctuation}") raise ArgumentError, "#{method} is already synchronized. Double synchronization is not currently supported." end + module_eval(<<-EOS, __FILE__, __LINE__) def #{aliased_method}_with_synchronization#{punctuation}(*args, &block) #{with}.synchronize do @@ -30,7 +32,8 @@ class Module end end EOS + alias_method_chain method, :synchronization end end -end \ No newline at end of file +end -- cgit v1.2.3 From 621f48edb27022a0798d083e50339c552221d0bf Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 8 Sep 2008 18:06:58 -0700 Subject: BigDecimal to_s and yaml housekeeping --- .../core_ext/bigdecimal/conversions.rb | 40 ++++++++++------------ 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/bigdecimal/conversions.rb b/activesupport/lib/active_support/core_ext/bigdecimal/conversions.rb index 94c7c779f7..bc9d578f38 100644 --- a/activesupport/lib/active_support/core_ext/bigdecimal/conversions.rb +++ b/activesupport/lib/active_support/core_ext/bigdecimal/conversions.rb @@ -4,35 +4,31 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module BigDecimal #:nodoc: module Conversions + DEFAULT_STRING_FORMAT = 'F'.freeze + YAML_TAG = 'tag:yaml.org,2002:float'.freeze + YAML_MAPPING = { 'Infinity' => '.Inf', '-Infinity' => '-.Inf', 'NaN' => '.NaN' } + def self.included(base) #:nodoc: - base.instance_eval do + base.class_eval do alias_method :_original_to_s, :to_s alias_method :to_s, :to_formatted_s + + yaml_as YAML_TAG end end - - def to_formatted_s(format="F") + + def to_formatted_s(format = DEFAULT_STRING_FORMAT) _original_to_s(format) end - - yaml_as "tag:yaml.org,2002:float" - def to_yaml( opts = {} ) - YAML::quick_emit( nil, opts ) do |out| - # This emits the number without any scientific notation. - # I prefer it to using self.to_f.to_s, which would lose precision. - # - # Note that YAML allows that when reconstituting floats - # to native types, some precision may get lost. - # There is no full precision real YAML tag that I am aware of. - str = self.to_s - if str == "Infinity" - str = ".Inf" - elsif str == "-Infinity" - str = "-.Inf" - elsif str == "NaN" - str = ".NaN" - end - out.scalar( "tag:yaml.org,2002:float", str, :plain ) + + # This emits the number without any scientific notation. + # This is better than self.to_f.to_s since it doesn't lose precision. + # + # Note that reconstituting YAML floats to native floats may lose precision. + def to_yaml(opts = {}) + YAML.quick_emit(nil, opts) do |out| + string = to_s + out.scalar(YAML_TAG, YAML_MAPPING[string] || string, :plain) end end end -- cgit v1.2.3 From dc0aed1151f418c417770539629d26fa34c03815 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 8 Sep 2008 18:08:48 -0700 Subject: Don't shadow arg with block var --- activesupport/lib/active_support/core_ext/enumerable.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index fd94bc051f..788f3a7e9e 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -77,9 +77,9 @@ module Enumerable # (1..5).each_with_object(1) { |value, memo| memo *= value } # => 1 # def each_with_object(memo, &block) - returning memo do |memo| + returning memo do |m| each do |element| - block.call(element, memo) + block.call(element, m) end end end unless [].respond_to?(:each_with_object) -- cgit v1.2.3 From 1b94d5dc6500a34803fda784d87a361b532b3fb4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 8 Sep 2008 18:08:56 -0700 Subject: Fix indentation mismatch --- activesupport/lib/active_support/cache/file_store.rb | 2 +- activesupport/lib/active_support/callbacks.rb | 2 +- activesupport/lib/active_support/core_ext/hash/conversions.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index 659bde64f0..ef533633b2 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -65,6 +65,6 @@ module ActiveSupport end end end - end + end end end diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 7b905930bb..5cdcaf5ad1 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -188,7 +188,7 @@ module ActiveSupport "Callbacks must be a symbol denoting the method to call, a string to be evaluated, " + "a block to be invoked, or an object responding to the callback method." end - end + end end def should_run_callback?(*args) diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index 2c606b401b..50dc7c61fc 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -156,7 +156,7 @@ module ActiveSupport #:nodoc: XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value, attributes ) - end + end end end -- cgit v1.2.3 From 8b4461c1a405e52e22ee05dd8b46168402d02968 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 8 Sep 2008 19:43:52 -0700 Subject: Check whether memoized ivar is initialized --- activesupport/lib/active_support/memoizable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb index 6506238ac0..4786fd6e0b 100644 --- a/activesupport/lib/active_support/memoizable.rb +++ b/activesupport/lib/active_support/memoizable.rb @@ -60,7 +60,7 @@ module ActiveSupport #{memoized_ivar} ||= {} unless frozen? reload = args.pop if args.last == true || args.last == :reload - if #{memoized_ivar} + if defined?(#{memoized_ivar}) && #{memoized_ivar} if !reload && #{memoized_ivar}.has_key?(args) #{memoized_ivar}[args] elsif #{memoized_ivar} -- cgit v1.2.3 From 6e2851d4261fbbea00e8e74802a507a89af80e2f Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Tue, 9 Sep 2008 10:28:53 +0200 Subject: Remove the Version check as it's not always available. --- activesupport/lib/active_support/core_ext/rexml.rb | 42 +++++++++++----------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/rexml.rb b/activesupport/lib/active_support/core_ext/rexml.rb index af8ce3af47..058295b057 100644 --- a/activesupport/lib/active_support/core_ext/rexml.rb +++ b/activesupport/lib/active_support/core_ext/rexml.rb @@ -5,30 +5,28 @@ require 'rexml/entity' # http://www.ruby-lang.org/en/news/2008/08/23/dos-vulnerability-in-rexml/ # This fix is identical to rexml-expansion-fix version 1.0.1 -unless REXML::VERSION > "3.1.7.2" - module REXML - class Entity < Child - undef_method :unnormalized - def unnormalized - document.record_entity_expansion! if document - v = value() - return nil if v.nil? - @unnormalized = Text::unnormalize(v, parent) - @unnormalized - end +module REXML + class Entity < Child + undef_method :unnormalized + def unnormalized + document.record_entity_expansion! if document + v = value() + return nil if v.nil? + @unnormalized = Text::unnormalize(v, parent) + @unnormalized + end + end + class Document < Element + @@entity_expansion_limit = 10_000 + def self.entity_expansion_limit= val + @@entity_expansion_limit = val end - class Document < Element - @@entity_expansion_limit = 10_000 - def self.entity_expansion_limit= val - @@entity_expansion_limit = val - end - def record_entity_expansion! - @number_of_expansions ||= 0 - @number_of_expansions += 1 - if @number_of_expansions > @@entity_expansion_limit - raise "Number of entity expansions exceeded, processing aborted." - end + def record_entity_expansion! + @number_of_expansions ||= 0 + @number_of_expansions += 1 + if @number_of_expansions > @@entity_expansion_limit + raise "Number of entity expansions exceeded, processing aborted." end end end -- cgit v1.2.3 From cc17863709882787654403fb20faf4a23eefb5ba Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 9 Sep 2008 23:05:09 -0500 Subject: Revert "Remove the Version check as it's not always available." This reverts commit 6e2851d4261fbbea00e8e74802a507a89af80e2f. --- activesupport/lib/active_support/core_ext/rexml.rb | 42 +++++++++++----------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/rexml.rb b/activesupport/lib/active_support/core_ext/rexml.rb index 058295b057..af8ce3af47 100644 --- a/activesupport/lib/active_support/core_ext/rexml.rb +++ b/activesupport/lib/active_support/core_ext/rexml.rb @@ -5,28 +5,30 @@ require 'rexml/entity' # http://www.ruby-lang.org/en/news/2008/08/23/dos-vulnerability-in-rexml/ # This fix is identical to rexml-expansion-fix version 1.0.1 -module REXML - class Entity < Child - undef_method :unnormalized - def unnormalized - document.record_entity_expansion! if document - v = value() - return nil if v.nil? - @unnormalized = Text::unnormalize(v, parent) - @unnormalized - end - end - class Document < Element - @@entity_expansion_limit = 10_000 - def self.entity_expansion_limit= val - @@entity_expansion_limit = val +unless REXML::VERSION > "3.1.7.2" + module REXML + class Entity < Child + undef_method :unnormalized + def unnormalized + document.record_entity_expansion! if document + v = value() + return nil if v.nil? + @unnormalized = Text::unnormalize(v, parent) + @unnormalized + end end + class Document < Element + @@entity_expansion_limit = 10_000 + def self.entity_expansion_limit= val + @@entity_expansion_limit = val + end - def record_entity_expansion! - @number_of_expansions ||= 0 - @number_of_expansions += 1 - if @number_of_expansions > @@entity_expansion_limit - raise "Number of entity expansions exceeded, processing aborted." + def record_entity_expansion! + @number_of_expansions ||= 0 + @number_of_expansions += 1 + if @number_of_expansions > @@entity_expansion_limit + raise "Number of entity expansions exceeded, processing aborted." + end end end end -- cgit v1.2.3 From 23a0e2c4a9c41936fec0c6e44b946108c701d33b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 9 Sep 2008 23:07:34 -0500 Subject: Fixed that REXML fix would break on earlier versions of ruby (Frederick Cheung) [state:resolved #987] --- activesupport/lib/active_support/core_ext/rexml.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/rexml.rb b/activesupport/lib/active_support/core_ext/rexml.rb index af8ce3af47..baced8111a 100644 --- a/activesupport/lib/active_support/core_ext/rexml.rb +++ b/activesupport/lib/active_support/core_ext/rexml.rb @@ -5,7 +5,9 @@ require 'rexml/entity' # http://www.ruby-lang.org/en/news/2008/08/23/dos-vulnerability-in-rexml/ # This fix is identical to rexml-expansion-fix version 1.0.1 -unless REXML::VERSION > "3.1.7.2" +#earlier versions of rexml defined REXML::Version, newer ones REXML::VERSION +version = defined?(REXML::VERSION) ? REXML::VERSION : REXML::Version +unless version > "3.1.7.2" module REXML class Entity < Child undef_method :unnormalized -- cgit v1.2.3 From 704fc3785eb4c0bca6a37060b551f09489352b6e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 9 Sep 2008 23:13:37 -0500 Subject: No local variable in the global namespace plz --- activesupport/lib/active_support/core_ext/rexml.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/rexml.rb b/activesupport/lib/active_support/core_ext/rexml.rb index baced8111a..187f3e0f5e 100644 --- a/activesupport/lib/active_support/core_ext/rexml.rb +++ b/activesupport/lib/active_support/core_ext/rexml.rb @@ -5,9 +5,8 @@ require 'rexml/entity' # http://www.ruby-lang.org/en/news/2008/08/23/dos-vulnerability-in-rexml/ # This fix is identical to rexml-expansion-fix version 1.0.1 -#earlier versions of rexml defined REXML::Version, newer ones REXML::VERSION -version = defined?(REXML::VERSION) ? REXML::VERSION : REXML::Version -unless version > "3.1.7.2" +# Earlier versions of rexml defined REXML::Version, newer ones REXML::VERSION +unless (defined?(REXML::VERSION) ? REXML::VERSION : REXML::Version) > "3.1.7.2" module REXML class Entity < Child undef_method :unnormalized -- cgit v1.2.3 From 90366a1521659d07a3b75936b3231adeb376f1a4 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 10 Sep 2008 00:26:50 -0500 Subject: Added Inflector#parameterize for easy slug generation ("Donald E. Knuth".parameterize => "donald-e-knuth") #713 [Matt Darby] --- .../lib/active_support/core_ext/string/inflections.rb | 19 +++++++++++++++++++ activesupport/lib/active_support/inflector.rb | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb index 3bbad7dad8..de99fe5791 100644 --- a/activesupport/lib/active_support/core_ext/string/inflections.rb +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -87,6 +87,25 @@ module ActiveSupport #:nodoc: Inflector.demodulize(self) end + # Replaces special characters in a string so that it may be used as part of a 'pretty' URL. + # + # ==== Examples + # + # class Person + # def to_param + # "#{id}-#{name.parameterize}" + # end + # end + # + # @person = Person.find(1) + # # => # + # + # <%= link_to(@person.name, person_path %> + # # => Donald E. Knuth + def parameterize + Inflector.parameterize(self) + end + # Creates the name of a table like Rails does for models to table names. This method # uses the +pluralize+ method on the last word in the string. # diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 7ae9e0c6ab..e5f0063285 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -240,6 +240,25 @@ module ActiveSupport def demodulize(class_name_in_module) class_name_in_module.to_s.gsub(/^.*::/, '') end + + # Replaces special characters in a string so that it may be used as part of a 'pretty' URL. + # + # ==== Examples + # + # class Person + # def to_param + # "#{id}-#{name.parameterize}" + # end + # end + # + # @person = Person.find(1) + # # => # + # + # <%= link_to(@person.name, person_path %> + # # => Donald E. Knuth + def parameterize(string, sep = '-') + string.gsub(/[^a-z0-9]+/i, sep).downcase + end # Create the name of a table like Rails does for models to table names. This method # uses the +pluralize+ method on the last word in the string. -- cgit v1.2.3