Skip to content

Commit 66976d6

Browse files
committed
Merge pull request #2163 from alphagov/remove-predicate-functionality
Remove predicate functionality
2 parents 968867b + 91f9275 commit 66976d6

18 files changed

+12
-644
lines changed

doc/smart-answer-flows.md

-13
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,6 @@ next_node(permitted: permitted_next_nodes) do |response|
107107
end
108108
```
109109

110-
#### DEPRECATED: Using predicates
111-
112-
This is the same example from above expressed using predicates:
113-
114-
```ruby
115-
next_node_if(:green, responded_with('green')) )
116-
next_node(:red)
117-
```
118-
119-
The `responded_with` function actually returns a [predicate](http://en.wikipedia.org/wiki/Predicate_%28mathematical_logic%29) which will be invoked during processing. If the predicate returns `true` then the `:green` node will be next, otherwise the next rule will be evaluated. In this case the next rule says `:red` is the next node with no condition.
120-
121-
See [Smart Answer predicates](smart-answers-predicates.md) for more detailed information about this style.
122-
123110
### Storing data for later use
124111

125112
You can use the `precalculate`, `next_node_calculation`, `save_input_as` and `calculate` methods to store data for later use.

doc/smart-answers-predicates.md

-47
This file was deleted.

lib/graph_presenter.rb

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ def adjacency_list
1313
adjacency_list = {}
1414
@flow.questions.each do |node|
1515
adjacency_list[node.name] = []
16-
node.next_node_function_chain.each do |(nextnode, predicates)|
17-
adjacency_list[node.name] << [nextnode, predicates.map(&:label).compact.join(" AND\n")]
18-
end
1916
node.permitted_next_nodes.each do |permitted_next_node|
2017
existing_next_nodes = adjacency_list[node.name].map(&:first)
2118
unless existing_next_nodes.include?(permitted_next_node)

lib/smart_answer/calculators/registrations_data_query.rb

-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ def commonwealth_country?(country_slug)
6060
COMMONWEALTH_COUNTRIES.include?(country_slug)
6161
end
6262

63-
def responded_with_commonwealth_country?
64-
SmartAnswer::Predicate::RespondedWith.new(COMMONWEALTH_COUNTRIES, "commonwealth country")
65-
end
66-
6763
def has_consulate?(country_slug)
6864
COUNTRIES_WITH_CONSULATES.include?(country_slug)
6965
end

lib/smart_answer/predicate/base.rb

-35
This file was deleted.

lib/smart_answer/predicate/callable.rb

-18
This file was deleted.

lib/smart_answer/predicate/responded_with.rb

-22
This file was deleted.

lib/smart_answer/predicate/response_has_all_of.rb

-17
This file was deleted.

lib/smart_answer/predicate/response_is_one_of.rb

-17
This file was deleted.

lib/smart_answer/predicate/variable_matches.rb

-53
This file was deleted.

lib/smart_answer/question/base.rb

+3-72
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
module SmartAnswer
22
module Question
33
class Base < Node
4-
attr_reader :next_node_function_chain
54
class NextNodeUndefined < StandardError; end
65

76
def initialize(flow, name, options = {}, &block)
87
@save_input_as = nil
98
@validations ||= []
10-
@next_node_function_chain ||= []
119
@default_next_node_function ||= lambda {|_|}
1210
@permitted_next_nodes = []
13-
@predicate_stack = []
14-
@predicates = {}
1511
@uses_erb_template = options[:use_erb_template]
1612
super
1713
end
@@ -24,18 +20,13 @@ def next_node(next_node = nil, permitted: [], &block)
2420
@permitted_next_nodes += permitted
2521
@default_next_node_function = block
2622
elsif next_node
27-
next_node_if(next_node)
23+
@permitted_next_nodes = [next_node]
24+
@default_next_node_function = proc { next_node }
2825
else
2926
raise ArgumentError
3027
end
3128
end
3229

33-
def next_node_if(next_node, *predicates, &block)
34-
predicates << block if block_given?
35-
@next_node_function_chain << [next_node, normalize_predicates(@predicate_stack + predicates)]
36-
@permitted_next_nodes << next_node
37-
end
38-
3930
def validate(message = nil, &block)
4031
@validations << [message, block]
4132
end
@@ -46,7 +37,7 @@ def permitted_next_nodes(*args)
4637

4738
def next_node_for(current_state, input)
4839
validate!(current_state, input)
49-
next_node = next_node_from_function_chain(current_state, input) || next_node_from_default_function(current_state, input)
40+
next_node = next_node_from_default_function(current_state, input)
5041
responses_and_input = current_state.responses + [input]
5142
raise NextNodeUndefined.new("Next node undefined. Node: #{current_state.current_node}. Responses: #{responses_and_input}") unless next_node
5243
unless @permitted_next_nodes.include?(next_node)
@@ -74,47 +65,6 @@ def transition(current_state, raw_input)
7465
new_state
7566
end
7667

77-
# Within an #on_condition block, all #next_node and #next_node_if
78-
# clauses must additionally satisfy the given predicate. Nesting of
79-
# #on_condition blocks is permitted.
80-
#
81-
# Example:
82-
#
83-
# on_condition(->(r) {r == 'tourism'}) do
84-
# next_node_if(:outcome_visit_waiver) { %w(oman qatar united-arab-emirates).include?(passport_country) }
85-
# next_node_if(:outcome_taiwan_exception) { %w(taiwan).include?(passport_country) }
86-
# next_node_if(:outcome_school_n) do
87-
# country_group_non_visa_national.include?(passport_country) or country_group_ukot.include?(passport_country))
88-
# end
89-
# next_node(:outcome_general_y)
90-
# end
91-
def on_condition(predicate, &block)
92-
@predicate_stack << predicate
93-
instance_eval(&block)
94-
@predicate_stack.pop
95-
end
96-
97-
def responded_with(acceptable_responses)
98-
SmartAnswer::Predicate::RespondedWith.new(acceptable_responses)
99-
end
100-
101-
def variable_matches(variable_name, acceptable_responses)
102-
SmartAnswer::Predicate::VariableMatches.new(variable_name, acceptable_responses)
103-
end
104-
105-
def define_predicate(identifier, label = nil, &block)
106-
raise "method #{identifier} already defined" if self.class.method_defined?(identifier)
107-
@predicates[identifier] = SmartAnswer::Predicate::Callable.new(label || identifier.to_s, &block)
108-
end
109-
110-
def respond_to_missing?(method, include_private = false)
111-
@predicates.has_key?(method)
112-
end
113-
114-
def method_missing(method, *args, &block)
115-
@predicates.fetch(method)
116-
end
117-
11868
def parse_input(raw_input)
11969
raw_input
12070
end
@@ -148,25 +98,6 @@ def validate!(current_state, input)
14898
end
14999
end
150100

151-
def normalize_predicates(predicates)
152-
predicates.map do |predicate|
153-
if predicate.is_a?(SmartAnswer::Predicate::Base)
154-
predicate
155-
else
156-
SmartAnswer::Predicate::Callable.new(nil, predicate)
157-
end
158-
end
159-
end
160-
161-
def next_node_from_function_chain(current_state, input)
162-
found = @next_node_function_chain.find do |(_, predicates)|
163-
predicates.all? do |predicate|
164-
predicate.call(current_state, input)
165-
end
166-
end
167-
found && found.first
168-
end
169-
170101
def next_node_from_default_function(current_state, input)
171102
current_state.instance_exec(input, &@default_next_node_function)
172103
end

0 commit comments

Comments
 (0)