Skip to content

Commit

Permalink
Merge pull request #10 from DmitryTsepelev/cast-value-with-nil
Browse files Browse the repository at this point in the history
#cast_value properly handles nil
  • Loading branch information
DmitryTsepelev committed Jun 13, 2019
2 parents 7bb7f06 + 575ba45 commit 8192753
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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][])
Expand Down
3 changes: 1 addition & 2 deletions Gemfile
Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/store_model/types/array_type.rb
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/store_model/types/json_type.rb
Expand Up @@ -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, " \
Expand Down
11 changes: 9 additions & 2 deletions spec/store_model/types/array_type_spec.rb
Expand Up @@ -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)
Expand All @@ -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) { {} }

Expand Down
10 changes: 8 additions & 2 deletions spec/store_model/types/json_type_spec.rb
Expand Up @@ -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
Expand All @@ -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) { [] }

Expand Down

0 comments on commit 8192753

Please sign in to comment.