Skip to content

Commit

Permalink
Add after init callback (#89)
Browse files Browse the repository at this point in the history
* Add after init callback

* Test the after_initialize callback
  • Loading branch information
joeldrapper authored Jun 24, 2024
1 parent dc7abc5 commit 1da9d9f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/literal/properties/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def generate_initializer_body
[
"@literal_properties = self.class.literal_properties",
generate_initializer_handle_properties(@sorted_properties),
"after_initialize if respond_to?(:after_initialize)",
].join("\n")
end

Expand Down
18 changes: 18 additions & 0 deletions test/properties.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,21 @@ class WithNilableType
prop_name = props.first
assert(prop_name.optional?) { "Expected name to be optional" }
end

test "after initialize callback" do
callback_called = false

example = Class.new do
extend Literal::Properties

prop :name, String

define_method :after_initialize do
callback_called = true
end
end

example.new(name: "John")

assert callback_called
end

0 comments on commit 1da9d9f

Please sign in to comment.