What does instantiating an ActiveModel::Model class within itself provide from a design perspective?

16 views Asked by At

Assuming a PORO such as:

class Player
  include ActiveModel::Model
  include ActiveModel::Validations

  attr_accessor :name, :position
  validates :name, :position
  

  VALID_POSITIONS = ['goalie', 'defense', 'forward']
  validates :position, :inclusion { in: VALID_POSITIONS}

  BENCH_WARMER = Player.new(name: "Biz", position: "bench_warmer")
end
  1. How is BENCH_WARMER able to supersede the validation explicitly stated for the position attribute?

  2. And what does this instantiation within itself of BENCH_WARMER afford the application/What can I do with BENCH_WARMER now that it exists within this class?

  3. Why validate if it's simply going to be ignored in the next line?

Googling this sort of question points to a bunch of rudimentary resources about using self and how to create instances of an object outside of itself.

0

There are 0 answers