add constant
This commit is contained in:
parent
ed2d9ed861
commit
58da4805fa
5 changed files with 93 additions and 0 deletions
39
app/models/logic/constant.rb
Normal file
39
app/models/logic/constant.rb
Normal 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
5
app/models/logic/term.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class Logic::Term
|
||||
def to_json
|
||||
to_h.to_json
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue