PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/js/RuleList.js

https://bitbucket.org/juliand89/firewall-game
JavaScript | 31 lines | 27 code | 4 blank | 0 comment | 4 complexity | c0990dae87cd3982e7229542083f7232 MD5 | raw file
  1. var RuleList = Backbone.Collection.extend({
  2. model: Rule,
  3. initialize: function (models, options) {
  4. var rules = _.map(models, function (rule) {
  5. return (rule instanceof Rule) ? rule : new Rule(rule);
  6. });
  7. this.models = rules;
  8. options = options || {};
  9. if (options.policy === undefined) {
  10. this.policy = 'allow';
  11. } else {
  12. this.policy = options.policy;
  13. }
  14. },
  15. decide: function (packet) {
  16. var rule = _.find(this.models, function (rule) {
  17. return rule.match(packet);
  18. });
  19. return (rule === undefined) ? this.policy : rule.get('action');
  20. },
  21. addRule: function (rule) {
  22. if (!(rule instanceof Rule)) {
  23. rule = new Rule(rule);
  24. }
  25. this.add(rule);
  26. }
  27. });