Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,17 @@ | ||
module PowerTypes | ||
module Command | ||
# rubocop:disable Metrics/MethodLength | ||
def self.new(*_attributes) | ||
attr_names = [] | ||
attr_defaults = {} | ||
|
||
_attributes.each do |att| | ||
if att.is_a? Hash | ||
attr_defaults.merge! att | ||
attr_names += att.keys | ||
else | ||
attr_names << att | ||
end | ||
end | ||
|
||
Class.new do | ||
def self.for(kwargs = {}) | ||
new(kwargs).perform | ||
end | ||
|
||
def logger | ||
Rails.logger | ||
end | ||
|
||
def perform | ||
raise NotImplementedError, "Command must implement `perform`" | ||
end | ||
|
||
define_method(:initialize) do |kwargs = {}| | ||
unless (kwargs.keys - attr_names).empty? | ||
raise ArgumentError, "Unexpected arguments: #{(kwargs.keys - attr_names).join(', ')}" | ||
Service.new(*_attributes).tap do |klass| | ||
klass.class_eval do | ||
def self.for(kwargs = {}) | ||
new(kwargs).perform | ||
end | ||
|
||
kwargs = attr_defaults.merge kwargs | ||
attr_names.map do |a| | ||
raise ArgumentError, "Missing parameter: #{a}" unless kwargs.key? a | ||
instance_variable_set "@#{a}", kwargs[a] | ||
def perform | ||
raise NotImplementedError, "Command must implement `perform`" | ||
end | ||
end | ||
end | ||
end | ||
# rubocop:enable Metrics/MethodLength | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module PowerTypes | ||
module Service | ||
# rubocop:disable Metrics/MethodLength | ||
def self.new(*_attributes) | ||
attr_names = [] | ||
attr_defaults = {} | ||
|
||
_attributes.each do |att| | ||
if att.is_a? Hash | ||
attr_defaults.merge! att | ||
attr_names += att.keys | ||
else | ||
attr_names << att | ||
end | ||
end | ||
|
||
Class.new do | ||
def logger | ||
Rails.logger | ||
end | ||
|
||
define_method(:initialize) do |kwargs = {}| | ||
unless (kwargs.keys - attr_names).empty? | ||
raise ArgumentError, "Unexpected arguments: #{(kwargs.keys - attr_names).join(', ')}" | ||
end | ||
|
||
kwargs = attr_defaults.merge kwargs | ||
attr_names.map do |a| | ||
raise ArgumentError, "Missing parameter: #{a}" unless kwargs.key? a | ||
instance_variable_set "@#{a}", kwargs[a] | ||
end | ||
end | ||
end | ||
end | ||
# rubocop:enable Metrics/MethodLength | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'spec_helper' | ||
|
||
describe PowerTypes::Service do | ||
let(:service) do | ||
Class.new(described_class.new(:foo, bar: nil)) do | ||
end | ||
end | ||
|
||
describe "new" do | ||
it "fails if argument it not declared in argument list" do | ||
expect { service.new(teapot: 'bar') }.to raise_error(ArgumentError) | ||
end | ||
|
||
it "fails if required argument is not provided" do | ||
expect { service.new(bar: 'bar') }.to raise_error(ArgumentError) | ||
end | ||
|
||
it { expect(service.new(foo: 'bar').instance_variable_defined?(:@foo)).to be true } | ||
it { expect(service.new(foo: 'bar').instance_variable_defined?(:@bar)).to be true } | ||
it { expect(service.new(foo: 'bar').instance_variable_defined?(:@fur)).to be false } | ||
it { expect(service.new(foo: 'bar').instance_variable_get(:@foo)).to eq 'bar' } | ||
end | ||
end |