Class: Reflekt::RuleSetAggregator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta_map) ⇒ RuleSetAggregator

Returns a new instance of RuleSetAggregator.

Parameters:

  • meta_map (Hash)

    The rules that apply to each meta type.


21
22
23
24
25
# File 'lib/rule_set_aggregator.rb', line 21

def initialize(meta_map)
  @meta_map = meta_map
  # Key rule sets by class and method.
  @rule_sets = {}
end

Class Method Details

.testable?(args, input_rule_sets) ⇒ Boolean

Returns:

  • (Boolean)

193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/rule_set_aggregator.rb', line 193

def self.testable?(args, input_rule_sets)

  args.each_with_index do |arg, arg_num|

    rule_type = value_to_rule_type(arg)
    if input_rule_sets[arg_num].rules[rule_type].nil?
      return false
    end

  end

  return true

end

.value_to_rule_type(value) ⇒ Object

Get the base rule type for a data type.


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rule_set_aggregator.rb', line 175

def self.value_to_rule_type(value)

  data_type = value.class

  rule_types = {
    Array      => ArrayRule,
    TrueClass  => BooleanRule,
    FalseClass => BooleanRule,
    Float      => FloatRule,
    Integer    => IntegerRule,
    NilClass   => NullRule,
    String     => StringRule
  }

  return rule_types[data_type]

end

Instance Method Details

#get_input_rule_sets(klass, method) ⇒ Array

Get aggregated RuleSets for all inputs.

Parameters:

  • klass (Symbol)
  • method (Symbol)

Returns:

  • (Array)

156
157
158
# File 'lib/rule_set_aggregator.rb', line 156

def get_input_rule_sets(klass, method)
  @rule_sets.dig(klass, method, :inputs)
end

#get_output_rule_set(klass, method) ⇒ RuleSet

Get an aggregated RuleSet for an output.

Parameters:

  • klass (Symbol)
  • method (Symbol)

Returns:


168
169
170
# File 'lib/rule_set_aggregator.rb', line 168

def get_output_rule_set(klass, method)
  @rule_sets.dig(klass, method, :output)
end

#test_inputs(inputs, input_rule_sets) ⇒ Object

Validate inputs.

Parameters:

  • inputs (Array)

    The method's arguments.

  • input_rule_sets (Array)

    The RuleSets to validate each input with.


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rule_set_aggregator.rb', line 100

def test_inputs(inputs, input_rule_sets)

  # Default result to PASS.
  result = true

  # Validate each argument against each rule set for that argument.
  inputs.each_with_index do |input, arg_num|

    unless input_rule_sets[arg_num].nil?

      rule_set = input_rule_sets[arg_num]

      unless rule_set.test(input)
        result = false
      end

    end
  end

  return result

end

#test_output(output, output_rule_set) ⇒ Object

Validate output.

Parameters:

  • output (Dynamic)

    The method's return value.

  • output_rule_set (RuleSet)

    The rule set to validate the output with.


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rule_set_aggregator.rb', line 130

def test_output(output, output_rule_set)

  # Default to a PASS result.
  result = true

  unless output_rule_set.nil?

    # Validate output rule set for that argument.
    unless output_rule_set.test(output)
      result = false
    end

  end

  return result

end

#train(controls) ⇒ Object

Create aggregated rule sets from control metadata.

Parameters:

  • controls (Array)

    Controls with metadata.


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rule_set_aggregator.rb', line 34

def train(controls)

  # On first use there are no previous controls.
  return if controls.nil?

  controls.each do |control|

    klass = control["class"].to_sym
    method = control["method"].to_sym

    ##
    # INPUT
    ##

    # Singular null input.
    if control["inputs"].nil?
      train_input(klass, method, nil, 0)
    # Multiple inputs.
    else
      control["inputs"].each_with_index do |meta, arg_num|
        train_input(klass, method, meta, arg_num)
      end
    end

    ##
    # OUTPUT
    ##

    # Get rule set.
    output_rule_set = get_output_rule_set(klass, method)
    if output_rule_set.nil?
      output_rule_set = RuleSet.new(@meta_map)
      set_output_rule_set(klass, method, output_rule_set)
    end

    # Train on metadata.
    output_rule_set.train(Meta.deserialize(control["output"]))

  end

end

#train_input(klass, method, meta, arg_num) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rule_set_aggregator.rb', line 76

def train_input(klass, method, meta, arg_num)

  # Get deserialized meta.
  meta = Meta.deserialize(meta)

  # Get rule set.
  rule_set = get_input_rule_set(klass, method, arg_num)
  if rule_set.nil?
    rule_set = RuleSet.new(@meta_map)
    set_input_rule_set(klass, method, arg_num, rule_set)
  end

  # Train on metadata.
  rule_set.train(meta)

end