Skip to content
This repository has been archived by the owner on Sep 17, 2022. It is now read-only.

Enable the use of the "send" method on the configuration object #10

Closed
fusillicode opened this issue Jun 11, 2015 · 6 comments
Closed

Comments

@fusillicode
Copy link

While trying to set up a bunch of options identified by variables name I got stuck with the inability to use the method "send".

Here's the problematic snippet of code:

MyGem.configure do |c|
    # 'configs' and 'cli_options' are just two hashes. I've left out their construction to focus on the key point of the issue
    configs.deep_merge(cli_options).map do |key, value|
      c.send "#{key}=", value
    end
  end
end

If I've left out some important information or if the issue isn't appropriate for some reasons please forgive me.

Cheers.

@beatrichartz
Copy link
Owner

Hi there!

You can use from_h to achieve what you want:

MyGem.configure do |c|
  c.from_h(configs.deep_merge(cli_options))
end

If your configuration is using strict mode (defined attributes), you can use a shorthand:

MyGem.configuration.from_h(configs.deep_merge(cli_options))

If you still want to use send, use __send__

Cheers

@fusillicode
Copy link
Author

Ty a lot for the quick reply!

I think I'm missing something here. I've already tried with from_h but it doesn't works.

If I try to access a configuration value I get nil in response :(

@fusillicode
Copy link
Author

Ok, got it!

The problem are the hash keys. They're strings, not symbols.

Now I've only a "design" question. Personally I think it would be great if configurations would handle transparently strings as well as symbols. From the other side I also think that the user should worry to supply the correct input to the gem.

What do you think? Would you accept a pull request to add this "sugar" to the gem?

P.S: forgive again for the bother and ty again for your helpfulness.

@beatrichartz
Copy link
Owner

Cool. I'll put some thought into handling strings in from_h - the fact it does not handle strings is more incidental atm.

@beatrichartz beatrichartz reopened this Jun 11, 2015
@fusillicode
Copy link
Author

Roger that.

Anyway, jftr, a quick fix could be to explicitly transform the property passed to the __assign! method in the Configuration class (line 153) to a symbol.

If there is something I can do let me know, I'll stick around ;)

Ty again.

@beatrichartz
Copy link
Owner

Released configuration with hashes with string keys in 2.1.0.

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

No branches or pull requests

2 participants