samedi 5 mars 2022

Ruby class accessing protected member of child class?

I have a background in Node Js and I am new to Ruby, Pardon me If this question is a noob. I have 3 classes like this.

class Babylon::Metric::Counter < Babylon::Metric
  protected

  def record
    # some code here
  end
end
class Babylon::Metric::Gauge < Babylon::Metric
  protected

  def record
    # some code here
  end
end

and the parent class of these is

class Babylon::Metric
  class ArgumentError < ArgumentError; end

  def initialize name, category, amount
    @name = name
    @category = category
    @amount = amount
  end

  def self.call name:, category:, amount:
    new(name, category, amount).()
  end

  # What is it doing here exactly is it calling Counter or Gauge record method from here, 
  # based on valid method returns true or false?

  def call
    valid? ? record : notify
  rescue => e
    begin
      raise Babylon::Error, e.inspect
    rescue Babylon::Error => e
      Babylon::Event.(event: "babylon.error", error: e)
    end
  end

  def valid?
    # do validation
  end

  def notify
    # do some stuff 
  end

end

I think that the call method is able to in turn call Counter class and Gauge class record methods If the valid method returns true, But I don't know how it can call as these are protected members ?.

Aucun commentaire:

Enregistrer un commentaire