Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#cast_value properly handles nil #10

Merged
merged 1 commit into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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