Class: Inquirex::Rules::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/inquirex/rules/base.rb

Overview

Abstract base for rule AST nodes. Subclasses implement #evaluate and #to_s for use in transitions and visibility conditions. Rules are immutable and serialize to/from JSON for cross-platform evaluation.

Direct Known Subclasses

All, Any, Contains, Equals, GreaterThan, LessThan, NotEmpty

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_h(hash) ⇒ Rules::Base

Deserializes a rule from a plain Hash (e.g. parsed from JSON).

Parameters:

  • hash (Hash)

    rule hash with string or symbol keys

Returns:

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/inquirex/rules/base.rb', line 36

def self.from_h(hash)
  op = hash["op"] || hash[:op]
  case op.to_s
  when "contains"   then Contains.from_h(hash)
  when "equals"     then Equals.from_h(hash)
  when "greater_than" then GreaterThan.from_h(hash)
  when "less_than"  then LessThan.from_h(hash)
  when "not_empty"  then NotEmpty.from_h(hash)
  when "all"        then All.from_h(hash)
  when "any"        then Any.from_h(hash)
  else
    raise Inquirex::Errors::SerializationError, "Unknown rule operator: #{op.inspect}"
  end
end

Instance Method Details

#evaluate(_answers) ⇒ Boolean

Evaluates the rule against the current answer context.

Parameters:

  • _answers (Hash)

    step_id => value

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/inquirex/rules/base.rb', line 13

def evaluate(_answers)
  raise NotImplementedError, "#{self.class}#evaluate must be implemented"
end

#to_hHash

Serializes the rule to a plain Hash for JSON serialization.

Returns:

  • (Hash)

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/inquirex/rules/base.rb', line 20

def to_h
  raise NotImplementedError, "#{self.class}#to_h must be implemented"
end

#to_sString

Human-readable representation (e.g. for graph labels).

Returns:

  • (String)

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/inquirex/rules/base.rb', line 27

def to_s
  raise NotImplementedError, "#{self.class}#to_s must be implemented"
end