Commit 9a9aee470c525c703748ccbe72d6cd076d284f67
1 parent
eb9790f6
Exists in
master
Refactoring Person class
Signed-off-by: David Carlos <ddavidcarlos1392@gmail.com>
Showing
1 changed file
with
33 additions
and
28 deletions
Show diff stats
lib/ext/person.rb
@@ -5,51 +5,56 @@ class Person | @@ -5,51 +5,56 @@ class Person | ||
5 | def attr_to_hash | 5 | def attr_to_hash |
6 | attrs = super | 6 | attrs = super |
7 | 7 | ||
8 | + attrs = friends_attrs(attrs) | ||
9 | + | ||
10 | + attrs = commuinities_attrs(attrs) | ||
11 | + | ||
12 | + attrs = softwares_attrs(attrs) | ||
13 | + | ||
14 | + attrs | ||
15 | + end | ||
16 | + | ||
17 | + def friends_attrs(attrs) | ||
8 | attrs['friends-count'] = self.friends.count | 18 | attrs['friends-count'] = self.friends.count |
9 | attrs['friends'] = [] | 19 | attrs['friends'] = [] |
10 | self.friends.each do |friend| | 20 | self.friends.each do |friend| |
11 | - attrs_friends = { | ||
12 | - "id" => friend.id, | ||
13 | - "identifier" => friend.identifier, | ||
14 | - "name" => friend.name | ||
15 | - } | ||
16 | - attrs['friends'] << attrs_friends | 21 | + attrs['friends'] << profile_attrs(friend) |
17 | end | 22 | end |
23 | + attrs | ||
24 | + end | ||
18 | 25 | ||
19 | - attrs['communities-count'] = self.respond_to?("softwares") ? self.communities.count - self.softwares.count : self.communities.count | 26 | + def commuinities_attrs(attrs) |
27 | + attrs['communities-count'] = self.respond_to?("softwares") ? | ||
28 | + self.communities.count - self.softwares.count : | ||
29 | + self.communities.count | ||
20 | attrs['communities'] = [] | 30 | attrs['communities'] = [] |
21 | self.communities.each do |community| | 31 | self.communities.each do |community| |
22 | if community.respond_to?("software?") | 32 | if community.respond_to?("software?") |
23 | - if !community.software? | ||
24 | - attrs_community = { | ||
25 | - "id" => community.id, | ||
26 | - "identifier" => community.identifier, | ||
27 | - "name" => community.name | ||
28 | - } | ||
29 | - attrs['communities'] << attrs_community | ||
30 | - end | ||
31 | - else | ||
32 | - attrs_community = { | ||
33 | - "id" => community.id, | ||
34 | - "identifier" => community.identifier, | ||
35 | - "name" => community.name | ||
36 | - } | ||
37 | - attrs['communities'] << attrs_community | 33 | + attrs['communities'] << profile_attrs(community) |
38 | end | 34 | end |
39 | end | 35 | end |
36 | + attrs | ||
37 | + end | ||
40 | 38 | ||
39 | + def softwares_attrs(attrs) | ||
41 | attrs['softwares-count'] = self.respond_to?("softwares") ? self.softwares.count : 0 | 40 | attrs['softwares-count'] = self.respond_to?("softwares") ? self.softwares.count : 0 |
42 | attrs['softwares'] = [] | 41 | attrs['softwares'] = [] |
43 | if self.respond_to?("softwares") | 42 | if self.respond_to?("softwares") |
44 | self.softwares.each do |software| | 43 | self.softwares.each do |software| |
45 | - attrs_software = { | ||
46 | - "id" => software.id, | ||
47 | - "identifier" => software.identifier, | ||
48 | - "name" => software.name | ||
49 | - } | ||
50 | - attrs['softwares'] << attrs_software | 44 | + attrs['softwares'] << profile_attrs(software) |
51 | end | 45 | end |
52 | end | 46 | end |
53 | attrs | 47 | attrs |
54 | end | 48 | end |
49 | + | ||
50 | + private | ||
51 | + | ||
52 | + def profile_attrs(profile) | ||
53 | + profile = { | ||
54 | + "id" => profile.id, | ||
55 | + "identifier" => profile.identifier, | ||
56 | + "name" => profile.name | ||
57 | + } | ||
58 | + end | ||
59 | + | ||
55 | end | 60 | end |