Skip to content

Commit

Permalink
Remove Configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Oct 18, 2020
1 parent 1cc2bb0 commit fc9acd1
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 120 deletions.
42 changes: 24 additions & 18 deletions lib/mobility.rb
Expand Up @@ -25,7 +25,6 @@ class Error < StandardError

require "mobility/backend"
require "mobility/backends"
require "mobility/configuration"
require "mobility/plugin"
require "mobility/plugins"
require "mobility/translations"
Expand Down Expand Up @@ -83,29 +82,36 @@ def storage
RequestStore.store
end

# @!group Configuration Methods
# @return [Mobility::Configuration] Mobility configuration
def config
@configuration ||= Configuration.new
# @return [Symbol,Class]
def default_backend
translations_class.defaults[:backend]
end

# (see Mobility::Configuration#default_backend)
# @!method default_backend
# Configure Mobility
# @yield [Mobility::Translations]
def configure
translates_with(Class.new(Translations)) unless translations_class
yield translations_class
end
# @!endgroup

def translates_with(pluggable)
raise ArgumentError, "translations class must be a subclass of Module." unless Module === pluggable
@translations_class = pluggable
end

# (see Mobility::Configuration#plugins)
# @!method plugins
%w[default_backend plugins].each do |method_name|
define_method method_name do
config.public_send(method_name)
def translations_class
if @translations_class
@translations_class
else
raise Error, "Mobility has not been configured. "\
"Configure with Mobility.configure, or assign a translations class with Mobility.translates_with(<class>)"
end
end

# Configure Mobility
# @yield [Mobility::Configuration] Mobility configuration
def configure
yield config
def reset_translations_class
@translations_class = nil
end
# @!endgroup

# Return normalized locale
# @param [String,Symbol] locale
Expand Down Expand Up @@ -181,7 +187,7 @@ module Translates
# @param [Hash] options
# @yield Yields to block with backend as context
def translates(*args, **options)
include Mobility.config.translations_class.new(*args, **options)
include Mobility.translations_class.new(*args, **options)
end
end

Expand Down
32 changes: 0 additions & 32 deletions lib/mobility/configuration.rb

This file was deleted.

29 changes: 3 additions & 26 deletions lib/mobility/plugins.rb
Expand Up @@ -2,32 +2,9 @@ module Mobility
=begin
Plugins allow modular customization of backends independent of the backend
itself. They are enabled through the {Configuration.plugins} configuration
setting, which takes an array of symbols corresponding to plugin names. The
order of these names is important since it determines the order in which
plugins will be applied.
So if our {Configuration.plugins} is an array +[:foo]+, and we call
`translates` on our model, +Post+, like this:
class Post
translates :title, foo: true
end
Then the +Foo+ plugin will be applied with the option value +true+. Applying a
module calls a class method, +apply+ (in this case +Foo.apply+), which takes
two arguments:
- an instance of the {Translations} class, +attributes+, from which the backend
can configure the backend class (+attributes.backend_class+) and the model
(+attributes.model_class+), and the +attributes+ module itself (which
will be included into the backend).
- the value of the +option+ passed into the model with +translates+ (in this
case, +true+).
Typically, the plugin will include a module into either
+attributes.backend_class+ or +attributes+ itself, configured according to the
option value. For examples, see classes under the {Mobility::Plugins} namespace.
itself. They are enabled through {Mobility::Translations.plugins} (delegated to
from {Mobility.configure}), which takes a block within which plugins can be
declared in any order (dependencies will be resolved).
=end
module Plugins
Expand Down
7 changes: 3 additions & 4 deletions lib/mobility/plugins/fallbacks.rb
Expand Up @@ -8,10 +8,9 @@ module Plugins
Falls back to one or more alternative locales in case no value is defined for a
given locale.
For +fallbacks: true+, Mobility will use the value of
{Mobility::Configuration#new_fallbacks} for the fallbacks instance. This
defaults to an instance of +I18n::Locale::Fallbacks+, but can be
configured (see {Mobility::Configuration}).
For +fallbacks: true+, Mobility will use an instance of
+I18n::Locale::Fallbacks+, but this can be configured by overriding
+generate_fallbacks+ in the translations class.
If a hash is passed to the +fallbacks+ option, a new fallbacks instance will be
created for the model with the hash defining additional fallbacks. To set a
Expand Down
16 changes: 0 additions & 16 deletions spec/mobility/configuration_spec.rb

This file was deleted.

42 changes: 27 additions & 15 deletions spec/mobility_spec.rb
Expand Up @@ -18,7 +18,9 @@ def attributes

context "with translated attributes" do
it "includes backend module into model class" do
expect(described_class::Translations).to receive(:new).
klass = Class.new(described_class::Translations)
Mobility.translates_with(klass)
expect(klass).to receive(:new).
with(:title, backend: :null, foo: :bar).
and_call_original
model.extend described_class
Expand Down Expand Up @@ -166,32 +168,42 @@ def perform_with_locale(locale)
end
end

describe '.config' do
it 'initializes a new configuration' do
expect(described_class.config).to be_a(described_class::Configuration)
end

it 'memoizes configuration' do
expect(described_class.config).to be(described_class.config)
end
end

describe ".configure" do
it "yields configuration" do
it "yields the translation class, or creates one if not defined" do
klass = Class.new
described_class.translates_with(klass)
expect { |block|
described_class.configure &block
}.to yield_with_args(described_class.config)
}.to yield_with_args(klass)
end
end

describe "#translates" do
it "delegates to Configuration translation_class" do
expect(Mobility.config.translations_class).to receive(:new).once.with("title", foo: "bar").and_return(mod = Module.new)
it "delegates to translation_class" do
translations_class = Class.new(Mobility::Translations)
Mobility.translates_with(translations_class)
expect(translations_class).to receive(:new).once.with("title", foo: "bar").and_return(mod = Module.new)

klass = Class.new
klass.extend Mobility
klass.translates "title", foo: "bar"
expect(klass.included_modules).to include(mod)
end
end

describe ".translates_with" do
it "sets translations_class" do
klass = Class.new(Mobility::Translations)
described_class.translates_with(klass)
expect(described_class.translations_class).to eq(klass)
end
end

describe "#default_backend" do
it "defaults to nil" do
translations_class = Class.new(Mobility::Translations)
subject.translates_with translations_class
expect(subject.default_backend).to eq(nil)
end
end
end
9 changes: 0 additions & 9 deletions spec/performance/configuration_spec.rb

This file was deleted.

3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -77,6 +77,9 @@
if defined?(ActiveSupport)
ActiveSupport::Dependencies::Reference.clear!
end

# ensure this is reset in each run
Mobility.reset_translations_class
end

unless orm == 'none'
Expand Down

0 comments on commit fc9acd1

Please sign in to comment.