added pluralized_array method to Hash and tests
This commit is contained in:
		
							parent
							
								
									6b74454a07
								
							
						
					
					
						commit
						e0e4a47af1
					
				| 
						 | 
				
			
			@ -19,6 +19,28 @@ class Hash
 | 
			
		|||
 | 
			
		||||
    target
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Read array from the supplied hash favouring the singular key
 | 
			
		||||
  # and then the plural key, and handling any nil entries.
 | 
			
		||||
  #   +hash+ the hash to read from
 | 
			
		||||
  #   +singular_key+ the singular key
 | 
			
		||||
  #   +plural_key+ the singular key
 | 
			
		||||
  #
 | 
			
		||||
  # Returns an array
 | 
			
		||||
  def pluralized_array(singular_key, plural_key)
 | 
			
		||||
    hash = self
 | 
			
		||||
    if hash.has_key?(singular_key)
 | 
			
		||||
      array = [hash[singular_key]] if hash[singular_key]
 | 
			
		||||
    elsif hash.has_key?(plural_key)
 | 
			
		||||
      case hash[plural_key]
 | 
			
		||||
      when String
 | 
			
		||||
        array = hash[plural_key].split
 | 
			
		||||
      when Array
 | 
			
		||||
        array = hash[plural_key].compact
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
    array || []
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
# Thanks, ActiveSupport!
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,66 @@
 | 
			
		|||
require File.dirname(__FILE__) + '/helper'
 | 
			
		||||
 | 
			
		||||
class TestCoreExt < Test::Unit::TestCase
 | 
			
		||||
  context "hash" do
 | 
			
		||||
 | 
			
		||||
    context "pluralized_array" do
 | 
			
		||||
 | 
			
		||||
      should "return empty array with no values" do
 | 
			
		||||
        data = {}
 | 
			
		||||
        assert_equal [], data.pluralized_array('tag', 'tags')
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      should "return empty array with no matching values" do
 | 
			
		||||
        data = { 'foo' => 'bar' }
 | 
			
		||||
        assert_equal [], data.pluralized_array('tag', 'tags')
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      should "return empty array with matching nil singular" do
 | 
			
		||||
        data = { 'foo' => 'bar', 'tag' => nil, 'tags' => ['dog', 'cat'] }
 | 
			
		||||
        assert_equal [], data.pluralized_array('tag', 'tags')
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      should "return single value array with matching singular" do
 | 
			
		||||
        data = { 'foo' => 'bar', 'tag' => 'dog', 'tags' => ['dog', 'cat'] }
 | 
			
		||||
        assert_equal ['dog'], data.pluralized_array('tag', 'tags')
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      should "return single value array with matching singular with spaces" do
 | 
			
		||||
        data = { 'foo' => 'bar', 'tag' => 'dog cat', 'tags' => ['dog', 'cat'] }
 | 
			
		||||
        assert_equal ['dog cat'], data.pluralized_array('tag', 'tags')
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      should "return empty array with matching nil plural" do
 | 
			
		||||
        data = { 'foo' => 'bar', 'tags' => nil }
 | 
			
		||||
        assert_equal [], data.pluralized_array('tag', 'tags')
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      should "return empty array with matching empty array" do
 | 
			
		||||
        data = { 'foo' => 'bar', 'tags' => [] }
 | 
			
		||||
        assert_equal [], data.pluralized_array('tag', 'tags')
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      should "return single value array with matching plural with single string value" do
 | 
			
		||||
        data = { 'foo' => 'bar', 'tags' => 'dog' }
 | 
			
		||||
        assert_equal ['dog'], data.pluralized_array('tag', 'tags')
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      should "return multiple value array with matching plural with single string value with spaces" do
 | 
			
		||||
        data = { 'foo' => 'bar', 'tags' => 'dog cat' }
 | 
			
		||||
        assert_equal ['dog', 'cat'], data.pluralized_array('tag', 'tags')
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      should "return single value array with matching plural with single value array" do
 | 
			
		||||
        data = { 'foo' => 'bar', 'tags' => ['dog'] }
 | 
			
		||||
        assert_equal ['dog'], data.pluralized_array('tag', 'tags')
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      should "return multiple value array with matching plural with multiple value array" do
 | 
			
		||||
        data = { 'foo' => 'bar', 'tags' => ['dog', 'cat'] }
 | 
			
		||||
        assert_equal ['dog', 'cat'], data.pluralized_array('tag', 'tags')
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
		Reference in New Issue