Class: ImproveHunter

Inherits:
Object
  • Object
show all
Defined in:
app/services/improve_hunter.rb

Overview

This is the base class for improving a Hunter. All services that utilize this class belong in /services/improves

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hunters_improvement) ⇒ ImproveHunter

This will initialize @hunters_improvement, @hunter, @improvement, and @improvable. Any setup that all improve services need will be handled here.

Parameters:

  • hunters_improvement

16
17
18
19
20
21
# File 'app/services/improve_hunter.rb', line 16

def initialize(hunters_improvement)
  @hunters_improvement = hunters_improvement
  @improvement = hunters_improvement.improvement
  @hunter = hunters_improvement.hunter
  @improvable = hunters_improvement.improvable
end

Class Method Details

.for(improvement) ⇒ Object


6
7
8
9
# File 'app/services/improve_hunter.rb', line 6

def self.for(improvement)
  return ImproveHunter unless improvement.type
  improvement.type.gsub('Improvements', 'Improves').constantize
end

Instance Method Details

#check(check, attribute, message) ⇒ Object


55
56
57
# File 'app/services/improve_hunter.rb', line 55

def check(check, attribute, message)
  @hunters_improvement.errors.add(attribute, message) if send check
end

#hunter_not_advanced?Boolean

Returns:

  • (Boolean)

47
48
49
# File 'app/services/improve_hunter.rb', line 47

def hunter_not_advanced?
  @improvement.advanced && !Improvement.advanced_eligible?(@hunter)
end

#hunter_playbook_mismatch?Boolean

Returns:

  • (Boolean)

51
52
53
# File 'app/services/improve_hunter.rb', line 51

def hunter_playbook_mismatch?
  @hunter.playbook != @improvement.playbook
end

#improveObject

This method is overridden and used to update and improve the hunter in subclasses returns false if the hunter/improvement/improvable combination is invalid returns a truthy value otherwise


28
29
30
31
# File 'app/services/improve_hunter.rb', line 28

def improve
  return false unless valid?
  true
end

#valid?Boolean

All improve services should extend this method to apply their own constraints, and if they extend the last check should be `@hunters_improvement.errors.none?` This validations checks the hunter is authorized to use the improvement, and that the hunter is of sufficient level for advanced improvements.

Returns:

  • (Boolean)

39
40
41
42
43
44
45
# File 'app/services/improve_hunter.rb', line 39

def valid?
  check(:hunter_playbook_mismatch?, :hunter,
        "does not match improvement playbook: #{@improvement.playbook.name}")
  check(:hunter_not_advanced?, :hunter,
        'is not qualified for advanced improvements')
  @hunters_improvement.errors.none?
end