diff --git a/CHANGELOG.md b/CHANGELOG.md index bab8f93..928c368 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## master +- [PR #10](https://github.com/DmitryTsepelev/store_model/pull/6) Fixes [Issue #9](https://github.com/DmitryTsepelev/store_model/pull/9) + ## 0.3.0 (2019-05-06) - [PR #6](https://github.com/DmitryTsepelev/store_model/pull/6) Rewrite MergeErrorStrategy to work with Rails 6.1 ([@DmitryTsepelev][]) diff --git a/Gemfile b/Gemfile index 2002693..020ba97 100644 --- a/Gemfile +++ b/Gemfile @@ -3,12 +3,11 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } gemspec -gem "sqlite3", "~> 1.3.6" - local_gemfile = File.join(__dir__, "Gemfile.local") if File.exist?(local_gemfile) eval(File.read(local_gemfile)) # rubocop:disable Security/Eval else + gem "sqlite3", "~> 1.3.6" gem "activerecord", "~> 5.0" end diff --git a/lib/store_model/types/array_type.rb b/lib/store_model/types/array_type.rb index 529de14..a7b399b 100644 --- a/lib/store_model/types/array_type.rb +++ b/lib/store_model/types/array_type.rb @@ -17,6 +17,7 @@ def cast_value(value) case value when String then decode_and_initialize(value) when Array then ensure_model_class(value) + when nil then value else raise StoreModel::Types::CastError, "failed casting #{value.inspect}, only String or Array instances are allowed" diff --git a/lib/store_model/types/json_type.rb b/lib/store_model/types/json_type.rb index 9d57b4d..3644fa4 100644 --- a/lib/store_model/types/json_type.rb +++ b/lib/store_model/types/json_type.rb @@ -17,7 +17,7 @@ def cast_value(value) case value when String then decode_and_initialize(value) when Hash then @model_klass.new(value) - when @model_klass then value + when @model_klass, nil then value else raise StoreModel::Types::CastError, "failed casting #{value.inspect}, only String, " \ diff --git a/spec/store_model/types/array_type_spec.rb b/spec/store_model/types/array_type_spec.rb index 6072c4f..ce296e0 100644 --- a/spec/store_model/types/array_type_spec.rb +++ b/spec/store_model/types/array_type_spec.rb @@ -35,10 +35,11 @@ end describe "#cast_value" do - shared_examples "cast examples" do - subject { type.cast_value(value) } + subject { type.cast_value(value) } + shared_examples "cast examples" do it { is_expected.to be_a(Array) } + it "assigns attributes" do subject.zip(attributes_array).each do |config, config_attributes| expect(config).to have_attributes(config_attributes) @@ -61,6 +62,12 @@ include_examples "cast examples" end + context "when nil is passed" do + let(:value) { nil } + + it { is_expected.to be_nil } + end + context "when instance of illegal class is passed" do let(:value) { {} } diff --git a/spec/store_model/types/json_type_spec.rb b/spec/store_model/types/json_type_spec.rb index 5304739..ff41956 100644 --- a/spec/store_model/types/json_type_spec.rb +++ b/spec/store_model/types/json_type_spec.rb @@ -25,9 +25,9 @@ end describe "#cast_value" do - shared_examples "cast examples" do - subject { type.cast_value(value) } + subject { type.cast_value(value) } + shared_examples "cast examples" do it { is_expected.to be_a(Configuration) } it("assigns attributes") { is_expected.to have_attributes(attributes) } end @@ -47,6 +47,12 @@ include_examples "cast examples" end + context "when nil is passed" do + let(:value) { nil } + + it { is_expected.to be_nil } + end + context "when instance of illegal class is passed" do let(:value) { [] }