Skip to content

Commit

Permalink
Implement set_default macro
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Oct 28, 2023
1 parent ccd96ee commit 8b5bf55
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/kangaru/concerns/attributes_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def attributes
end
end

def set_default(**attributes)
defaults.merge!(**attributes)
end

def defaults
@defaults ||= {}
end
Expand Down
2 changes: 2 additions & 0 deletions sig/kangaru/concerns/attributes_concern.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Kangaru

def defaults: -> Hash[Symbol, untyped]

def set_default: (**untyped) -> void

@defaults: Hash[Symbol, untyped]
end

Expand Down
12 changes: 12 additions & 0 deletions spec/kangaru/concerns/attributes_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,16 @@
end
end
end

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

let(:attributes) { { bar: "bar", baz: "baz" } }

it "sets the default attributes" do
expect { set_default }
.to change { model_class.defaults }
.to(include(**attributes))
end
end
end

0 comments on commit 8b5bf55

Please sign in to comment.