demarches-normaliennes/app/models/logic/constant.rb

49 lines
788 B
Ruby
Raw Normal View History

2022-06-09 11:48:30 +02:00
class Logic::Constant < Logic::Term
attr_reader :value
def initialize(value)
2022-06-09 11:48:30 +02:00
@value = value
end
def compute(_champs = nil) = @value
2022-09-26 21:21:55 +02:00
def to_s(_type_de_champs = [])
2022-06-27 12:32:00 +02:00
case @value
when TrueClass
I18n.t('utils.yes')
when FalseClass
I18n.t('utils.no')
else
@value.to_s
end
end
2022-06-09 11:48:30 +02:00
2022-09-26 21:07:43 +02:00
def type(_type_de_champs = [])
2022-06-09 11:48:30 +02:00
case @value
when TrueClass, FalseClass
:boolean
when Integer, Float
:number
else
@value.class.name.downcase.to_sym
end
end
2022-09-26 21:11:43 +02:00
def errors(_type_de_champs = nil) = []
2022-06-09 11:48:30 +02:00
def to_h
{
2022-07-05 14:47:32 +02:00
"term" => self.class.name,
"value" => @value
2022-06-09 11:48:30 +02:00
}
end
def self.from_h(h)
self.new(h['value'])
2022-06-09 11:48:30 +02:00
end
def ==(other)
self.class == other.class && @value == other.value
end
end