Skip to content

Commit

Permalink
Move accessor method to keyword argument in Attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Aug 12, 2017
1 parent 3ded2fb commit e1f2987
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/mobility/attributes.rb
Expand Up @@ -17,7 +17,7 @@ module Mobility
Since {Attributes} is a subclass of +Module+, including an instance of it is
like including a module. Creating an instance like this:
Attributes.new(:accessor, ["title"], backend: :my_backend, locale_accessors: [:en, :ja], cache: true, fallbacks: true)
Attributes.new("title", backend: :my_backend, locale_accessors: [:en, :ja], cache: true, fallbacks: true)
will generate an anonymous module that behaves like this:
Expand Down Expand Up @@ -128,7 +128,7 @@ class Attributes < Module
# @param [Hash] backend_options Backend options hash
# @option backend_options [Class] model_class Class of model
# @raise [ArgumentError] if method is not reader, writer or accessor
def initialize(method, *attribute_names, backend: Mobility.default_backend, **backend_options)
def initialize(*attribute_names, method: :accessor, backend: Mobility.default_backend, **backend_options)
raise ArgumentError, "method must be one of: reader, writer, accessor" unless %i[reader writer accessor].include?(method)
@method = method
@options = Mobility.default_options.merge(backend_options)
Expand Down
2 changes: 1 addition & 1 deletion lib/mobility/translates.rb
Expand Up @@ -61,7 +61,7 @@ module Translates
%w[accessor reader writer].each do |method|
class_eval <<-EOM, __FILE__, __LINE__ + 1
def mobility_#{method}(*args, **options, &block)
attributes = Attributes.new(:#{method}, *args, options)
attributes = Attributes.new(*args, method: :#{method}, **options)
attributes.backend.instance_eval(&block) if block_given?
include attributes
end
Expand Down
28 changes: 14 additions & 14 deletions spec/mobility/attributes_spec.rb
Expand Up @@ -30,17 +30,17 @@

describe "initializing" do
it "raises ArgumentError if method is not reader, writer or accessor" do
expect { described_class.new(:foo) }.to raise_error(ArgumentError)
expect { described_class.new(method: :foo) }.to raise_error(ArgumentError)
end

it "raises BackendRequired error if backend is nil and no default is set" do
expect { described_class.new(:accessor, "title") }.to raise_error(Mobility::BackendRequired)
expect { described_class.new("title") }.to raise_error(Mobility::BackendRequired)
end

it "does not raise error if backend is nil but default_backend is set" do
original_default_backend = Mobility.config.default_backend
Mobility.config.default_backend = :null
expect { described_class.new(:accessor, "title") }.not_to raise_error
expect { described_class.new("title") }.not_to raise_error
Mobility.config.default_backend = original_default_backend
end
end
Expand All @@ -50,39 +50,39 @@

it "calls configure on backend class with options merged with default options" do
expect(backend_class).to receive(:configure).with(expected_options)
attributes = described_class.new(:accessor, "title", backend: backend_class, foo: "bar")
attributes = described_class.new("title", backend: backend_class, foo: "bar")
Article.include attributes
end

it "calls setup_model on backend class with model_class, attributes, and options merged with default options" do
expect(backend_class).to receive(:setup_model).with(Article, ["title"], expected_options)
Article.include described_class.new(:accessor, "title", backend: backend_class, foo: "bar")
Article.include described_class.new("title", backend: backend_class, foo: "bar")
end

it "includes module in model_class.mobility" do
attributes = described_class.new(:accessor, "title", backend: backend_class)
attributes = described_class.new("title", backend: backend_class)
Article.include attributes
expect(Article.mobility.modules).to eq([attributes])
end

describe "cache" do
it "includes Plugins::Cache into backend when options[:cache] is not false" do
clean_options.delete(:cache)
attributes = described_class.new(:accessor, "title", backend: backend_class, **clean_options)
attributes = described_class.new("title", backend: backend_class, **clean_options)
Article.include attributes
expect(attributes.backend_class.ancestors).to include(Mobility::Plugins::Cache)
end

it "does not include Plugins::Cache into backend when options[:cache] is false" do
attributes = described_class.new(:accessor, "title", backend: backend_class, **clean_options)
attributes = described_class.new("title", backend: backend_class, **clean_options)
Article.include attributes
expect(attributes.backend_class.ancestors).not_to include(Mobility::Plugins::Cache)
end
end

describe "defining attribute backend on model" do
before do
Article.include described_class.new(:accessor, "title", backend: backend_class, foo: "bar")
Article.include described_class.new("title", backend: backend_class, foo: "bar")
end
let(:article) { Article.new }
let(:expected_options) { { foo: "bar", **Mobility.default_options, model_class: Article } }
Expand Down Expand Up @@ -178,14 +178,14 @@
end

describe "method = :accessor" do
before { Article.include described_class.new(:accessor, "title", backend: backend_class) }
before { Article.include described_class.new("title", backend: backend_class) }

it_behaves_like "reader"
it_behaves_like "writer"
end

describe "method = :reader" do
before { Article.include described_class.new(:reader, "title", backend: backend_class) }
before { Article.include described_class.new("title", backend: backend_class, method: :reader) }

it_behaves_like "reader"

Expand All @@ -196,7 +196,7 @@
end

describe "method = :writer" do
before { Article.include described_class.new(:writer, "title", backend: backend_class) }
before { Article.include described_class.new("title", backend: backend_class, method: :writer) }

it_behaves_like "writer"

Expand Down Expand Up @@ -240,14 +240,14 @@

describe "#each" do
it "delegates to attributes" do
attributes = described_class.new(:accessor, :title, :content, backend: :null)
attributes = described_class.new("title", "content", backend: :null)
expect { |b| attributes.each(&b) }.to yield_successive_args("title", "content")
end
end

describe "#backend_name" do
it "returns backend name" do
attributes = described_class.new(:accessor, :title, :content, backend: :null)
attributes = described_class.new("title", "content", backend: :null)
expect(attributes.backend_name).to eq(:null)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/mobility/translates_spec.rb
Expand Up @@ -11,7 +11,7 @@
attributes = Module.new do
def self.each &block; end
end
expect(Mobility::Attributes).to receive(:new).with(:accessor, *attribute_names, {}).and_return(attributes)
expect(Mobility::Attributes).to receive(:new).with(*attribute_names, { method: :accessor }).and_return(attributes)
MyClass.mobility_accessor *attribute_names
end

Expand Down
4 changes: 2 additions & 2 deletions spec/performance/attributes_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe Mobility::Attributes do
describe "initializing" do
specify {
expect { described_class.new(:accessor, backend: :null) }.to allocate_under(9).objects
expect { described_class.new(backend: :null) }.to allocate_under(9).objects
}
end

Expand All @@ -12,7 +12,7 @@
klass = Class.new do
include Mobility
end
attributes = described_class.new(:accessor, backend: :null)
attributes = described_class.new(backend: :null)
expect { klass.include attributes }.to allocate_under(125).objects
}
end
Expand Down

0 comments on commit e1f2987

Please sign in to comment.