Merge pull request #7533 from betagouv/add_uuid_to_condition
feature: ajoute un identifiant aux termes d'une condition
This commit is contained in:
commit
f6384fbc1d
8 changed files with 51 additions and 20 deletions
|
@ -1,6 +1,6 @@
|
||||||
module Logic
|
module Logic
|
||||||
def self.from_h(h)
|
def self.from_h(h)
|
||||||
class_from_name(h['op']).from_h(h)
|
class_from_name(h['term']).from_h(h)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.from_json(s)
|
def self.from_json(s)
|
||||||
|
|
|
@ -1,20 +1,22 @@
|
||||||
class Logic::BinaryOperator < Logic::Term
|
class Logic::BinaryOperator < Logic::Term
|
||||||
attr_reader :left, :right
|
attr_reader :left, :right
|
||||||
|
|
||||||
def initialize(left, right)
|
def initialize(left, right, id = nil)
|
||||||
@left, @right = left, right
|
@left, @right = left, right
|
||||||
|
super(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_h
|
def to_h
|
||||||
{
|
{
|
||||||
"op" => self.class.name,
|
"term" => self.class.name,
|
||||||
"left" => @left.to_h,
|
"left" => @left.to_h,
|
||||||
"right" => @right.to_h
|
"right" => @right.to_h,
|
||||||
|
"id" => @id
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.from_h(h)
|
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
|
end
|
||||||
|
|
||||||
def errors(stable_ids = [])
|
def errors(stable_ids = [])
|
||||||
|
|
|
@ -17,8 +17,9 @@ class Logic::ChampValue < Logic::Term
|
||||||
|
|
||||||
attr_reader :stable_id
|
attr_reader :stable_id
|
||||||
|
|
||||||
def initialize(stable_id)
|
def initialize(stable_id, id = nil)
|
||||||
@stable_id = stable_id
|
@stable_id = stable_id
|
||||||
|
super(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def compute(champs)
|
def compute(champs)
|
||||||
|
@ -59,13 +60,14 @@ class Logic::ChampValue < Logic::Term
|
||||||
|
|
||||||
def to_h
|
def to_h
|
||||||
{
|
{
|
||||||
"op" => self.class.name,
|
"term" => self.class.name,
|
||||||
"stable_id" => @stable_id
|
"stable_id" => @stable_id,
|
||||||
|
"id" => @id
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.from_h(h)
|
def self.from_h(h)
|
||||||
self.new(h['stable_id'])
|
self.new(h['stable_id'], h['id'])
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
class Logic::Constant < Logic::Term
|
class Logic::Constant < Logic::Term
|
||||||
attr_reader :value
|
attr_reader :value
|
||||||
|
|
||||||
def initialize(value)
|
def initialize(value, id = nil)
|
||||||
@value = value
|
@value = value
|
||||||
|
super(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def compute(_champs = nil) = @value
|
def compute(_champs = nil) = @value
|
||||||
|
@ -33,13 +34,14 @@ class Logic::Constant < Logic::Term
|
||||||
|
|
||||||
def to_h
|
def to_h
|
||||||
{
|
{
|
||||||
"op" => self.class.name,
|
"term" => self.class.name,
|
||||||
"value" => @value
|
"value" => @value,
|
||||||
|
"id" => @id
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.from_h(h)
|
def self.from_h(h)
|
||||||
self.new(h['value'])
|
self.new(h['value'], h['id'])
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
class Logic::Empty < Logic::Term
|
class Logic::Empty < Logic::Term
|
||||||
|
def initialize(id = nil)
|
||||||
|
super(id)
|
||||||
|
end
|
||||||
|
|
||||||
def to_s = I18n.t('logic.empty')
|
def to_s = I18n.t('logic.empty')
|
||||||
|
|
||||||
def type = :empty
|
def type = :empty
|
||||||
|
@ -7,12 +11,13 @@ class Logic::Empty < Logic::Term
|
||||||
|
|
||||||
def to_h
|
def to_h
|
||||||
{
|
{
|
||||||
"op" => self.class.name
|
"term" => self.class.name,
|
||||||
|
"id" => @id
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.from_h(_h)
|
def self.from_h(h)
|
||||||
self.new
|
self.new(h['id'])
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
|
|
|
@ -1,19 +1,21 @@
|
||||||
class Logic::NAryOperator < Logic::Term
|
class Logic::NAryOperator < Logic::Term
|
||||||
attr_reader :operands
|
attr_reader :operands
|
||||||
|
|
||||||
def initialize(operands)
|
def initialize(operands, id = nil)
|
||||||
@operands = operands
|
@operands = operands
|
||||||
|
super(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_h
|
def to_h
|
||||||
{
|
{
|
||||||
"op" => self.class.name,
|
"term" => self.class.name,
|
||||||
"operands" => @operands.map(&:to_h)
|
"operands" => @operands.map(&:to_h),
|
||||||
|
"id" => @id
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.from_h(h)
|
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
|
end
|
||||||
|
|
||||||
def errors(stable_ids = [])
|
def errors(stable_ids = [])
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
class Logic::Term
|
class Logic::Term
|
||||||
|
attr_reader :id
|
||||||
|
|
||||||
|
def initialize(id = nil)
|
||||||
|
@id = id || SecureRandom.uuid
|
||||||
|
end
|
||||||
|
|
||||||
def to_json
|
def to_json
|
||||||
to_h.to_json
|
to_h.to_json
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,6 +15,18 @@ describe Logic do
|
||||||
.to eq(ds_and([constant(true), constant(true), constant(false)]))
|
.to eq(ds_and([constant(true), constant(true), constant(false)]))
|
||||||
end
|
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
|
describe '.ensure_compatibility_from_left' do
|
||||||
subject { Logic.ensure_compatibility_from_left(condition) }
|
subject { Logic.ensure_compatibility_from_left(condition) }
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue