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

Replace nils by empty strings? #402

Open
manuelmeurer opened this issue Feb 7, 2013 · 2 comments
Open

Replace nils by empty strings? #402

manuelmeurer opened this issue Feb 7, 2013 · 2 comments

Comments

@manuelmeurer
Copy link
Contributor

One requirement for the API I'm writing with Rabl is that there should be no nil/null values in the JSON response. I quickly did this by monkeypatching Rabl::Builder#compile_hash.
Would you accept a pull request that implements this propery and adds a config.replace_nil_by_empty_string configuration option?

@nesquena
Copy link
Owner

Yes, that would be a welcome property that can be enabled in configuration

@manuelmeurer
Copy link
Contributor Author

Ok, I will work on that as soon as I have some time. (famous last words)

For now, here's my monkeypatch:

class Rabl::Builder
  def compile_hash_with_replace_nil_by_empty_string(options = {})
    result = compile_hash_without_replace_nil_by_empty_string(options)
    @_result = @_result.each_with_object({}) do |(k, v), hash|
      hash[k] = v.nil? ? '' : v
    end
    @_root_name ? { @_root_name => @_result } : @_result
  end

  alias_method_chain :compile_hash, :replace_nil_by_empty_string
end

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

No branches or pull requests

2 participants