Sha256: 53f3b849ab69ab9521e2c4700d1534aeaaaf6e718d50abf8f74b68218ba0526c

Contents?: true

Size: 1.49 KB

Versions: 15

Compression:

Stored size: 1.49 KB

Contents

require 'spec_helper'

describe Hashie::Extensions::IgnoreUndeclared do
  context 'included in Trash' do
    class ForgivingTrash < Hashie::Trash
      include Hashie::Extensions::IgnoreUndeclared
      property :city
      property :state, from: :provence
    end

    subject { ForgivingTrash }

    it 'silently ignores undeclared properties on initialization' do
      expect { subject.new(city: 'Toronto', provence: 'ON', country: 'Canada') }.to_not raise_error
    end

    it 'works with translated properties (with symbol keys)' do
      expect(subject.new(provence: 'Ontario').state).to eq('Ontario')
    end

    it 'works with translated properties (with string keys)' do
      expect(subject.new(provence: 'Ontario').state).to eq('Ontario')
    end

    it 'requires properties to be declared on assignment' do
      hash = subject.new(city: 'Toronto')
      expect { hash.country = 'Canada' }.to raise_error(NoMethodError)
    end
  end

  context 'combined with DeepMerge' do
    class ForgivingTrashWithMerge < Hashie::Trash
      include Hashie::Extensions::DeepMerge
      include Hashie::Extensions::IgnoreUndeclared
      property :some_key
    end

    it 'deep merges' do
      class ForgivingTrashWithMergeAndProperty < ForgivingTrashWithMerge
        property :some_other_key
      end
      hash = ForgivingTrashWithMergeAndProperty.new(some_ignored_key: 17, some_key: 12)
      expect(hash.deep_merge(some_other_key: 55, some_ignored_key: 18)).to eq(some_key: 12, some_other_key: 55)
    end
  end
end

Version data entries

15 entries across 13 versions & 4 rubygems

Version Path
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/hashie-3.4.3/spec/hashie/extensions/ignore_undeclared_spec.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/hashie-3.4.3/spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.4.3 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.4.2 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.4.1 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.4.0 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.3.2 spec/hashie/extensions/ignore_undeclared_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/hashie-3.3.1/spec/hashie/extensions/ignore_undeclared_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/hashie-3.3.1/spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.3.1 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.2.0 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.1.0 spec/hashie/extensions/ignore_undeclared_spec.rb
whos_dated_who-0.1.0 vendor/bundle/gems/hashie-3.0.0/spec/hashie/extensions/ignore_undeclared_spec.rb
whos_dated_who-0.0.1 vendor/bundle/gems/hashie-3.0.0/spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.0.0 spec/hashie/extensions/ignore_undeclared_spec.rb