-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmembers_entity.rb
47 lines (38 loc) · 1.15 KB
/
members_entity.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require 'dry-types'
require 'dry-struct'
module Entity
# Add dry types to Entity module
module Types
include Dry::Types.module
end
end
module Entity
# Domain entity for team members
class Member < Dry::Struct
include Dry::Types.module
attribute :id, Integer.optional
attribute :origin_id, Strict::Integer
attribute :username, Strict::String
attribute :email, Strict::String.optional
def git_identity
"(#{origin_id}) #{username}"
end
end
end
ray = Entity::Member.new(
id:nil, origin_id: 3248987,
username: 'soumyaray', email: nil
)
# => #<Entity::Member id=nil origin_id=12 username="soumyaray" email=nil>
# Assume request was previously sent to an external service
# Response now comes in from an external service
service_response = {
id:nil, origin_id: 2349493,
username: 'rayray', email: '[email protected]'
}
member = Entity::Member.new(service_response)
# => #<Entity::Member id=nil origin_id=2349493 username="rayray" email="[email protected]">
member.git_identity
# "(2349493) rayray"
ray.email = '[email protected]'
# NoMethodError: undefined method `email=' for #<Entity::Member>