Merge pull request #7533 from betagouv/add_uuid_to_condition

feature: ajoute un identifiant aux termes d'une condition
This commit is contained in:
LeSim 2022-07-05 14:53:54 +02:00 committed by GitHub
commit f6384fbc1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 20 deletions

View file

@ -1,6 +1,6 @@
module Logic
def self.from_h(h)
class_from_name(h['op']).from_h(h)
class_from_name(h['term']).from_h(h)
end
def self.from_json(s)

View file

@ -1,20 +1,22 @@
class Logic::BinaryOperator < Logic::Term
attr_reader :left, :right
def initialize(left, right)
def initialize(left, right, id = nil)
@left, @right = left, right
super(id)
end
def to_h
{
"op" => self.class.name,
"term" => self.class.name,
"left" => @left.to_h,
"right" => @right.to_h
"right" => @right.to_h,
"id" => @id
}
end
def self.from_h(h)
self.new(Logic.from_h(h['left']), Logic.from_h(h['right']))
self.new(Logic.from_h(h['left']), Logic.from_h(h['right']), h['id'])
end
def errors(stable_ids = [])

View file

@ -17,8 +17,9 @@ class Logic::ChampValue < Logic::Term
attr_reader :stable_id
def initialize(stable_id)
def initialize(stable_id, id = nil)
@stable_id = stable_id
super(id)
end
def compute(champs)
@ -59,13 +60,14 @@ class Logic::ChampValue < Logic::Term
def to_h
{
"op" => self.class.name,
"stable_id" => @stable_id
"term" => self.class.name,
"stable_id" => @stable_id,
"id" => @id
}
end
def self.from_h(h)
self.new(h['stable_id'])
self.new(h['stable_id'], h['id'])
end
def ==(other)

View file

@ -1,8 +1,9 @@
class Logic::Constant < Logic::Term
attr_reader :value
def initialize(value)
def initialize(value, id = nil)
@value = value
super(id)
end
def compute(_champs = nil) = @value
@ -33,13 +34,14 @@ class Logic::Constant < Logic::Term
def to_h
{
"op" => self.class.name,
"value" => @value
"term" => self.class.name,
"value" => @value,
"id" => @id
}
end
def self.from_h(h)
self.new(h['value'])
self.new(h['value'], h['id'])
end
def ==(other)

View file

@ -1,4 +1,8 @@
class Logic::Empty < Logic::Term
def initialize(id = nil)
super(id)
end
def to_s = I18n.t('logic.empty')
def type = :empty
@ -7,12 +11,13 @@ class Logic::Empty < Logic::Term
def to_h
{
"op" => self.class.name
"term" => self.class.name,
"id" => @id
}
end
def self.from_h(_h)
self.new
def self.from_h(h)
self.new(h['id'])
end
def ==(other)

View file

@ -1,19 +1,21 @@
class Logic::NAryOperator < Logic::Term
attr_reader :operands
def initialize(operands)
def initialize(operands, id = nil)
@operands = operands
super(id)
end
def to_h
{
"op" => self.class.name,
"operands" => @operands.map(&:to_h)
"term" => self.class.name,
"operands" => @operands.map(&:to_h),
"id" => @id
}
end
def self.from_h(h)
self.new(h['operands'].map { |operand_h| Logic.from_h(operand_h) })
self.new(h['operands'].map { |operand_h| Logic.from_h(operand_h) }, h['id'])
end
def errors(stable_ids = [])

View file

@ -1,4 +1,10 @@
class Logic::Term
attr_reader :id
def initialize(id = nil)
@id = id || SecureRandom.uuid
end
def to_json
to_h.to_json
end

View file

@ -15,6 +15,18 @@ describe Logic do
.to eq(ds_and([constant(true), constant(true), constant(false)]))
end
it 'saves its id' do
[
constant(1),
empty,
champ_value(1),
ds_eq(empty, empty),
ds_and([constant(true), constant(true)])
].each do |term|
expect(Logic.from_h(term.to_h).id).to eq(term.id)
end
end
describe '.ensure_compatibility_from_left' do
subject { Logic.ensure_compatibility_from_left(condition) }