Skip to content

Commit

Permalink
Refactor Factory Specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Decot committed Jul 4, 2019
1 parent 8d8090e commit ac3fe0c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
5 changes: 4 additions & 1 deletion lib/app_store_connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
require 'app_store_connect/bundle_id_create_request'
require 'app_store_connect/user_invitation_create_request'
require 'app_store_connect/version'
require 'app_store_connect/factory'
require 'app_store_connect/config'

require 'app_store_connect/type'
require 'app_store_connect/type/enum'

require 'app_store_connect/factory'
require 'app_store_connect/factory/builder/enum'

module AppStoreConnect
Expand Down
9 changes: 7 additions & 2 deletions lib/app_store_connect/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

module AppStoreConnect
class Factory
BuilderNotRegistered = Class.new(StandardError)

def self.register(name, builder)
builders[name] = builder
end

def self.builders
@builders ||= {}
end
private_class_method :builders

def self.build(name, options = {})
builders[name].call(options)
builder = builders[name]

raise BuilderNotRegistered, "Builder not registered: #{name}" if builder.nil?

builder.call(options)
end
end
end
16 changes: 2 additions & 14 deletions lib/app_store_connect/factory/builder/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,8 @@ class Factory
module Builder
class Enum
def self.call(values:)
Class.new do
attr_reader :value

const_set('VALUES', values)

def initialize(value:)
@value = value
end

def self.const_missing(const)
return const_set(const, new(value: const.to_s)) if const.to_s.in?(const_get('VALUES'))

super(const)
end
Class.new(Type::Enum) do |base|
base.const_set('VALUES', values)
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions lib/app_store_connect/type/enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module AppStoreConnect
module Type
class Enum
attr_reader :value

def initialize(value:)
@value = value
end

def self.const_missing(const)
return const_set(const, new(value: const.to_s)) if const.to_s.in?(const_get('VALUES'))

super(const)
end
end
end
end
14 changes: 7 additions & 7 deletions spec/app_store_connect/factory_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# frozen_string_literal: true

RSpec.describe AppStoreConnect::Factory do
describe '.type' do
context 'when the type is not supported' do
it 'should raise UnsupportedType' do
describe '.build' do
context 'when the builder is not registered' do
it 'should raise BuilderNotRegistered' do
expect do
described_class.type(type: 'Foobar')
end.to raise_error(described_class::UnsupportedType)
described_class.build('foo')
end.to raise_error(described_class::BuilderNotRegistered)
end
end

context 'when the type is "enum"' do
context 'when "enum"' do
it 'should return a class that inherits from AppStoreConnect::Type::Enum' do
klass = described_class.type(type: 'enum', values: [])
klass = described_class.build('enum', values: [])

expect(klass).to be < AppStoreConnect::Type::Enum
end
Expand Down

0 comments on commit ac3fe0c

Please sign in to comment.