Home > Mobile >  overide setter attribute dosen't calling
overide setter attribute dosen't calling

Time:09-16

I create object TriggerFillLocalRunAt.

If attribute tables_ids is null need set to this attribute all ids. I would like to overide getter of tables_ids.

class TriggersFillLocalRunAt
  attr_reader :tables_ids
  attr_reader :offset
  attr_reader :timezone
  attr_reader :org
  attr_reader :run_at

  def initialize(org, table_ids, offset, timezone, run_at)
    @org = org
    tables_ids = table_ids
    @offset = offset.to_i
    @timezone = timezone
    @run_at = run_at
  end

  def tables_ids=(value)
    @tables_ids = value ? value.split(' ').map(&:to_i) : tables_ids_for_subdomain
  end

  def perform
.................
end

end

I create object TriggersFillLocalRunAt.new(org, table_ids, offset, timezone, run_at).perform

where table_ids is null

My overide getter dosen't calling and @tables_ids is null.

CodePudding user response:

You always need to explicitly use self when calling setters:

def initialize(org, table_ids, offset, timezone, run_at)
  @org = org
  self.tables_ids = table_ids
  @offset = offset.to_i
  @timezone = timezone
  @run_at = run_at
end

If you don't use self you're simply assigning the local variable tables_ids. I would also suggest that you use keyword arguments so that you don't have to remember the order of 5 parameters:

def initialize(org:, table_ids:, offset:, timezone:, run_at:)
  @org = org
  self.tables_ids = table_ids
  @offset = offset.to_i
  @timezone = timezone
  @run_at = run_at
end
  • Related