Created
May 24, 2020 06:30
-
-
Save kinnrot/370b4a8d95a7e5076a1416c58ecd7b66 to your computer and use it in GitHub Desktop.
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
require 'active_support/all' | |
module UnderwritingService | |
extend self | |
PolicyMeta = Struct.new(:product_type, :public_id, :status) | |
def policies_metas(user_id:) | |
UserPoliciesRegistry.all.flat_map do |provider| | |
provider.of_user(user_id:user_id).map { |policy| | |
PolicyMeta.new(provider.product_type, provider.public_id(policy), provider.status(policy)) | |
} | |
end | |
end | |
def fetch_policies(product:, public_ids:) | |
UserPoliciesRegistry.for_product(product).fetch(public_ids) | |
end | |
end | |
module UserPoliciesRegistry | |
extend self | |
def register(provider) | |
@providers ||= {} | |
@providers[provider.product_type] = provider | |
end | |
def all | |
@providers.values | |
end | |
def for_product(product_type) | |
@providers[product_type] | |
end | |
end | |
module UserPoliciesProvider | |
extend ::ActiveSupport::Concern | |
included do | |
UserPoliciesRegistry.register(self) | |
end | |
end | |
module PetUserPolicies | |
extend self | |
def product_type | |
:pet | |
end | |
include UserPoliciesProvider | |
def of_user(user_id:) | |
[{id:2}] | |
end | |
def fetch(ids:) | |
[] | |
end | |
def public_id(policy) | |
'1' | |
end | |
def status(policy) | |
true | |
end | |
end | |
metas = UnderwritingService.policies_metas(user_id: 2) | |
p metas |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment