Changeset 4387
- Timestamp:
- 05/31/06 23:25:36 (2 years ago)
- Files:
-
- trunk/activerecord/test/base_test.rb (modified) (1 diff)
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/array.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/array/conversions.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/array/grouping.rb (added)
- trunk/activesupport/test/core_ext/array_ext_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/test/base_test.rb
r4314 r4387 933 933 end 934 934 935 def test_quoting_arrays 936 replies = Reply.find(:all, :conditions => [ "id IN (?)", topics(:first).replies.to_s(:db) ]) 937 assert_equal topics(:first).replies.size, replies.size 938 939 replies = Reply.find(:all, :conditions => [ "id IN (?)", [].to_s(:db) ]) 940 assert_equal 0, replies.size 941 end 942 935 943 MyObject = Struct.new :attribute1, :attribute2 936 944 trunk/activesupport/CHANGELOG
r4359 r4387 1 1 *SVN* 2 3 * Added Array#to_s(:db) that'll produce a comma-separated list of ids [DHH]. Example: 4 5 Purchase.find(:all, :conditions => [ "product_id IN (?)", shop.products.to_s(:db) ]) 2 6 3 7 * Normalize classify's argument to a String so that it plays nice with Symbols. [Marcel Molina Jr.] trunk/activesupport/lib/active_support/core_ext/array.rb
r4345 r4387 1 1 require File.dirname(__FILE__) + '/array/conversions' 2 require File.dirname(__FILE__) + '/array/grouping' 2 3 3 4 class Array #:nodoc: 4 5 include ActiveSupport::CoreExtensions::Array::Conversions 5 6 # Iterate over an array in groups of a certain size, padding any remaining 7 # slots with specified value (<tt>nil</tt> by default). 8 # 9 # E.g. 10 # 11 # %w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g} 12 # ["1", "2", "3"] 13 # ["4", "5", "6"] 14 # ["7", nil, nil] 15 def in_groups_of(number, fill_with = nil, &block) 16 require 'enumerator' 17 collection = dup 18 collection << fill_with until collection.size.modulo(number).zero? 19 grouped_collection = [] unless block_given? 20 collection.each_slice(number) do |group| 21 block_given? ? yield(group) : grouped_collection << group 22 end 23 grouped_collection unless block_given? 24 end 25 26 # Divide the array into one or more subarrays based on a delimiting +value+ 27 # or the result of an optional block. 28 # 29 # ex. 30 # 31 # [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]] 32 # (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]] 33 def split(value = nil, &block) 34 block ||= Proc.new { |e| e == value } 35 inject([[]]) do |results, element| 36 if block.call(element) 37 results << [] 38 else 39 results.last << element 40 end 41 results 42 end 43 end 6 include ActiveSupport::CoreExtensions::Array::Grouping 44 7 end trunk/activesupport/lib/active_support/core_ext/array/conversions.rb
r3901 r4387 27 27 end 28 28 29 def self.included(klass) #:nodoc: 30 klass.send(:alias_method, :to_default_s, :to_s) 31 klass.send(:alias_method, :to_s, :to_formatted_s) 32 end 33 34 def to_formatted_s(format = :default) 35 case format 36 when :db 37 if respond_to?(:empty?) && self.empty? 38 "null" 39 else 40 collect { |element| element.id }.join(",") 41 end 42 else 43 to_default_s 44 end 45 end 46 29 47 def to_xml(options = {}) 30 48 raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } trunk/activesupport/test/core_ext/array_ext_test.rb
r4345 r4387 14 14 end 15 15 16 class ArrayExt ConversionTests < Test::Unit::TestCase16 class ArrayExtToSentenceTests < Test::Unit::TestCase 17 17 def test_plain_array_to_sentence 18 18 assert_equal "", [].to_sentence … … 37 37 def test_one_element 38 38 assert_equal "one", ['one'].to_sentence 39 end 40 end 41 42 class ArrayExtToSTests < Test::Unit::TestCase 43 def test_to_s_db 44 collection = [ 45 Class.new { def id() 1 end }.new, 46 Class.new { def id() 2 end }.new, 47 Class.new { def id() 3 end }.new 48 ] 49 50 assert_equal "null", [].to_s(:db) 51 assert_equal "1,2,3", collection.to_s(:db) 39 52 end 40 53 end