add constant

This commit is contained in:
simon lehericey 2022-06-09 11:48:30 +02:00
parent ed2d9ed861
commit 58da4805fa
5 changed files with 93 additions and 0 deletions

View file

@ -0,0 +1,39 @@
class Logic::Constant < Logic::Term
attr_reader :value
def initialize(value)
@value = value
end
def compute(_champs = nil) = @value
def to_s = @value.to_s
def type
case @value
when TrueClass, FalseClass
:boolean
when Integer, Float
:number
else
@value.class.name.downcase.to_sym
end
end
def errors(_stable_ids = nil) = []
def to_h
{
"op" => self.class.name,
"value" => @value
}
end
def self.from_h(h)
self.new(h['value'])
end
def ==(other)
self.class == other.class && @value == other.value
end
end

5
app/models/logic/term.rb Normal file
View file

@ -0,0 +1,5 @@
class Logic::Term
def to_json
to_h.to_json
end
end