Total Complexity | 15 |
Total Lines | 55 |
Duplicated Lines | 14.55 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | module GlipSdk::REST::Cache |
||
2 | class Groups |
||
3 | attr_accessor :groups |
||
4 | attr_accessor :groups_name2id |
||
5 | attr_accessor :teams |
||
6 | attr_accessor :teams_name2id |
||
7 | def initialize |
||
8 | @groups = {} |
||
9 | @teams = {} |
||
10 | @teams_name2id = {} |
||
11 | @groups_name2id = {} |
||
12 | end |
||
13 | |||
14 | def load_groups(groups) |
||
15 | if groups.is_a? Array |
||
16 | groups.each { |g| load_group g } |
||
17 | elsif groups.is_a? Hash |
||
18 | groups.each { |_, g| load_group g} |
||
19 | end |
||
20 | end |
||
21 | |||
22 | def load_group(group) |
||
23 | if group.key? 'id' |
||
24 | id = group['id'] |
||
25 | type = group['type'] |
||
26 | if type.to_s.downcase == 'team' |
||
27 | @teams[id.to_s] = group |
||
28 | @teams_name2id[group['name']] = id.to_s |
||
29 | else |
||
30 | @groups[id.to_s] = group |
||
31 | @groups_name2id[group['name']] = id.to_s |
||
32 | end |
||
33 | end |
||
34 | end |
||
35 | |||
36 | def id_by_name(name) |
||
37 | group = by_name name |
||
38 | group.nil? ? nil : group['id'] |
||
39 | end |
||
40 | |||
41 | def by_name(name) |
||
42 | team = team_by_name name |
||
43 | return team unless team.nil? |
||
44 | group_by_name name |
||
45 | end |
||
46 | |||
47 | View Code Duplication | def team_by_name(name) |
|
|
|||
48 | team_id = @teams_name2id[name.to_s] |
||
49 | team_id ? @teams[team_id] : nil |
||
50 | end |
||
51 | |||
52 | View Code Duplication | def group_by_name(name) |
|
53 | group_id = @groups_name2id[name.to_s] |
||
54 | group_id ? @groups[group_id] : nil |
||
55 | end |
||
56 | end |
||
57 | end |
||
58 |