Skip to content
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

DRY ActiveResource decode methods #40

Open
jistr opened this issue Jan 31, 2013 · 1 comment
Open

DRY ActiveResource decode methods #40

jistr opened this issue Jan 31, 2013 · 1 comment

Comments

@jistr
Copy link
Contributor

jistr commented Jan 31, 2013

A lot of the XML decode methods [1] will probably repeat itself across models, so we could extract it as common behavior.

[1] https://github.com/aeolus-incubator/thor-cli/blob/master/lib/aeolus_cli/model/provider.rb#L4-L29

EDIT after pblaho's comment:

Here's a permalink:

def decode(xml)
if xml == nil or xml.size == 0
return nil
end
if xml.start_with?('<providers>')
h = ActiveResource::Formats.remove_root(Hash.from_xml(xml))
# h is a hash of 'provider' => array of provider hashes
if h['provider'].is_a?(Hash)
# Having just one provider causes the value of 'provider' to
# not exist in a array, so we wrap it in an array to prevent
# an error like:
# ...active_resource/base.rb:929:in `instantiate_collection':
# undefined method `collect!' for #<Hash:0x0000000102e800> (NoMethodError)
[h['provider']]
else
# return value is an array of hashes
h['provider']
end
elsif xml.start_with?('<provider ') or xml.start_with?('<provider>')
Hash.from_xml(xml)
else
# TODO raise an appropriate error that we didn't get <providers>
# or <provider> xml
nil
end
end

@petrblaho
Copy link
Contributor

Link is no longer valid after renaming of files.

Here is new one - https://github.com/aeolus-incubator/thor-cli/blob/master/lib/aeolus/client/provider.rb#L6-L31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants