Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2740

__nested__ for hash doesn't work with integer keys.

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.1.0
    • Affects Version/s: None
    • Component/s: None
    • None

      Example spec (https://github.com/matsimitsu/mongoid/blob/master/spec/mongoid/extensions/hash_spec.rb#L220)

       ruby
        describe "#__nested__" do
      
          context "when the hash key is a string" do
      
            let(:hash) do
              { '100' => { 'name' => 'hundred' } }
            end
      
            let(:nested) do
              hash.__nested__('100.name')
            end
      
            it "should retrieve a nested value under the provided key" do
              nested.should eq 'hundred'
            end
      
          end
      
          context "when the hash key is an integer" do
      
            let(:hash) do
              { 100 => { 'name' => 'hundred' } }
            end
      
            let(:nested) do
              hash.__nested__('100.name')
            end
      
            it "should retrieve a nested value under the provided key" do
              nested.should eq 'hundred'
            end
      
          end
      
        end
      

      result:

       ruby
      Mongoid::Extensions::Hash
        #__nested__
          when the hash key is a string
            should retrieve a nested value under the provided key
          when the hash key is an integer
            should retrieve a nested value under the provided key (FAILED - 1)
      
      Failures:
      
        1) Mongoid::Extensions::Hash#__nested__ when the hash key is an integer should retrieve a nested value under the provided key
           Failure/Error: hash.__nested__('100.name')
           NoMethodError:
             undefined method `[]' for nil:NilClass
           # ./lib/mongoid/extensions/hash.rb:104:in `block in __nested__'
           # ./lib/mongoid/extensions/hash.rb:103:in `each'
           # ./lib/mongoid/extensions/hash.rb:103:in `__nested__'
           # ./spec/mongoid/extensions/hash_spec.rb:245:in `block (4 levels) in <top (required)>'
           # ./spec/mongoid/extensions/hash_spec.rb:249:in `block (4 levels) in <top (required)>'
      
      Finished in 0.00824 seconds
      2 examples, 1 failure
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            matsimitsu robert beekman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: