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

Make it easier to include modules into backend class #538

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
([#536](https://github.com/shioyama/mobility/pull/536))
- Support primary keys other then :id on model classes
([#542](https://github.com/shioyama/mobility/pull/542))
- Make it easier to include modules into backend class
([#538](https://github.com/shioyama/mobility/pull/538))

## 1.2

Expand Down
33 changes: 31 additions & 2 deletions lib/mobility/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ module Mobility
used to include any module(s) into the backend class, see
{Mobility::Plugins::Backend}.)

Also includes a +configure+ class method to apply plugins to a pluggable
({Mobility::Pluggable} instance), with a block.
To define methods on the backend, the plugin +included+ hook also looks for
constants named +BackendMethods+ and/or +BackendClassMethods+ in the plugin
namespace. If it finds these, *and* no included hook has been defined on the
plugin, then it auto-generates an included hook in which the backend class
includes/extends these modules.

The module also includes a +configure+ class method to apply plugins to a
pluggable ({Mobility::Pluggable} instance), with a block.

@example Defining a plugin
module MyPlugin
Expand All @@ -36,6 +42,21 @@ module MyPlugin
end
end

@example With +BackendMethods+ module
module MyPlugin
extend Mobility::Plugin

module BackendMethods
def read(locale, **)
# ...
end

def write(locale, value, **)
# ...
end
end
end

@example Configure an attributes class with plugins
class Translations < Mobility::Translations
end
Expand Down Expand Up @@ -95,6 +116,14 @@ def included(pluggable)
if defined?(@default) && !pluggable.defaults.has_key?(name = Plugins.lookup_name(self))
pluggable.defaults[name] = @default
end
if !method_defined?(:included) &&
(defined?(self::BackendMethods) || defined?(self::BackendClassMethods))
plugin = self
included_hook do |_, backend_class|
backend_class.include(plugin::BackendMethods) if defined?(plugin::BackendMethods)
backend_class.extend(plugin::BackendClassMethods) if defined?(plugin::BackendClassMethods)
end
end
super
end

Expand Down
7 changes: 1 addition & 6 deletions lib/mobility/plugins/active_record/column_fallback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ module ColumnFallback

requires :column_fallback, include: false

included_hook do |_, backend_class|
backend_class.include BackendInstanceMethods
backend_class.extend BackendClassMethods
end

def self.use_column_fallback?(options, locale)
case column_fallback = options[:column_fallback]
when TrueClass
Expand All @@ -31,7 +26,7 @@ def self.use_column_fallback?(options, locale)
end
end

module BackendInstanceMethods
module BackendMethods
def read(locale, **)
if ColumnFallback.use_column_fallback?(options, locale)
model.read_attribute(attribute)
Expand Down
8 changes: 0 additions & 8 deletions lib/mobility/plugins/active_record/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ module Dirty
end
end

included_hook do |_, backend_class|
if options[:dirty]
backend_class.include BackendMethods
end
end

private

def dirty_handler_methods
Expand Down Expand Up @@ -109,8 +103,6 @@ def reload(*)
end
end
end

BackendMethods = ActiveModel::Dirty::BackendMethods
end
end

Expand Down
5 changes: 0 additions & 5 deletions lib/mobility/plugins/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ module Default

requires :backend, include: :before

# Applies default plugin to attributes.
included_hook do |_klass, backend_class|
backend_class.include(BackendMethods)
end

# Generate a default value for given parameters.
# @param [Object, Proc] default_value A default value or Proc
# @param [Symbol] locale
Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/plugins/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ module Fallbacks
# Applies fallbacks plugin to attributes. Completely disables fallbacks
# on model if option is +false+.
included_hook do |_, backend_class|
backend_class.include(BackendInstanceMethods) unless options[:fallbacks] == false
backend_class.include(BackendMethods) unless options[:fallbacks] == false
# This is weird. We need to find a better way to allow customization of
# rarely-customized code like this.
backend_class.define_method(:generate_fallbacks, &method(:generate_fallbacks))
Expand All @@ -136,7 +136,7 @@ def [](locale)
end
end

module BackendInstanceMethods
module BackendMethods
def read(locale, fallback: true, **kwargs)
return super(locale, **kwargs) if !fallback || kwargs[:locale]

Expand Down
17 changes: 7 additions & 10 deletions lib/mobility/plugins/presence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,25 @@ module Presence
default true
requires :backend, include: :before

# Applies presence plugin to attributes.
included_hook do |_, backend_class|
backend_class.include(BackendMethods) if options[:presence]
end

module BackendMethods
# @!group Backend Accessors
# @!macro backend_reader
# @option options [Boolean] presence
# *false* to disable presence filter.
def read(locale, **options)
options.delete(:presence) == false ? super : Presence[super]
def read(locale, **kwargs)
return super unless options[:presence]
kwargs.delete(:presence) == false ? super : Presence[super]
end

# @!macro backend_writer
# @option options [Boolean] presence
# *false* to disable presence filter.
def write(locale, value, **options)
if options.delete(:presence) == false
def write(locale, value, **kwargs)
return super unless options[:presence]
if kwargs.delete(:presence) == false
super
else
super(locale, Presence[value], **options)
super(locale, Presence[value], **kwargs)
end
end
# @!endgroup
Expand Down
7 changes: 1 addition & 6 deletions lib/mobility/plugins/sequel/column_fallback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ module ColumnFallback

requires :column_fallback, include: false

included_hook do |_, backend_class|
backend_class.include BackendInstanceMethods
backend_class.extend BackendClassMethods
end

def self.use_column_fallback?(options, locale)
case column_fallback = options[:column_fallback]
when TrueClass
Expand All @@ -31,7 +26,7 @@ def self.use_column_fallback?(options, locale)
end
end

module BackendInstanceMethods
module BackendMethods
def read(locale, **)
if ColumnFallback.use_column_fallback?(options, locale)
model[attribute.to_sym]
Expand Down