-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from kyledecot/kd-type
Add Enum Type Factory
- Loading branch information
Showing
6 changed files
with
89 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module AppStoreConnect | ||
class Factory | ||
UnsupportedType = Class.new(StandardError) | ||
|
||
TYPES = { | ||
'enum' => 'Enum' | ||
}.freeze | ||
|
||
def self.type(type:, **options) | ||
case type | ||
when *TYPES.keys | ||
klass = "AppStoreConnect::Type::#{TYPES[type]}".constantize | ||
Class.new(klass, &send(type, options)) | ||
else | ||
raise UnsupportedType, "Unsupported Type: #{type}" | ||
end | ||
end | ||
|
||
def self.enum(options) | ||
proc do |base| | ||
base.const_set('VALUES', options.fetch(:values)) | ||
end | ||
end | ||
private_class_method :enum | ||
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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module AppStoreConnect | ||
class Parser | ||
def self.parse! | ||
json = JSON.parse(File.read(File.join(__dir__, '../config/api.json'))) | ||
|
||
parse_types(json['Type']) | ||
end | ||
|
||
def self.parse_types(types) | ||
types.each do |name, type| | ||
klass = Factory.type(type.deep_symbolize_keys) | ||
|
||
AppStoreConnect::Type.const_set(name, klass) | ||
end | ||
end | ||
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,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module AppStoreConnect | ||
module Type | ||
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,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 |
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,11 @@ | ||
{ | ||
"Type": { | ||
"BundleIdPlatform": { | ||
"type": "enum", | ||
"values": [ | ||
"IOS", | ||
"MAC_OS" | ||
] | ||
} | ||
} | ||
} |