Sha256: 2b8955066aad524a60ebba473b6269fe479ac1fbbf6241080149fbe8480d9bba

Contents?: true

Size: 1.55 KB

Versions: 18

Compression:

Stored size: 1.55 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
      property :str_state, from: 'str_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('str_provence' => 'Ontario').str_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

18 entries across 17 versions & 4 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/hashie-3.6.0/spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.6.0 spec/hashie/extensions/ignore_undeclared_spec.rb
tdiary-5.0.8 vendor/bundle/gems/hashie-3.5.7/spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.5.7 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.5.6 spec/hashie/extensions/ignore_undeclared_spec.rb
tdiary-5.0.5 vendor/bundle/gems/hashie-3.5.5/spec/hashie/extensions/ignore_undeclared_spec.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/hashie-3.5.5/spec/hashie/extensions/ignore_undeclared_spec.rb
tdiary-5.0.4 vendor/bundle/gems/hashie-3.5.5/spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.5.5 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.5.4 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.5.3 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.5.2 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.5.1 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.5.0 spec/hashie/extensions/ignore_undeclared_spec.rb
logstash-input-salesforce-3.0.0 vendor/jruby/1.9/gems/hashie-3.4.6/spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.4.6 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.4.5 spec/hashie/extensions/ignore_undeclared_spec.rb
hashie-3.4.4 spec/hashie/extensions/ignore_undeclared_spec.rb