Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from DmitryTsepelev/array-types
Add array type generation via Model#to_array_type
- Loading branch information
Showing
14 changed files
with
285 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
gem "rails", github: "rails/rails" | ||
gem "sqlite3", "~> 1.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
source "https://rubygems.org" | ||
|
||
gem "sqlite3", "~> 1.4.0" | ||
gem "activerecord", "~> 6.0.0.rc1" | ||
|
||
gemspec path: "../" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
source "https://rubygems.org" | ||
|
||
gem "rails", github: "rails/rails" | ||
gem "sqlite3", "~> 1.3.6" | ||
gem "sqlite3", "~> 1.4" | ||
|
||
gemspec path: ".." |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
require "store_model/types/json_type" | ||
require "store_model/types/array_type" | ||
|
||
module StoreModel | ||
module Types | ||
class CastError < StandardError; end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_model" | ||
|
||
module StoreModel | ||
module Types | ||
class ArrayType < ActiveModel::Type::Value | ||
def initialize(model_klass) | ||
@model_klass = model_klass | ||
end | ||
|
||
def type | ||
:array | ||
end | ||
|
||
def cast_value(value) | ||
case value | ||
when String then decode_and_initialize(value) | ||
when Array then ensure_model_class(value) | ||
else | ||
raise StoreModel::Types::CastError, | ||
"failed casting #{value.inspect}, only String or Array instances are allowed" | ||
end | ||
end | ||
|
||
def serialize(value) | ||
case value | ||
when Array | ||
ActiveSupport::JSON.encode(value) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def changed_in_place?(raw_old_value, new_value) | ||
cast_value(raw_old_value) != new_value | ||
end | ||
|
||
private | ||
|
||
# rubocop:disable Style/RescueModifier | ||
def decode_and_initialize(array_value) | ||
decoded = ActiveSupport::JSON.decode(array_value) rescue [] | ||
decoded.map { |attributes| @model_klass.new(attributes) } | ||
end | ||
# rubocop:enable Style/RescueModifier | ||
|
||
def ensure_model_class(array) | ||
array.map do |object| | ||
object.is_a?(@model_klass) ? object : @model_klass.new(object) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_model" | ||
|
||
module StoreModel | ||
module Types | ||
class JsonType < ActiveModel::Type::Value | ||
def initialize(model_klass) | ||
@model_klass = model_klass | ||
end | ||
|
||
def type | ||
:json | ||
end | ||
|
||
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 | ||
else | ||
raise StoreModel::Types::CastError, | ||
"failed casting #{value.inspect}, only String, " \ | ||
"Hash or #{@model_klass.name} instances are allowed" | ||
end | ||
end | ||
|
||
def serialize(value) | ||
case value | ||
when Hash, @model_klass | ||
ActiveSupport::JSON.encode(value) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def changed_in_place?(raw_old_value, new_value) | ||
cast_value(raw_old_value) != new_value | ||
end | ||
|
||
private | ||
|
||
# rubocop:disable Style/RescueModifier | ||
def decode_and_initialize(value) | ||
decoded = ActiveSupport::JSON.decode(value) rescue nil | ||
@model_klass.new(decoded) unless decoded.nil? | ||
end | ||
# rubocop:enable Style/RescueModifier | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe StoreModel::Types::ArrayType do | ||
let(:type) { described_class.new(Configuration) } | ||
|
||
let(:attributes_array) do | ||
[ | ||
{ | ||
color: "red", | ||
disabled_at: Time.new(2019, 2, 22, 12, 30) | ||
}, | ||
{ | ||
color: "green", | ||
disabled_at: Time.new(2019, 3, 12, 8, 10) | ||
} | ||
] | ||
end | ||
|
||
describe "#type" do | ||
subject { type.type } | ||
|
||
it { is_expected.to eq(:array) } | ||
end | ||
|
||
describe "#changed_in_place?" do | ||
let(:configurations) do | ||
attributes_array.map { |attributes| Configuration.new(attributes) } | ||
end | ||
|
||
it "marks object as changed" do | ||
expect(type.changed_in_place?([], configurations)).to be_truthy | ||
end | ||
end | ||
|
||
describe "#cast_value" do | ||
shared_examples "cast examples" do | ||
subject { type.cast_value(value) } | ||
|
||
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) | ||
end | ||
end | ||
end | ||
|
||
context "when String is passed" do | ||
let(:value) { ActiveSupport::JSON.encode(attributes_array) } | ||
include_examples "cast examples" | ||
end | ||
|
||
context "when Array of hashes is passed" do | ||
let(:value) { attributes_array } | ||
include_examples "cast examples" | ||
end | ||
|
||
context "when Array of instances is passed" do | ||
let(:value) { attributes_array.map { |attrs| Configuration.new(attrs) } } | ||
include_examples "cast examples" | ||
end | ||
|
||
context "when instance of illegal class is passed" do | ||
let(:value) { {} } | ||
|
||
it "raises exception" do | ||
expect { type.cast_value(value) }.to raise_error( | ||
StoreModel::Types::CastError, | ||
"failed casting {}, only String or Array instances are allowed" | ||
) | ||
end | ||
end | ||
end | ||
|
||
describe "#serialize" do | ||
shared_examples "serialize examples" do | ||
subject { type.serialize(value) } | ||
|
||
it { is_expected.to be_a(String) } | ||
it "is equal to attributes" do | ||
expect(subject).to eq(ActiveSupport::JSON.encode(attributes_array)) | ||
end | ||
end | ||
|
||
context "when Array is passed" do | ||
let(:value) { attributes_array } | ||
include_examples "serialize examples" | ||
end | ||
|
||
context "when String is passed" do | ||
let(:value) { ActiveSupport::JSON.encode(attributes_array) } | ||
include_examples "serialize examples" | ||
end | ||
end | ||
end |
Oops, something went wrong.