huacnlee / rails-settings-cached

Global settings for your Rails application.
Other
1.06k stars 202 forks source link

Feature: Automatically validate the type when setting a value instead of casting it #203

Closed sjieg closed 3 weeks ago

sjieg commented 3 years ago

Hello, first of all, good stuff with this gem! Very useful making global app settings dynamic, but also to quickly create a single variable to "remember" something. Props to all the contributors and maintainer!

So I might be wrong, because I just started using this gem, but it seems that the type set for a field is being cast:

# field :foo, type: :integer
Setting.foo = 'hello' # => 0

What do you think about making it so that if the type is set and a different type is given, an error is thrown?

# field :foo, type: :integer
Setting.foo = 'hello' # InvalidTypeError - Got type String, expected type Integer
# field :bar, type :date_time
Setting.bar = '2020-12-30' # InvalidTypeErrror - Got type String, expected type DateTime

Personally, I feel like this will make it more robust to use because:

Like I said, I just started using the gem, so I might be overlooking something obvious.

WDYT? Could this be a feature for v3? :)

huacnlee commented 3 weeks ago

Close this, we have validates feature may solve this.