add champ_value
This commit is contained in:
parent
daaa54b6f0
commit
809d991819
4 changed files with 132 additions and 1 deletions
|
@ -8,7 +8,7 @@ module Logic
|
|||
end
|
||||
|
||||
def self.class_from_name(name)
|
||||
[Constant, Empty, LessThan, LessThanEq, Eq, GreaterThanEq, GreaterThan, EmptyOperator, And, Or]
|
||||
[ChampValue, Constant, Empty, LessThan, LessThanEq, Eq, GreaterThanEq, GreaterThan, EmptyOperator, And, Or]
|
||||
.find { |c| c.name == name }
|
||||
end
|
||||
|
||||
|
@ -33,6 +33,8 @@ module Logic
|
|||
|
||||
def constant(value) = Logic::Constant.new(value)
|
||||
|
||||
def champ_value(stable_id) = Logic::ChampValue.new(stable_id)
|
||||
|
||||
def empty = Logic::Empty.new
|
||||
|
||||
def empty_operator(left, right) = Logic::EmptyOperator.new(left, right)
|
||||
|
|
70
app/models/logic/champ_value.rb
Normal file
70
app/models/logic/champ_value.rb
Normal file
|
@ -0,0 +1,70 @@
|
|||
class Logic::ChampValue < Logic::Term
|
||||
attr_reader :stable_id
|
||||
|
||||
def initialize(stable_id)
|
||||
@stable_id = stable_id
|
||||
end
|
||||
|
||||
def compute(champs)
|
||||
case type_de_champ.type_champ
|
||||
when all_types.fetch(:yes_no),
|
||||
all_types.fetch(:checkbox)
|
||||
champ(champs).true?
|
||||
when all_types.fetch(:integer_number), all_types.fetch(:decimal_number)
|
||||
champ(champs).for_api
|
||||
when all_types.fetch(:text)
|
||||
champ(champs).value
|
||||
end
|
||||
end
|
||||
|
||||
def to_s = "#{type_de_champ.libelle} Nº#{stable_id}"
|
||||
|
||||
def type
|
||||
case type_de_champ.type_champ
|
||||
when all_types.fetch(:yes_no),
|
||||
all_types.fetch(:checkbox)
|
||||
:boolean
|
||||
when all_types.fetch(:integer_number), all_types.fetch(:decimal_number)
|
||||
:number
|
||||
when all_types.fetch(:text)
|
||||
:string
|
||||
end
|
||||
end
|
||||
|
||||
def errors(stable_ids)
|
||||
if !stable_ids.include?(stable_id)
|
||||
["le type de champ stable_id=#{stable_id} n'est pas disponible"]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def to_h
|
||||
{
|
||||
"op" => self.class.name,
|
||||
"stable_id" => @stable_id
|
||||
}
|
||||
end
|
||||
|
||||
def self.from_h(h)
|
||||
self.new(h['stable_id'])
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
self.class == other.class && @stable_id == other.stable_id
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def type_de_champ
|
||||
TypeDeChamp.find_by(stable_id: stable_id)
|
||||
end
|
||||
|
||||
def champ(champs)
|
||||
champs.find { |c| c.stable_id == stable_id }
|
||||
end
|
||||
|
||||
def all_types
|
||||
TypeDeChamp.type_champs
|
||||
end
|
||||
end
|
57
spec/models/logic/champ_value_spec.rb
Normal file
57
spec/models/logic/champ_value_spec.rb
Normal file
|
@ -0,0 +1,57 @@
|
|||
describe Logic::ChampValue do
|
||||
include Logic
|
||||
|
||||
subject { champ_value(champ.stable_id).compute([champ]) }
|
||||
|
||||
context 'yes_no tdc' do
|
||||
let(:value) { 'true' }
|
||||
let(:champ) { create(:champ_yes_no, value: value) }
|
||||
|
||||
it { expect(champ_value(champ.stable_id).type).to eq(:boolean) }
|
||||
|
||||
context 'with true value' do
|
||||
it { is_expected.to be(true) }
|
||||
end
|
||||
|
||||
context 'with false value' do
|
||||
let(:value) { 'false' }
|
||||
|
||||
it { is_expected.to be(false) }
|
||||
end
|
||||
end
|
||||
|
||||
context 'text tdc' do
|
||||
let(:champ) { create(:champ_text, value: 'text') }
|
||||
|
||||
it { expect(champ_value(champ.stable_id).type).to eq(:string) }
|
||||
it { is_expected.to eq('text') }
|
||||
end
|
||||
|
||||
context 'integer tdc' do
|
||||
let(:champ) { create(:champ_integer_number, value: '42') }
|
||||
|
||||
it { expect(champ_value(champ.stable_id).type).to eq(:number) }
|
||||
it { is_expected.to eq(42) }
|
||||
end
|
||||
|
||||
context 'decimal tdc' do
|
||||
let(:champ) { create(:champ_decimal_number, value: '42.01') }
|
||||
|
||||
it { expect(champ_value(champ.stable_id).type).to eq(:number) }
|
||||
it { is_expected.to eq(42.01) }
|
||||
end
|
||||
|
||||
context 'checkbox tdc' do
|
||||
let(:champ) { create(:champ_checkbox, value: 'on') }
|
||||
|
||||
it { expect(champ_value(champ.stable_id).type).to eq(:boolean) }
|
||||
it { is_expected.to eq(true) }
|
||||
end
|
||||
|
||||
describe 'errors' do
|
||||
let(:champ) { create(:champ) }
|
||||
|
||||
it { expect(champ_value(champ.stable_id).errors([champ.stable_id])).to be_empty }
|
||||
it { expect(champ_value(champ.stable_id).errors(['other stable ids'])).to eq(["le type de champ stable_id=#{champ.stable_id} n'est pas disponible"]) }
|
||||
end
|
||||
end
|
|
@ -7,6 +7,8 @@ describe Logic do
|
|||
|
||||
expect(Logic.from_h(empty.to_h)).to eq(empty)
|
||||
|
||||
expect(Logic.from_h(champ_value(1).to_h)).to eq(champ_value(1))
|
||||
|
||||
expect(Logic.from_h(greater_than(constant(1), constant(2)).to_h)).to eq(greater_than(constant(1), constant(2)))
|
||||
|
||||
expect(Logic.from_h(ds_and([constant(true), constant(true), constant(false)]).to_h))
|
||||
|
|
Loading…
Reference in a new issue