Changeset 6443
- Timestamp:
- 03/18/07 07:05:58 (2 years ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/blank.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/object/extending.rb (modified) (4 diffs)
- trunk/activesupport/lib/active_support/core_ext/object/misc.rb (modified) (2 diffs)
- trunk/activesupport/lib/active_support/dependencies.rb (modified) (2 diffs)
- trunk/activesupport/lib/active_support/json.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/decoding.rb (added)
- trunk/activesupport/lib/active_support/json/encoders.rb (deleted)
- trunk/activesupport/lib/active_support/json/encoders/core.rb (deleted)
- trunk/activesupport/lib/active_support/json/encoders/enumerable.rb (added)
- trunk/activesupport/lib/active_support/json/encoders/false_class.rb (added)
- trunk/activesupport/lib/active_support/json/encoders/hash.rb (added)
- trunk/activesupport/lib/active_support/json/encoders/nil_class.rb (added)
- trunk/activesupport/lib/active_support/json/encoders/numeric.rb (added)
- trunk/activesupport/lib/active_support/json/encoders/object.rb (added)
- trunk/activesupport/lib/active_support/json/encoders/regexp.rb (added)
- trunk/activesupport/lib/active_support/json/encoders/string.rb (added)
- trunk/activesupport/lib/active_support/json/encoders/symbol.rb (added)
- trunk/activesupport/lib/active_support/json/encoders/true_class.rb (added)
- trunk/activesupport/lib/active_support/json/encoding.rb (added)
- trunk/activesupport/lib/active_support/json/variable.rb (added)
- trunk/activesupport/lib/active_support/vendor/builder/blankslate.rb (modified) (2 diffs)
- trunk/activesupport/test/json (added)
- trunk/activesupport/test/json.rb (deleted)
- trunk/activesupport/test/json/decoding_test.rb (added)
- trunk/activesupport/test/json/encoding_test.rb (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r6441 r6443 1 1 *SVN* 2 3 * Refactor ActiveSupport::JSON to be less obtuse. Add support for JSON decoding by way of Syck with ActiveSupport::JSON.decode(json_string). Prevent hash keys that are JavaScript reserved words from being unquoted during encoding. [Sam Stephenson] 2 4 3 5 * alias_method_chain preserves the original method's visibility. #7854 [Jonathan Viney] trunk/activesupport/lib/active_support/core_ext/blank.rb
r3493 r6443 1 class Object #:nodoc:1 class Object 2 2 # "", " ", nil, [], and {} are blank 3 def blank? 3 def blank? #:nodoc: 4 4 if respond_to?(:empty?) && respond_to?(:strip) 5 5 empty? or strip.empty? trunk/activesupport/lib/active_support/core_ext/object/extending.rb
r6036 r6443 1 class Object #:nodoc:2 def remove_subclasses_of(*superclasses) 1 class Object 2 def remove_subclasses_of(*superclasses) #:nodoc: 3 3 Class.remove_class(*subclasses_of(*superclasses)) 4 4 end 5 5 6 def subclasses_of(*superclasses) 6 def subclasses_of(*superclasses) #:nodoc: 7 7 subclasses = [] 8 8 ObjectSpace.each_object(Class) do |k| … … 17 17 end 18 18 19 def extended_by 19 def extended_by #:nodoc: 20 20 ancestors = class << self; ancestors end 21 21 ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ] 22 22 end 23 23 24 def copy_instance_variables_from(object, exclude = []) 24 def copy_instance_variables_from(object, exclude = []) #:nodoc: 25 25 exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables 26 26 … … 29 29 end 30 30 31 def extend_with_included_modules_from(object) 31 def extend_with_included_modules_from(object) #:nodoc: 32 32 object.extended_by.each { |mod| extend mod } 33 33 end 34 34 35 def instance_values 35 def instance_values #:nodoc: 36 36 instance_variables.inject({}) do |values, name| 37 37 values[name[1..-1]] = instance_variable_get(name) … … 41 41 42 42 unless defined? instance_exec # 1.9 43 def instance_exec(*arguments, &block) 43 def instance_exec(*arguments, &block) #:nodoc: 44 44 block.bind(self)[*arguments] 45 45 end trunk/activesupport/lib/active_support/core_ext/object/misc.rb
r6011 r6443 44 44 end 45 45 46 # Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.47 #48 # Account.find(1).to_json49 # => "{attributes: {username: \"foo\", id: \"1\", password: \"bar\"}}"50 #51 def to_json52 ActiveSupport::JSON.encode(self)53 end54 55 46 # A duck-type assistant method. For example, ActiveSupport extends Date 56 47 # to define an acts_like_date? method, and extends Time to define … … 59 50 # we want to act like Time simply need to define an acts_like_time? method. 60 51 def acts_like?(duck) 61 respond_to? :"acts_like_#{duck}?"52 respond_to? "acts_like_#{duck}?" 62 53 end 63 54 end trunk/activesupport/lib/active_support/dependencies.rb
r6426 r6443 481 481 end 482 482 483 class Object #:nodoc:483 class Object 484 484 485 485 alias_method :load_without_new_constant_marking, :load 486 486 487 def load(file, *extras) 487 def load(file, *extras) #:nodoc: 488 488 Dependencies.new_constants_in(Object) { super(file, *extras) } 489 489 rescue Exception => exception # errors from loading file … … 492 492 end 493 493 494 def require(file, *extras) 494 def require(file, *extras) #:nodoc: 495 495 Dependencies.new_constants_in(Object) { super(file, *extras) } 496 496 rescue Exception => exception # errors from required file trunk/activesupport/lib/active_support/json.rb
r5486 r6443 1 require 'active_support/json/encoders' 1 require 'active_support/json/encoding' 2 require 'active_support/json/decoding' 2 3 3 4 module ActiveSupport 4 module JSON #:nodoc: 5 class CircularReferenceError < StandardError #:nodoc: 6 end 7 8 # A string that returns itself as as its JSON-encoded form. 9 class Variable < String #:nodoc: 10 def to_json 11 self 12 end 13 end 14 15 # When +true+, Hash#to_json will omit quoting string or symbol keys 16 # if the keys are valid JavaScript identifiers. Note that this is 17 # technically improper JSON (all object keys must be quoted), so if 18 # you need strict JSON compliance, set this option to +false+. 19 mattr_accessor :unquote_hash_key_identifiers 20 @@unquote_hash_key_identifiers = true 5 module JSON 6 RESERVED_WORDS = %w( 7 abstract delete goto private transient 8 boolean do if protected try 9 break double implements public typeof 10 byte else import return var 11 case enum in short void 12 catch export instanceof static volatile 13 char extends int super while 14 class final interface switch with 15 const finally long synchronized 16 continue float native this 17 debugger for new throw 18 default function package throws 19 ) #:nodoc: 21 20 22 21 class << self 23 REFERENCE_STACK_VARIABLE = :json_reference_stack 24 25 def encode(value) 26 raise_on_circular_reference(value) do 27 Encoders[value.class].call(value) 28 end 22 def valid_identifier?(key) #:nodoc: 23 key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/ && !reserved_word?(key) 29 24 end 30 31 def can_unquote_identifier?(key) 32 return false unless unquote_hash_key_identifiers 33 key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/ 25 26 def reserved_word?(key) #:nodoc: 27 RESERVED_WORDS.include?(key.to_s) 34 28 end 35 36 protected37 def raise_on_circular_reference(value)38 stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= []39 raise CircularReferenceError, 'object references itself' if40 stack.include? value41 stack << value42 yield43 ensure44 stack.pop45 end46 29 end 47 30 end trunk/activesupport/lib/active_support/vendor/builder/blankslate.rb
r4260 r6443 49 49 end 50 50 51 class Object #:nodoc:51 class Object 52 52 class << self 53 53 alias_method :blank_slate_method_added, :method_added … … 55 55 # Detect method additions to Object and remove them in the 56 56 # BlankSlate class. 57 def method_added(name) 57 def method_added(name) #:nodoc: 58 58 blank_slate_method_added(name) 59 59 return if self != Object