diff --git a/lib/store_model/types/enum_type.rb b/lib/store_model/types/enum_type.rb index 0e3f067..75fc0b4 100644 --- a/lib/store_model/types/enum_type.rb +++ b/lib/store_model/types/enum_type.rb @@ -34,7 +34,7 @@ def cast_value(value) case value when String, Symbol then cast_symbol_value(value) - when Integer then cast_integer_value(value) + when Integer, Float then cast_integer_value(value) else raise StoreModel::Types::CastError, "failed casting #{value.inspect}, only String, Symbol or " \ diff --git a/spec/store_model/types/enum_type_spec.rb b/spec/store_model/types/enum_type_spec.rb index fc73860..b920231 100644 --- a/spec/store_model/types/enum_type_spec.rb +++ b/spec/store_model/types/enum_type_spec.rb @@ -4,6 +4,7 @@ RSpec.describe StoreModel::Types::EnumType do let(:type) { described_class.new({ active: 1, archived: 0 }, raise_on_invalid_values) } + let(:float_type) { described_class.new({ pi: 3.14, tau: 6.28 }, raise_on_invalid_values) } let(:raise_on_invalid_values) { true } describe "#type" do @@ -95,15 +96,33 @@ it { is_expected.to be_nil } end + end + + describe "float#cast_value" do + subject { float_type.cast_value(value) } + + context "when Float is passed" do + let(:value) { 3.14 } - context "when instance of illegal class is passed" do - let(:value) { 3.5 } + it { is_expected.to eq(3.14) } + + context "when value is not in the list" do + let(:value) { 1.5 } - it "raises exception" do - expect { subject }.to raise_error( - StoreModel::Types::CastError, - "failed casting #{value}, only String, Symbol or Integer instances are allowed" - ) + context "when raise_on_invalid_values is true" do + it "raises exception" do + expect { subject }.to raise_error( + ArgumentError, + "invalid value '#{value}' is assigned" + ) + end + end + + context "when raise_on_invalid_values is false" do + let(:raise_on_invalid_values) { false } + + it { is_expected.to eq(value) } + end end end end