From e567a5eb1afe1ac38f1da37f1c1e3922bbf79d2a Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Tue, 27 Dec 2005 03:11:03 +0000 Subject: Add ActiveSupport::JSON and Object#to_json for converting Ruby objects to JSON strings git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3356 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/object_and_class.rb | 11 +++++++++++ activesupport/lib/active_support/core_ext/string.rb | 2 ++ .../lib/active_support/core_ext/string/iterators.rb | 17 +++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/string/iterators.rb (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/object_and_class.rb b/activesupport/lib/active_support/core_ext/object_and_class.rb index aef1f22c6f..4856a9f5d5 100644 --- a/activesupport/lib/active_support/core_ext/object_and_class.rb +++ b/activesupport/lib/active_support/core_ext/object_and_class.rb @@ -54,6 +54,17 @@ class Object #:nodoc: def with_options(options) yield ActiveSupport::OptionMerger.new(self, options) end + + def instance_values + instance_variables.inject({}) do |values, name| + values[name[1..-1]] = instance_variable_get(name) + values + end + end + + def to_json + ActiveSupport::JSON.encode(self) + end end class Class #:nodoc: diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb index 979f8a0515..240e1ff1da 100644 --- a/activesupport/lib/active_support/core_ext/string.rb +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -2,10 +2,12 @@ require File.dirname(__FILE__) + '/string/inflections' require File.dirname(__FILE__) + '/string/conversions' require File.dirname(__FILE__) + '/string/access' require File.dirname(__FILE__) + '/string/starts_ends_with' +require File.dirname(__FILE__) + '/string/iterators' class String #:nodoc: include ActiveSupport::CoreExtensions::String::Access include ActiveSupport::CoreExtensions::String::Conversions include ActiveSupport::CoreExtensions::String::Inflections include ActiveSupport::CoreExtensions::String::StartsEndsWith + include ActiveSupport::CoreExtensions::String::Iterators end diff --git a/activesupport/lib/active_support/core_ext/string/iterators.rb b/activesupport/lib/active_support/core_ext/string/iterators.rb new file mode 100644 index 0000000000..73114d9d5f --- /dev/null +++ b/activesupport/lib/active_support/core_ext/string/iterators.rb @@ -0,0 +1,17 @@ +require 'strscan' + +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module String #:nodoc: + # Custom string iterators + module Iterators + # Yields a single-character string for each character in the string. + # When $KCODE = 'UTF8', multi-byte characters are yielded appropriately. + def each_char + scanner, char = StringScanner.new(self), /./mu + loop { yield(scanner.scan(char) || break) } + end + end + end + end +end -- cgit v1.2.3