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

Add predicate methods (#89) #97

Merged
merged 4 commits into from
Jul 6, 2021
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions lib/store_model/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -110,5 +113,14 @@ def _has_attribute?(attr_name)
def unknown_attributes
@unknown_attributes ||= {}
end

private

def attribute?(attribute)
case value = attributes[attribute]
when 0 then false
else value.present?
end
end
end
end
7 changes: 7 additions & 0 deletions spec/dummy/app/models/bicycle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Bicycle
include StoreModel::Model

attribute :gears, :integer
end
72 changes: 72 additions & 0 deletions spec/store_model/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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