From 948127f33b5660f2d7667b4bcd2d85dc6cbd0c54 Mon Sep 17 00:00:00 2001 From: Andrew Kozin Date: Wed, 18 Jan 2023 21:13:14 +0000 Subject: [PATCH] Support hash argument in a model --- CHANGELOG.md | 8 +++++++- lib/evil/client/model.rb | 8 ++++---- spec/unit/model_spec.rb | 40 +++++++++++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c88391c..25b21ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog], and this project adheres to [Semantic Versioning]. +## [3.2.0] [2023-01-18] + +### Added +- Support for plain hash argument in the model (@nepalez) + ## [3.1.0] [2022-07-04] ### Added @@ -501,4 +506,5 @@ formats will be added. [3.0.3]: https://github.com/evilmartians/evil-client/compare/v3.0.2...v3.0.3 [3.0.4]: https://github.com/evilmartians/evil-client/compare/v3.0.3...v3.0.4 [3.0.5]: https://github.com/evilmartians/evil-client/compare/v3.0.4...v3.0.5 -[3.1.0]: https://github.com/evilmartians/evil-client/compare/v3.0.5...v3.1.0 \ No newline at end of file +[3.1.0]: https://github.com/evilmartians/evil-client/compare/v3.0.5...v3.1.0 +[3.1.0]: https://github.com/evilmartians/evil-client/compare/v3.1.0...v3.2.0 \ No newline at end of file diff --git a/lib/evil/client/model.rb b/lib/evil/client/model.rb index aa2de0b..62160df 100644 --- a/lib/evil/client/model.rb +++ b/lib/evil/client/model.rb @@ -88,12 +88,12 @@ def extend(other) # Model instance constructor # - # @param [Hash] op Model options + # @param [Hash] options The list of options as a plain hash # @return [Evil::Client::Model] # - def new(**op) - op = Hash(op).transform_keys(&:to_sym) - super(**op).tap { |item| in_english { policy[item].validate! } } + def new(options = {}, **kwargs) + kwargs = Hash(options).transform_keys(&:to_sym).merge(kwargs) + super(**kwargs).tap { |item| in_english { policy[item].validate! } } end alias call new alias [] call diff --git a/spec/unit/model_spec.rb b/spec/unit/model_spec.rb index 2f25c27..5123939 100644 --- a/spec/unit/model_spec.rb +++ b/spec/unit/model_spec.rb @@ -108,18 +108,40 @@ class Test::Model < described_class subject { model } - it "behaves like a model" do - expect(subject).to be_a klass - expect(subject.email).to eq "joe@example.com" - end + context "with kwargs" do + let(:model) { klass.new(**options) } + + it "behaves like a model" do + expect(subject).to be_a klass + expect(subject.email).to eq "joe@example.com" + end - it "injects options from the other model" do - expect(subject.first_name).to eq "Joe" - expect(subject.last_name).to eq "Doe" + it "injects options from the other model" do + expect(subject.first_name).to eq "Joe" + expect(subject.last_name).to eq "Doe" + end + + it "injects memoizers from the other model" do + expect(subject.name).to eq "Joe Doe" + end end - it "injects memoizers from the other model" do - expect(subject.name).to eq "Joe Doe" + context "with hash argument" do + let(:model) { klass.new(options) } + + it "behaves like a model" do + expect(subject).to be_a klass + expect(subject.email).to eq "joe@example.com" + end + + it "injects options from the other model" do + expect(subject.first_name).to eq "Joe" + expect(subject.last_name).to eq "Doe" + end + + it "injects memoizers from the other model" do + expect(subject.name).to eq "Joe Doe" + end end context "with invalid options" do