Skip to content

Reorganize module options#50

Merged
shioyama merged 26 commits into
masterfrom
module_options
Jul 17, 2017
Merged

Reorganize module options#50
shioyama merged 26 commits into
masterfrom
module_options

Conversation

@shioyama
Copy link
Copy Markdown
Owner

@shioyama shioyama commented Jul 8, 2017

Currently there is a lot of hard-coded dependencies between options-setting in
the Mobility::Attributes class and the various backend modules (cache,
fallbacks, etc.), whereas these should really be independent.

I'm going to try to restructure things so that modules can be defined in such a
way that Mobility::Attributes does not need to explicitly know anything about
them when including them from options.

@shioyama shioyama force-pushed the module_options branch 9 times, most recently from 25a1215 to 6f5ef60 Compare July 16, 2017 04:55
@shioyama shioyama force-pushed the module_options branch 2 times, most recently from 2e02538 to 85e4bca Compare July 16, 2017 13:13
@shioyama
Copy link
Copy Markdown
Owner Author

shioyama commented Jul 17, 2017

This change adds extracts hard-coded dependencies in the Mobility::Attributes class to the different "option modules" (cache, fallbacks, etc) so that the order and set of modules to include can be entirely customized.

Two new configuration options can be used to do this:

Mobility.option_modules         
#=> {
#   :cache => Mobility::Backend::Cache,          
#   :dirty => Mobility::Backend::Dirty,             
#   :fallbacks => Mobility::Backend::Fallbacks,     
#   :presence => Mobility::Backend::Presence,       
#   :default => Mobility::Backend::Default,         
#   :fallthrough_accessors => Mobility::FallthroughAccessors,                                      
#   :locale_accessors => Mobility::LocaleAccessors
# }

This is a hash of option keys and constants (classes or modules). The only requirement of the classes used as hash values is that they have a singleton method apply which takes an instance if Attributes and an option value (typically a boolean) and then does something with the Attributes instance, typically including a module into attributes.backend_class or into attributes itself.

This hash is iterated over here when initializing an Attributes module, taking the option value for the key and passing it and the attributes module itself to the option module (e.g. Mobility::Backend::Class.apply(attributes, true), if cache: true was passed to translates).

Mobility.default_options
#=> {:cache=>true, :dirty=>false, :fallbacks=>nil, :presence=>true, :default=>nil}

The other configuration option is Mobility.default_options. This gets merged into the options passed into translates, thus allowing you to set a hash of defaults to be used across all your models.

@shioyama shioyama merged commit 0e6d797 into master Jul 17, 2017
@shioyama shioyama deleted the module_options branch July 17, 2017 05:03
@valscion
Copy link
Copy Markdown
Contributor

Thank you for creating these detailed pull requests, and elaborating your thoughts here! Even though it might feel like no-one is reading these, I for one am grateful to be able to follow along how this gem evolves, and how you think through these changes.

🙇

@shioyama
Copy link
Copy Markdown
Owner Author

Thanks! That's great to hear. I was mainly doing these PRs as mental notes to myself, with the idea that anybody who was interested could follow along, but very glad to hear that they are useful to you and others. Really appreciate that you took the time to express that here 😄

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

Successfully merging this pull request may close these issues.

2 participants