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

Rebrand option modules as Plugins #64

Merged
merged 5 commits into from Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/mobility.rb
Expand Up @@ -32,14 +32,14 @@ class MyClass

=end
module Mobility
autoload :Attributes, "mobility/attributes"
autoload :Backend, "mobility/backend"
autoload :BackendResetter, "mobility/backend_resetter"
autoload :Configuration, "mobility/configuration"
autoload :FallthroughAccessors, "mobility/fallthrough_accessors"
autoload :LocaleAccessors, "mobility/locale_accessors"
autoload :Translates, "mobility/translates"
autoload :Wrapper, "mobility/wrapper"
autoload :Attributes, "mobility/attributes"
autoload :Backend, "mobility/backend"
autoload :Backends, "mobility/backends"
autoload :BackendResetter, "mobility/backend_resetter"
autoload :Configuration, "mobility/configuration"
autoload :Plugins, "mobility/plugins"
autoload :Translates, "mobility/translates"
autoload :Wrapper, "mobility/wrapper"

require "mobility/orm"

Expand Down Expand Up @@ -159,12 +159,12 @@ def config
# (see Mobility::Configuration#default_options)
# @!method default_options
#
# (see Mobility::Configuration#option_modules)
# @!method option_modules
# (see Mobility::Configuration#plugins)
# @!method plugins
#
# (see Mobility::Configuration#default_accessor_locales)
# @!method default_accessor_locales
%w[accessor_method query_method default_backend default_options option_modules default_accessor_locales].each do |method_name|
%w[accessor_method query_method default_backend default_options plugins default_accessor_locales].each do |method_name|
define_method method_name do
config.public_send(method_name)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mobility/active_record/model_translation.rb
Expand Up @@ -3,7 +3,7 @@ module ActiveRecord
=begin

Subclassed dynamically to generate translation class in
{Backend::ActiveRecord::Table} backend.
{Backends::ActiveRecord::Table} backend.

=end
class ModelTranslation < ::ActiveRecord::Base
Expand Down
33 changes: 21 additions & 12 deletions lib/mobility/attributes.rb
Expand Up @@ -21,9 +21,10 @@ module Mobility

Module.new do
def title_backend
# Create a subclass of Mobility::Backend::MyBackend and include in it:
# - Mobility::Cache (from the cache: true option)
# - Mobility::Fallbacks (from the fallbacks: true option)
# Create a subclass of Mobility::Backends::MyBackend and include in it:
# - Mobility::Plugins::Cache (from the +cache: true+ option)
# - Mobility::Plugins::Fallbacks (from the +fallbacks: true+ option)
# - Mobility::Plugins::Presence (by default, disabled by +presence: false+)
# Then instantiate the backend, memoize it, and return it.
end

