|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | # A class to hold specifications and results from validations.
|
2 | 4 | class DeckValidation
|
3 |
| - attr_reader :basic_deckbuilding_rules |
4 |
| - attr_reader :label |
5 |
| - attr_reader :errors |
6 |
| - attr_reader :format_id |
7 |
| - attr_reader :restriction_id |
8 |
| - attr_reader :card_pool_id |
9 |
| - attr_reader :snapshot_id |
| 5 | + attr_reader :basic_deckbuilding_rules, :label, :errors, :format_id, :restriction_id, :card_pool_id, :snapshot_id |
10 | 6 |
|
11 | 7 | def initialize(validation_hash)
|
12 | 8 | @label = nil
|
13 |
| - if validation_hash.has_key?('label') |
14 |
| - @label = validation_hash['label'] |
15 |
| - end |
| 9 | + @label = validation_hash['label'] if validation_hash.key?('label') |
16 | 10 | @basic_deckbuilding_rules = false
|
17 |
| - if validation_hash.has_key?('basic_deckbuilding_rules') |
| 11 | + if validation_hash.key?('basic_deckbuilding_rules') |
18 | 12 | @basic_deckbuilding_rules = validation_hash['basic_deckbuilding_rules']
|
19 | 13 | end
|
20 | 14 | @format_id = nil
|
21 |
| - if validation_hash.has_key?('format_id') |
22 |
| - @format_id = validation_hash['format_id'] |
23 |
| - end |
| 15 | + @format_id = validation_hash['format_id'] if validation_hash.key?('format_id') |
24 | 16 | @restriction_id = nil
|
25 |
| - if validation_hash.has_key?('restriction_id') |
26 |
| - @restriction_id = validation_hash['restriction_id'] |
27 |
| - end |
| 17 | + @restriction_id = validation_hash['restriction_id'] if validation_hash.key?('restriction_id') |
28 | 18 | @card_pool_id = nil
|
29 |
| - if validation_hash.has_key?('card_pool_id') |
30 |
| - @card_pool_id = validation_hash['card_pool_id'] |
31 |
| - end |
| 19 | + @card_pool_id = validation_hash['card_pool_id'] if validation_hash.key?('card_pool_id') |
32 | 20 | @snapshot_id = nil
|
33 |
| - if validation_hash.has_key?('snapshot_id') |
34 |
| - @snapshot_id = validation_hash['snapshot_id'] |
35 |
| - end |
| 21 | + @snapshot_id = validation_hash['snapshot_id'] if validation_hash.key?('snapshot_id') |
36 | 22 |
|
37 | 23 | expand_implied_ids
|
38 | 24 |
|
39 | 25 | @errors = []
|
40 | 26 | end
|
41 | 27 |
|
42 | 28 | def expand_implied_ids
|
43 |
| - if !@snapshot_id.nil? and (@format_id.nil? or @card_pool_id.nil? or @restriction_id.nil?) |
| 29 | + if !@snapshot_id.nil? && (@format_id.nil? || @card_pool_id.nil? || @restriction_id.nil?) |
44 | 30 | if Snapshot.exists?(@snapshot_id)
|
45 | 31 | snapshot = Snapshot.find(@snapshot_id)
|
46 |
| - if @format_id.nil? |
47 |
| - @format_id = snapshot.format_id |
48 |
| - end |
49 |
| - if @card_pool_id.nil? |
50 |
| - @card_pool_id = snapshot.card_pool_id |
51 |
| - end |
52 |
| - if @restriction_id.nil? |
53 |
| - @restriction_id = snapshot.restriction_id |
54 |
| - end |
| 32 | + @format_id = snapshot.format_id if @format_id.nil? |
| 33 | + @card_pool_id = snapshot.card_pool_id if @card_pool_id.nil? |
| 34 | + @restriction_id = snapshot.restriction_id if @restriction_id.nil? |
55 | 35 | end
|
56 |
| - elsif !@format_id.nil? and (@snapshot_id.nil? or @card_pool_id.nil? or @restriction_id.nil?) |
| 36 | + elsif !@format_id.nil? && (@snapshot_id.nil? || @card_pool_id.nil? || @restriction_id.nil?) |
57 | 37 | if Format.exists?(@format_id)
|
58 | 38 | format = Format.find(@format_id)
|
59 |
| - if @snapshot_id.nil? |
60 |
| - @snapshot_id = format.active_snapshot_id |
61 |
| - end |
| 39 | + @snapshot_id = format.active_snapshot_id if @snapshot_id.nil? |
62 | 40 | active_snapshot = format.snapshot
|
63 |
| - if !active_snapshot.nil? |
64 |
| - if @card_pool_id.nil? |
65 |
| - @card_pool_id = active_snapshot.card_pool_id |
66 |
| - end |
67 |
| - if @restriction_id.nil? |
68 |
| - @restriction_id = active_snapshot.restriction_id |
69 |
| - end |
| 41 | + unless active_snapshot.nil? |
| 42 | + @card_pool_id = active_snapshot.card_pool_id if @card_pool_id.nil? # rubocop:disable Metrics/BlockNesting |
| 43 | + @restriction_id = active_snapshot.restriction_id if @restriction_id.nil? # rubocop:disable Metrics/BlockNesting |
70 | 44 | end
|
71 | 45 | end
|
72 |
| - elsif !@card_pool_id.nil? and @format_id.nil? |
| 46 | + elsif !@card_pool_id.nil? && @format_id.nil? |
73 | 47 | if CardPool.exists?(@card_pool_id)
|
74 | 48 | card_pool = CardPool.find(@card_pool_id)
|
75 | 49 | @format_id = card_pool.format_id
|
76 | 50 | end
|
77 |
| - elsif !@restriction_id.nil? and @format_id.nil? |
| 51 | + elsif !@restriction_id.nil? && @format_id.nil? |
78 | 52 | if Restriction.exists?(@restriction_id)
|
79 | 53 | restriction = Restriction.find(@restriction_id)
|
80 | 54 | @format_id = restriction.format_id
|
81 | 55 | end
|
82 | 56 | end
|
83 | 57 | end
|
84 | 58 |
|
85 |
| - def add_error(e) |
86 |
| - @errors << e |
| 59 | + def add_error(error) |
| 60 | + @errors << error |
87 | 61 | end
|
88 | 62 |
|
89 |
| - def is_valid? |
90 |
| - return @errors.size == 0 |
| 63 | + def valid? |
| 64 | + @errors.empty? |
91 | 65 | end
|
92 | 66 | end
|
0 commit comments