-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Teach #from_hash to raise TypeError when argument is not a Hash #203
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,14 @@ def items(options={}, &block) | |
|
||
# TODO: revise lonely collection and build separate pipeline where we just use Serialize, etc. | ||
|
||
def from_hash(data, options={}, binding_builder=Binding) | ||
data = filter_wrap(data, options) | ||
data = [data] if ::Hash === data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @apotonick this makes |
||
raise TypeError, "Expected Enumerable, got #{data.class}." unless data.respond_to?(:each) | ||
|
||
update_properties_from(data, options, binding_builder) | ||
end | ||
|
||
def create_representation_with(doc, options, format) | ||
options = normalize_options(options) | ||
options[:_self] = options | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,10 +76,15 @@ class BandDecorator < Representable::Decorator | |
|
||
it do | ||
band.from_hash({"bands" => {"name"=>"Social Distortion"}}).name.must_equal "Social Distortion" | ||
band.from_hash({"name"=>"Social Distortion"}, wrap: false).name.must_equal "Social Distortion" | ||
band.from_hash({band: {"name"=>"Social Distortion"}}, wrap: :band).name.must_equal "Social Distortion" | ||
band.from_hash({"name"=>"Bad Religion"}, wrap: false).name.must_equal "Bad Religion" | ||
band.from_hash({"band"=>{"name"=>"Pennywise"}}, wrap: :band).name.must_equal "Pennywise" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @apotonick as long as |
||
end | ||
|
||
it 'raises a TypeError when unwrapped argument is not a Hash' do | ||
-> { band.from_hash('bands' => '') }.must_raise TypeError | ||
-> { band.from_hash('', wrap: false) }.must_raise TypeError | ||
-> { band.from_hash({'band' => ''}, wrap: :band) }.must_raise TypeError | ||
end | ||
|
||
class AlbumDecorator < Representable::Decorator | ||
include Representable::Hash | ||
|
@@ -149,4 +154,3 @@ class AlbumDecorator < Representable::Decorator | |
# album.from_hash({"albums" => {"band" => {"name"=>"Rvivr"}}}).band.name.must_equal "Rvivr" | ||
# end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@apotonick could rely on duck typing here (
unless data.respond_to?(:has_key?)
, but I think this approach is probably safer.