Skip to content

Commit

Permalink
Support hash argument in a model
Browse files Browse the repository at this point in the history
  • Loading branch information
nepalez committed Jan 18, 2023
1 parent 7461916 commit 948127f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
[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
8 changes: 4 additions & 4 deletions lib/evil/client/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 31 additions & 9 deletions spec/unit/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 948127f

Please sign in to comment.