Skip to content

Commit

Permalink
feat(): adds service pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
iobaixas committed Jul 14, 2016
1 parent 6a3ade9 commit eccdc22
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 34 deletions.
1 change: 1 addition & 0 deletions lib/power_types.rb
@@ -1,4 +1,5 @@
require "power_types/version"
require "power_types/patterns/service"
require "power_types/patterns/command"

module PowerTypes
Expand Down
40 changes: 6 additions & 34 deletions lib/power_types/patterns/command.rb
@@ -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
37 changes: 37 additions & 0 deletions lib/power_types/patterns/service.rb
@@ -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
23 changes: 23 additions & 0 deletions spec/lib/patterns/service_spec.rb
@@ -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

0 comments on commit eccdc22

Please sign in to comment.