Expand Down Expand Up @@ -81,8 +82,8 @@ def title_ja=(value)
will be called when {Attributes} is {#included} in the model class, passed
attributes and options defined when the backend was defined on the model class.
This allows a backend to do things like (for example) define associations on a
model class required by the backend, as happens in the {Backend::KeyValue} and
{Backend::Table} backends.
model class required by the backend, as happens in the {Backends::KeyValue} and
{Backends::Table} backends.

The +setup+ block is also used to extend the query scope/dataset (+i18n+ by
default) with backend-specific query method support.
Expand Down Expand Up @@ -139,13 +140,13 @@ def initialize(method, *attribute_names, backend: Mobility.default_backend, **ba
# @param klass [Class] Class of model
def included(klass)
@model_class = @options[:model_class] = klass
@backend_class = Class.new(get_backend_class(backend: backend_name,
model_class: model_class))
@backend_class = Class.new(get_backend_class(backend_name).for(model_class))

@backend_class.configure(options) if @backend_class.respond_to?(:configure)

Mobility.option_modules.each do |key, option_module|
option_module.apply(self, options[key])
Mobility.plugins.each do |name|
plugin = get_plugin_class(name)
plugin.apply(self, options[name])
end

names.each do |name|
Expand Down Expand Up @@ -196,9 +197,17 @@ def define_writer(attribute)
end
end

def get_backend_class(backend: nil, model_class: nil)
klass = Module === backend ? backend : Mobility::Backend.const_get(backend.to_s.camelize.gsub(/\s+/, ''.freeze).freeze)
klass.for(model_class)
def get_backend_class(backend)
Module === backend ? backend : get_class_from_key(Mobility::Backends, backend)
end

def get_plugin_class(name)
get_class_from_key(Mobility::Plugins, name)
end

def get_class_from_key(parent_class, key)
klass_name = key.to_s.gsub(/(^|_)(.)/){|x| x[-1..-1].upcase}
parent_class.const_get(klass_name)
end
end
end
29 changes: 6 additions & 23 deletions lib/mobility/backend.rb
Expand Up @@ -49,25 +49,8 @@ def self.configure(options)
=end

module Backend
autoload :ActiveModel, 'mobility/backend/active_model'
autoload :ActiveRecord, 'mobility/backend/active_record'
autoload :Cache, 'mobility/backend/cache'
autoload :Column, 'mobility/backend/column'
autoload :Default, 'mobility/backend/default'
autoload :Dirty, 'mobility/backend/dirty'
autoload :Fallbacks, 'mobility/backend/fallbacks'
autoload :HashValued, 'mobility/backend/hash_valued'
autoload :Hstore, 'mobility/backend/hstore'
autoload :Jsonb, 'mobility/backend/jsonb'
autoload :KeyValue, 'mobility/backend/key_value'
autoload :Null, 'mobility/backend/null'
autoload :OrmDelegator, 'mobility/backend/orm_delegator'
autoload :Presence, 'mobility/backend/presence'
autoload :Sequel, 'mobility/backend/sequel'
autoload :Serialized, 'mobility/backend/serialized'
autoload :StringifyLocale, 'mobility/backend/stringify_locale'
autoload :Table, 'mobility/backend/table'
autoload :TranslationCacher, 'mobility/backend/translation_cacher'

# @return [String] Backend attribute
attr_reader :attribute
Expand Down Expand Up @@ -147,12 +130,12 @@ def for(_)
self
end

# Called from option modules to apply custom processing for this backend.
# Name is the name of the option module.
# @param [Symbol] name Name of option module
# @return [Boolean] Whether the module was applied
# @note This is currently only called by Backend::Cache.
def apply_module(_)
# Called from plugins to apply custom processing for this backend.
# Name is the name of the plugin.
# @param [Symbol] name Name of plugin
# @return [Boolean] Whether the plugin was applied
# @note This is currently only called by Plugins::Cache.
def apply_plugin(_)
false
end
end
Expand Down
7 changes: 0 additions & 7 deletions lib/mobility/backend/active_model.rb

This file was deleted.

29 changes: 0 additions & 29 deletions lib/mobility/backend/active_record.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/mobility/backend/orm_delegator.rb
Expand Up @@ -8,8 +8,8 @@ module Backend
class Post < ActiveRecord::Base
# ...
end
Mobility::Backend::KeyValue.for(Post)
#=> Mobility::Backend::ActiveRecord::KeyValue
Mobility::Backends::KeyValue.for(Post)
#=> Mobility::Backends::ActiveRecord::KeyValue

=end
module OrmDelegator
Expand Down
29 changes: 0 additions & 29 deletions lib/mobility/backend/sequel.rb

This file was deleted.

38 changes: 0 additions & 38 deletions lib/mobility/backend/translation_cacher.rb

This file was deleted.

14 changes: 14 additions & 0 deletions lib/mobility/backends.rb
@@ -0,0 +1,14 @@
module Mobility
module Backends
autoload :ActiveRecord, 'mobility/backends/active_record'
autoload :Column, 'mobility/backends/column'
autoload :HashValued, 'mobility/backends/hash_valued'
autoload :Hstore, 'mobility/backends/hstore'
autoload :Jsonb, 'mobility/backends/jsonb'
autoload :KeyValue, 'mobility/backends/key_value'
autoload :Null, 'mobility/backends/null'
autoload :Sequel, 'mobility/backends/sequel'
autoload :Serialized, 'mobility/backends/serialized'
autoload :Table, 'mobility/backends/table'
end
end
28 changes: 28 additions & 0 deletions lib/mobility/backends/active_record.rb
@@ -0,0 +1,28 @@
module Mobility
module Backends
module ActiveRecord
autoload :Column, 'mobility/backends/active_record/column'
autoload :Hstore, 'mobility/backends/active_record/hstore'
autoload :Jsonb, 'mobility/backends/active_record/jsonb'
autoload :KeyValue, 'mobility/backends/active_record/key_value'
autoload :Serialized, 'mobility/backends/active_record/serialized'
autoload :QueryMethods, 'mobility/backends/active_record/query_methods'
autoload :Table, 'mobility/backends/active_record/table'

def setup_query_methods(query_methods)
setup do |attributes, options|
extend(Module.new do
define_method ::Mobility.query_method do
super().extending(query_methods.new(attributes, options))
end
end)
end
end

def self.included(backend_class)
backend_class.include(Backend)
backend_class.extend(self)
end
end
end
end
@@ -1,8 +1,8 @@
module Mobility
module Backend
module Backends
=begin

Implements the {Mobility::Backend::Column} backend for ActiveRecord models.
Implements the {Mobility::Backends::Column} backend for ActiveRecord models.

You can use the +mobility:translations+ generator to create a migration adding
translatable columns to the model table with:
Expand Down Expand Up @@ -32,7 +32,7 @@ class ActiveRecord::Column
include ActiveRecord
include Column

require 'mobility/backend/active_record/column/query_methods'
require 'mobility/backends/active_record/column/query_methods'

# @!group Backend Accessors
# @!macro backend_reader
Expand Down
@@ -1,6 +1,6 @@
module Mobility
module Backend
class ActiveRecord::Column::QueryMethods < Backend::ActiveRecord::QueryMethods
module Backends
class ActiveRecord::Column::QueryMethods < ActiveRecord::QueryMethods
def initialize(attributes, _)
super
attributes_extractor = @attributes_extractor
Expand Down
@@ -1,16 +1,16 @@
require 'mobility/backend/active_record/pg_hash'
require 'mobility/backends/active_record/pg_hash'

module Mobility
module Backend
module Backends
=begin

Implements the {Mobility::Backend::Hstore} backend for ActiveRecord models.
Implements the {Mobility::Backends::Hstore} backend for ActiveRecord models.

@see Mobility::Backend::ActiveRecord::HashValued
@see Mobility::Backends::ActiveRecord::HashValued

=end
class ActiveRecord::Hstore < ActiveRecord::PgHash
require 'mobility/backend/active_record/hstore/query_methods'
require 'mobility/backends/active_record/hstore/query_methods'

# @!group Backend Accessors
# @!macro backend_reader
Expand Down
@@ -1,5 +1,5 @@
module Mobility
module Backend
module Backends
class ActiveRecord::Hstore::QueryMethods < ActiveRecord::QueryMethods
def initialize(attributes, _)
super
Expand Down
@@ -1,16 +1,16 @@
require 'mobility/backend/active_record/pg_hash'
require 'mobility/backends/active_record/pg_hash'

module Mobility
module Backend
module Backends
=begin

Implements the {Mobility::Backend::Jsonb} backend for ActiveRecord models.
Implements the {Mobility::Backends::Jsonb} backend for ActiveRecord models.

@see Mobility::Backend::ActiveRecord::HashValued
@see Mobility::Backends::ActiveRecord::HashValued

=end
class ActiveRecord::Jsonb < ActiveRecord::PgHash
require 'mobility/backend/active_record/jsonb/query_methods'
require 'mobility/backends/active_record/jsonb/query_methods'

# @!group Backend Accessors
#
Expand Down