Class: Bali::RuleClass

Inherits:
Object
  • Object
show all
Defined in:
lib/bali/foundations/rule/rule_class.rb

Overview

the parent of all Bali::RuleGroup.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_class) ⇒ RuleClass

Returns a new instance of RuleClass.



15
16
17
18
19
20
21
22
23
24
# File 'lib/bali/foundations/rule/rule_class.rb', line 15

def initialize(target_class)
  if target_class.is_a?(Class)
    @target_class = target_class
  else
    raise Bali::DslError, "Target class must be a Class"
  end

  self.rule_groups = {}
  self.others_rule_group = Bali::RuleGroup.new(target_class, "__*__")
end

Instance Attribute Details

#others_rule_groupObject

rule group for “other” subtargets, always checked the last time after the “more proper” rule groups are checked



13
14
15
# File 'lib/bali/foundations/rule/rule_class.rb', line 13

def others_rule_group
  @others_rule_group
end

#rule_groupsObject

consist of canonised subtarget and its rule group, eg:

general_user: RuleGroup



9
10
11
# File 'lib/bali/foundations/rule/rule_class.rb', line 9

def rule_groups
  @rule_groups
end

#target_classObject (readonly)

Returns the value of attribute target_class.



3
4
5
# File 'lib/bali/foundations/rule/rule_class.rb', line 3

def target_class
  @target_class
end

Instance Method Details

#add_rule_group(rule_group) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bali/foundations/rule/rule_class.rb', line 26

def add_rule_group(rule_group)
  if rule_group.is_a?(Bali::RuleGroup)
    target_user = rule_group.subtarget
    if target_user == "__*__" || target_user == :"__*__"
      raise Bali::DslError, "__*__ is a reserved subtarget used by Bali's internal"
    end
    self.rule_groups[rule_group.subtarget] = rule_group
  else
    raise Bali::DslError, "Rule group must be an instance of Bali::RuleGroup, got: #{rule_group.class.name}"
  end
end

#clone(options = {}) ⇒ Object

options can contains:

:target_class => identifying the target class on which the clone will be applied


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bali/foundations/rule/rule_class.rb', line 46

def clone(options = {})
  target_class = options.fetch(:target_class)
  cloned_rc = Bali::RuleClass.new(target_class)

  rule_groups.each do |subtarget, rule_group|
    rule_group_clone = rule_group.clone
    rule_group_clone.target = target_class
    cloned_rc.add_rule_group(rule_group_clone)
  end

  others_rule_group_clone = others_rule_group.clone
  others_rule_group_clone.target = target_class
  cloned_rc.others_rule_group = others_rule_group_clone

  cloned_rc
end

#rules_for(subtarget) ⇒ Object



38
39
40
41
42
# File 'lib/bali/foundations/rule/rule_class.rb', line 38

def rules_for(subtarget)
  return others_rule_group if subtarget == "__*__"
  subtarget = Bali::RuleGroup.canon_name(subtarget)
  self.rule_groups[subtarget]
end