Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3356

Show
Ignore:
Timestamp:
12/27/05 03:11:03 (3 years ago)
Author:
sam
Message:

Add ActiveSupport::JSON and Object#to_json for converting Ruby objects to JSON strings

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r3314 r3356  
    11*SVN* 
     2 
     3* Add ActiveSupport::JSON and Object#to_json for converting Ruby objects to JSON strings. [Sam Stephenson] 
    24 
    35* Add Object#with_options for DRYing up multiple calls to methods having shared options. [Sam Stephenson]  Example: 
  • trunk/activesupport/lib/active_support.rb

    r3314 r3356  
    3636 
    3737require 'active_support/values/time_zone' 
     38 
     39require 'active_support/json' 
  • trunk/activesupport/lib/active_support/core_ext/object_and_class.rb

    r3314 r3356  
    5555    yield ActiveSupport::OptionMerger.new(self, options) 
    5656  end 
     57   
     58  def instance_values 
     59    instance_variables.inject({}) do |values, name| 
     60      values[name[1..-1]] = instance_variable_get(name) 
     61      values 
     62    end 
     63  end 
     64   
     65  def to_json 
     66    ActiveSupport::JSON.encode(self) 
     67  end 
    5768end 
    5869 
  • trunk/activesupport/lib/active_support/core_ext/string.rb

    r2171 r3356  
    33require File.dirname(__FILE__) + '/string/access' 
    44require File.dirname(__FILE__) + '/string/starts_ends_with' 
     5require File.dirname(__FILE__) + '/string/iterators' 
    56 
    67class String #:nodoc: 
     
    910  include ActiveSupport::CoreExtensions::String::Inflections 
    1011  include ActiveSupport::CoreExtensions::String::StartsEndsWith 
     12  include ActiveSupport::CoreExtensions::String::Iterators 
    1113end 
  • trunk/activesupport/test/core_ext/object_and_class_ext_test.rb

    r3133 r3356  
    105105    assert_equal 'baz', @dest.instance_variable_get('@baz') 
    106106  end 
     107   
     108  def test_instance_values 
     109    object = Object.new 
     110    object.instance_variable_set :@a, 1 
     111    object.instance_variable_set :@b, 2 
     112    assert_equal({'a' => 1, 'b' => 2}, object.instance_values) 
     113  end 
    107114end 
  • trunk/activesupport/test/core_ext/string_ext_test.rb

    r3127 r3356  
    9090    assert !s.ends_with?('el')   
    9191  end 
     92 
     93  def test_each_char_with_utf8_string_when_kcode_is_utf8 
     94    old_kcode, $KCODE = $KCODE, 'UTF8' 
     95    '€2.99'.each_char do |char| 
     96      assert_not_equal 1, char.length 
     97      break 
     98    end 
     99  ensure 
     100    $KCODE = old_kcode 
     101  end 
    92102end