Add id to term

This commit is contained in:
simon lehericey 2022-07-05 14:37:53 +02:00
parent aa291e8f8e
commit 87385d03fd
7 changed files with 46 additions and 15 deletions

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,
"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)
@ -60,12 +61,13 @@ class Logic::ChampValue < Logic::Term
def to_h
{
"op" => self.class.name,
"stable_id" => @stable_id
"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
@ -34,12 +35,13 @@ class Logic::Constant < Logic::Term
def to_h
{
"op" => self.class.name,
"value" => @value
"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
"op" => 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)
"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) }