From 7c1b5a33cbf293d88a7b6ccf214cc9af7d52b668 Mon Sep 17 00:00:00 2001 From: Fabian Mersch Date: Fri, 2 Jul 2021 17:25:03 +0200 Subject: [PATCH 1/4] Add predicate methods (#89) --- lib/store_model/model.rb | 14 +++++++ spec/dummy/app/models/bicycle.rb | 7 ++++ spec/store_model/model_spec.rb | 72 ++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 spec/dummy/app/models/bicycle.rb diff --git a/lib/store_model/model.rb b/lib/store_model/model.rb index 7f51eaa..52afcbf 100644 --- a/lib/store_model/model.rb +++ b/lib/store_model/model.rb @@ -11,10 +11,13 @@ module Model def self.included(base) # :nodoc: base.include ActiveModel::Model base.include ActiveModel::Attributes + base.include ActiveModel::AttributeMethods base.include StoreModel::NestedAttributes base.extend StoreModel::Enum base.extend StoreModel::TypeBuilders + + base.attribute_method_suffix "?" end attr_accessor :parent @@ -110,5 +113,16 @@ def _has_attribute?(attr_name) def unknown_attributes @unknown_attributes ||= {} end + + private + + def attribute?(attribute) + case value = attributes[attribute] + when true then true + when false, nil, 0 then false + else + value.present? + end + end end end diff --git a/spec/dummy/app/models/bicycle.rb b/spec/dummy/app/models/bicycle.rb new file mode 100644 index 0000000..f67b8d9 --- /dev/null +++ b/spec/dummy/app/models/bicycle.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class Bicycle + include StoreModel::Model + + attribute :gears, :integer +end diff --git a/spec/store_model/model_spec.rb b/spec/store_model/model_spec.rb index d4a7c43..91487d9 100644 --- a/spec/store_model/model_spec.rb +++ b/spec/store_model/model_spec.rb @@ -233,4 +233,76 @@ it { is_expected.to be_truthy } end end + + describe "predicate method for string attribute" do + subject { Configuration.new(color: value).color? } + + context "when value is present" do + let(:value) { "red" } + + it { is_expected.to eq(true) } + end + + context "when value is nil" do + let(:value) { nil } + + it { is_expected.to eq(false) } + end + + context "when value is blank" do + let(:value) { "" } + + it { is_expected.to eq(false) } + end + + context "when value is \" \"" do + let(:value) { " " } + + it { is_expected.to eq(false) } + end + end + + describe "predicate method for number attribute" do + subject { Bicycle.new(gears: value).gears? } + + context "when value is 1" do + let(:value) { 1 } + + it { is_expected.to eq(true) } + end + + context "when value is nil" do + let(:value) { nil } + + it { is_expected.to eq(false) } + end + + context "when value is 0" do + let(:value) { 0 } + + it { is_expected.to eq(false) } + end + end + + describe "predicate method for boolean attribute" do + subject { Configuration.new(active: value).active? } + + context "when value is true" do + let(:value) { true } + + it { is_expected.to eq(true) } + end + + context "when value is nil" do + let(:value) { nil } + + it { is_expected.to eq(false) } + end + + context "when value is false" do + let(:value) { false } + + it { is_expected.to eq(false) } + end + end end From 816df93379a254e69204634c4733bc84f876639a Mon Sep 17 00:00:00 2001 From: Fabian Mersch Date: Mon, 5 Jul 2021 14:43:37 +0200 Subject: [PATCH 2/4] Apply stylistic suggestion Co-authored-by: Dmitry Tsepelev --- lib/store_model/model.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/store_model/model.rb b/lib/store_model/model.rb index 52afcbf..fbb4441 100644 --- a/lib/store_model/model.rb +++ b/lib/store_model/model.rb @@ -120,8 +120,7 @@ def attribute?(attribute) case value = attributes[attribute] when true then true when false, nil, 0 then false - else - value.present? + else value.present? end end end From 63b269ff83c9864616b6ed807f429f50cea561ba Mon Sep 17 00:00:00 2001 From: Fabian Mersch Date: Mon, 5 Jul 2021 16:39:42 +0200 Subject: [PATCH 3/4] Simplify Co-authored-by: Dmitry Tsepelev --- lib/store_model/model.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/store_model/model.rb b/lib/store_model/model.rb index fbb4441..e4b2057 100644 --- a/lib/store_model/model.rb +++ b/lib/store_model/model.rb @@ -118,8 +118,7 @@ def unknown_attributes def attribute?(attribute) case value = attributes[attribute] - when true then true - when false, nil, 0 then false + when 0 then false else value.present? end end From cc500c5f335a9bbc955444b11c43f6f4767ce4cb Mon Sep 17 00:00:00 2001 From: Fabian Mersch Date: Mon, 5 Jul 2021 16:45:59 +0200 Subject: [PATCH 4/4] Add changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1427c4c..a5e49b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## master +- [PR #97](https://github.com/DmitryTsepelev/store_model/pull/97) Add predicate methods ([@f-mer]) + ## 0.9.0 (2021-04-21) - [PR #93](https://github.com/DmitryTsepelev/store_model/pull/93) Handle aliases with has_attributes ([@Zooip] @@ -99,3 +101,4 @@ [@bostanio]: https://github.com/bostanio [@timhwang21]: https://github.com/timhwang21 [@Zooip]: https://github.com/Zooip +[@f-mer]: https://github.com/f-mer