Skip to content

Commit

Permalink
Create attributes concern
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Oct 1, 2023
1 parent fb119f1 commit af90f3f
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/kangaru/concerns/attributes_concern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Kangaru
module Concerns
module AttributesConcern
extend Concern

class_methods do
def attributes
instance_methods.grep(/\w=$/).map do |attribute|
attribute.to_s.delete_suffix("=").to_sym
end
end
end

def initialize(**attributes)
attributes.slice(*self.class.attributes).each do |attr, value|
instance_variable_set(:"@#{attr}", value)
end
end
end
end
end
17 changes: 17 additions & 0 deletions sig/kangaru/concerns/attributes_concern.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Kangaru
module Concerns
module AttributesConcern
extend Concern

module ClassMethods
def attributes: -> Array[Symbol]
end

extend ClassMethods

def instance_methods: -> Array[Symbol]

def initialize: (**untyped) -> void
end
end
end
97 changes: 97 additions & 0 deletions spec/kangaru/concerns/attributes_concern_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
RSpec.describe Kangaru::Concerns::AttributesConcern do
subject(:model) { model_class.new(**attributes) }

let(:model_class) do
Class.new { include Kangaru::Concerns::AttributesConcern }
end

describe "#initialize" do
context "when concern has not defined any attr_accessors" do
context "and no attributes are given" do
let(:attributes) { {} }

it "does not raise any errors" do
expect { model }.not_to raise_error
end

it "does not set any instance variables" do
expect { model }.not_to change { model_class.instance_variables }
end
end

context "and attributes are given" do
let(:attributes) { { foo: "foo", bar: "bar" } }

it "does not raise any errors" do
expect { model }.not_to raise_error
end

it "does not set the attributes" do
expect(model).not_to respond_to(*attributes.keys)
end
end
end

context "when concern has defined attr_accessors" do
let(:model_class) do
Class.new do
include Kangaru::Concerns::AttributesConcern

attr_accessor :foo, :bar, :baz
end
end

context "and no attributes are given" do
let(:attributes) { {} }

it "does not raise any errors" do
expect { model }.not_to raise_error
end
end

context "and attributes are given" do
let(:attributes) { { foo: "foo", bar: "bar" } }

it "does not raise any errors" do
expect { model }.not_to raise_error
end

it "sets the attributes" do
expect(model).to have_attributes(**attributes)
end
end
end
end

describe ".attributes" do
subject(:attributes) { model_class.attributes }

context "when no attr_accessors are set" do
it "does not raise any errors" do
expect { attributes }.not_to raise_error
end

it "returns an empty array" do
expect(attributes).to be_empty
end
end

context "when attr_accessors are set" do
let(:model_class) do
Class.new do
include Kangaru::Concerns::AttributesConcern

attr_accessor :foo, :bar, :baz
end
end

it "does not raise any errors" do
expect { attributes }.not_to raise_error
end

it "returns the expected attributes" do
expect(attributes).to contain_exactly(:foo, :bar, :baz)
end
end
end
end

0 comments on commit af90f3f

Please sign in to comment.