Dry::Inflector lower camel case support

dry-inflector lacks support for lower case camel case conversion like ActiveSupport has, this would useful to have when generating strings for JSON attributes names.

inflector = Dry::Inflector.new

inflector.lower_camelize("first_name") # => "firstName"
inflector.lower_camelize("name") # => "name"

# Or maybe:

inflector.camelize("first_name", lower: true) # => "firstName"
inflector.camelize("name", lower: true) # => "name"

Copied from https://github.com/Ptico/flexus/issues/3, so we can keep discussing this feature here.

1 Like

My suggestion from the previous discussion is that we should favor the two forms lower_camelize and upper_camelize because they are way more clear and with less parameters. So, we should document these 2 forms.

But we should offer (undocumented) camelize as an alias to upper_camelize preserving the current behaviour and back compatibility. Also we should add the option of camelize("string", lower: true) as an alias to lower_camelize so people coming from ActiveSupport feel themselves at home.

This is really easy to implement. Just tell you all agree and I’ll do it.

Agree @Ptico, @jodosha, @solnic, @pabloh ?

This sounds new to me. Do you have examples to share about the new syntax? Both for the old methods and the new ones. Thanks!

I’d prefer to have camelize_lower and camelize_upper

Also we should add the option of camelize(“string”, lower: true)

Agreed, this should help people transition to dry-inflector.

1 Like

I don’t see any problem with camelize_lower.

Today we have

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.camelize("first_name") # => "FirstName"

In some set people need the lower version of camelize, where only the first character of the sentence is not uppercased.
ActiveSupport has provide this as an optional argument.

# camelize(term, uppercase_first_letter = true) 
camelize('active_model')                # => "ActiveModel"
camelize('active_model', false)         # => "activeModel"

Details at: http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-camelize

At a previous discussion at the flexus repo, @pabloh has suggested we should provide the same, but with a keyword argument, like this:

inflector.camelize('active_model', lower: true) # => "activeModel"

Details at: https://github.com/Ptico/flexus/issues/3

I have suggested that we shouldn’t go to this polymorphic behaviour through arguments, instead we should provide 2 different methods that clearly states what the method is about.

With the “camelize / camelize with lower set to true” approach, when facing a simple camelize without the argument, one will probably have to make some effort to remember what is the “default” camelize behavior, lower or upper.

With the “camelize_lower / camelize_upper” without any argument, we have a more clear api, in my humble opinion.

As a middle way approach, we could favor the camelize_lower / camelize_upper as the official and documented api. But, keep on providing the old camelize behavior as alias to these official methods. Then we could have the best of both worlds.

@jodosha, I hope this clarifies the issue a little bit.

Something came to mind as I was looking at the code snippets.

# If we provide:
inflector.camelize("some_string", upper: false)

# instead of
inflector.camelize("some_string", lower: true)

# will be more symetric to the active_support signature
camelize("some_string", uppercase_first_letter = true)

And can potentially ease transition from AS to Dry::Inflector.

PR open at https://github.com/dry-rb/dry-inflector/pull/17

@solnic I tried to better think about the method names. (although I said no problem with your suggestion up above).

Looking at “upper_camelize” closely I think it’s more (english) correct as upper is the adjective and goes before the verb. Like Dry and Inflector. dry-inflector not inflector-dry.

Also see Ruby’s String #upcase and #downcase methods.

What do you think?

We could also think about the upcamelize and downcamelize option, as they would be symetric to upcase and downcase. Something to think about.

camelize_upper/camelize_lower is a good idea, because we will also have handful autocomplete in IDEs and REPLs, like camel and see the options

1 Like

@Ptico exactly, also listing sorted methods on an inflector object would put them next to each other

1 Like

Ok. No problem. 2 votes against one. The PR is already this way. Can someone review and merge?

Folks, sorry for being late. I’ve reviewed the PR. I’m skeptical about these new methods. Can you please reply over the PR too? Thanks! :green_heart:

1 Like

Hi @jodosha, good review. I have answered at the PR.