Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IgnoreUndeclared, an extension to silently ignore undeclared pr…
…operties at hash intialization.
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Hashie | ||
module Extensions | ||
# IgnoreUndeclared is a simple mixin that silently ignores | ||
# undeclared properties on initialization instead of | ||
# raising an error. This is useful when using a Trash to | ||
# capture a subset of a larger hash. | ||
# | ||
# Note that attempting to retrieve an undeclared property | ||
# will still raise a NoMethodError, even if a value for | ||
# that property was provided at initialization. | ||
# | ||
# @example | ||
# class Person < Trash | ||
# include Hashie::Extensions::IgnoreUndeclared | ||
# | ||
# property :first_name | ||
# property :last_name | ||
# end | ||
# | ||
# user_data = { | ||
# :first_name => 'Freddy', | ||
# :last_name => 'Nostrils', | ||
# :email => 'freddy@example.com' | ||
# } | ||
# | ||
# p = Person.new(user_data) # 'email' is silently ignored | ||
# | ||
# p.first_name # => 'Freddy' | ||
# p.last_name # => 'Nostrils' | ||
# p.email # => NoMethodError | ||
module IgnoreUndeclared | ||
def initialize_attributes(attributes) | ||
attributes.each_pair do |att, value| | ||
if self.class.property?(att) || (self.class.respond_to?(:translations) && self.class.translations.include?(att.to_sym)) | ||
self[att] = value | ||
end | ||
end if attributes | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'spec_helper' | ||
|
||
describe Hashie::Extensions::IgnoreUndeclared do | ||
class ForgivingTrash < Hashie::Trash | ||
include Hashie::Extensions::IgnoreUndeclared | ||
property :city | ||
property :state, from: :provence | ||
end | ||
|
||
subject { ForgivingTrash } | ||
|
||
it 'should silently ignore undeclared properties on initialization' do | ||
expect { subject.new(city: 'Toronto', provence: 'ON', country: 'Canada') }.to_not raise_error | ||
end | ||
|
||
it 'should work with translated properties (with symbol keys)' do | ||
expect(subject.new(provence: 'Ontario').state).to eq('Ontario') | ||
end | ||
|
||
it 'should work with translated properties (with string keys)' do | ||
expect(subject.new(provence: 'Ontario').state).to eq('Ontario') | ||
end | ||
end |