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

Allow defaults with lambdas of arity 0 to be overriden #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/micro/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def __attributes
value_to_assign =
if default.is_a?(Proc) && !keep_proc
case default.arity
when 0 then default.call
when 0 then value.nil? ? default.call : value
when 2 then default.call(value, init_hash)
else default.call(value)
end
Expand Down
19 changes: 13 additions & 6 deletions test/micro/attributes/defaults_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,24 @@ def test_defaults_defined_via_the_attributes_method
class ArityZero
include Micro::Attributes.with(:initialize)

attribute :a, default: -> { Time.now }
attributes :b, :c, default: -> { Time.now + 1 }
attribute :a, default: -> { 0 }
attributes :b, :c, default: -> { 1 }
end

def test_default_receiving_a_lambda_with_0_as_its_arity
attributes = ArityZero.new({})

assert attributes.b > attributes.a
assert attributes.c > attributes.a
assert attributes.b != attributes.c
assert attributes.b.strftime('%H:%M:%S') == attributes.c.strftime('%H:%M:%S')
assert_equal(0, attributes.a)
assert_equal(1, attributes.b)
assert_equal(1, attributes.c)
end

def test_overriding_default_receiving_a_lambda_with_0_as_its_arity
attributes = ArityZero.new(a: 1, b: 2, c: 3)

assert_equal(1, attributes.a)
assert_equal(2, attributes.b)
assert_equal(3, attributes.c)
end

class ArityOne
Expand Down