Class: Reflekt::RuleSet

Inherits:
Object
  • Object
show all
Defined in:
lib/rule_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta_map) ⇒ RuleSet

Returns a new instance of RuleSet.

Parameters:

  • meta_map (Hash)

    The rules to apply to each data type.


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rule_set.rb', line 26

def initialize(meta_map)

  # The rules that apply to meta types.
  @meta_map = meta_map

  # The types of meta this rule set applies to.
  # Rules are only validated on their supported meta type.
  @meta_types = Set.new()

  @rules = {}

end

Instance Attribute Details

#rulesObject

Returns the value of attribute rules


21
22
23
# File 'lib/rule_set.rb', line 21

def rules
  @rules
end

Instance Method Details

#resultArray

Get the results of the rules.

Returns:

  • (Array)

    The rules.


97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rule_set.rb', line 97

def result()

  rules = {}

  @rules.each do |key, rule|
    rules[rule.class] = rule.result()
  end

  return rules

end

#test(value) ⇒ Object

Parameters:

  • value (Dynamic)

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rule_set.rb', line 70

def test(value)

  result = true
  meta_type = MetaBuilder.data_type_to_meta_type(value)

  # Fail if value's meta type not testable by rule set.
  unless @meta_types.include? meta_type
    return false
  end

  @rules.each do |klass, rule|
    if (rule.type == meta_type)
      unless rule.test(value)
         result = false
      end
    end
  end

  return result

end

#train(meta) ⇒ Object

Train rule set on metadata.

Parameters:

  • meta (Hash)

    The metadata to train on.


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rule_set.rb', line 44

def train(meta)

  # Track supported meta types.
  meta_type = meta[:type]
  @meta_types << meta_type

  # Get rule types for this meta type.
  if @meta_map.key? meta_type
    @meta_map[meta_type].each do |rule_type|

      # Ensure rule exists.
      if @rules[rule_type].nil?
        @rules[rule_type] = rule_type.new()
      end

      # Train rule.
      @rules[rule_type].train(meta)

    end
  end

end