监视子类

时间:2018-02-15 14:23:22

标签: ruby concurrency

我需要一个特定基类的所有子类的类互斥。由于MonitorMixin通过synchronize提供了一个简单的界面,我尝试使用mixin。

但我无法找到扩展基类的解决方案。

require 'monitor'

class Base
  def self.some
    synchronize { puts 'some' }
  end
end
Base.extend(MonitorMixin)

class A < Base; end
class B < Base; end

A.some
# /usr/lib/ruby/1.8/monitor.rb:215:in `mon_enter': undefined method `+' for nil:NilClass (NoMethodError)
#         from /usr/lib/ruby/1.8/monitor.rb:240:in `synchronize'

你有什么建议吗?

2 个答案:

答案 0 :(得分:1)

我不知道如何使用监视器,但如果你不介意使用继承的钩子,这很容易:

class Base
  def self.some
    @mutex.synchronize { puts 'some' }
  end

  # each subclass gets its own mutex
  def self.inherited subclass
    subclass.instance_variable_set :@mutex, Mutex.new
  end
end

答案 1 :(得分:0)

我使用type Parser err src target = ExceptT err (State [src]) target notFollowedBy :: e -> Parser e s t -> Parser e s t' -> Parser e s t notFollowedBy followedByError parser shouldFail = -- ... 挂钩找到了以下解决方案。

[HttpGet]
        [AutorisatieFilter(Rol = "Personeelsdossier | Rekeningen@Lezen")]
        public ActionResult Rekeningen()
        {
            var model = PersoneelsDossierService.GetRekeningLezenModel(Context, HuidigeDienstverbandId,GetMutatieRol(), Gebruiker.DienstverbandId);

            SetMedewerkerSelectie(model);

            model.IsBevoegd = true;

            try
            {
                BeveiligingService.ControleerManagerBevoegdheidVoorDienstverband(Context, Context.Klant.Id, int.Parse(Context.Gebruiker.ExternId), HuidigeDienstverbandId, Gebruiker.DienstverbandId);
            }
            catch(AuthenticationException)
            {
                model.IsBevoegd = false;
            }

            return PartialView("~/Areas/MSS/Views/PersoneelsDossier/Rekeningen.cshtml", model);
            //return View(model);
        }