Skip to content

Commit

Permalink
I think I've caught all the options this time. Fixes #92
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoertink committed Jun 15, 2024
1 parent e55a325 commit f55b1d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions spec/habitat_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,23 @@ end
class ASettingForEverything
alias Dot = Int32
Habitat.create do
setting proc_notation : (String -> Nil)?
setting proc_notation : (String -> Nil)
setting nilable_proc_notation : (String -> Nil)?
setting alias_setting : Dot = 3
setting nilable_alias_setting : Dot?
setting point : Point = Point.new(x: 3, y: 4)
setting nilable_point : Point?
setting mod : SuperMod
setting nilable_mod : SuperMod?
setting parent : Parent = Settings::Index.new
setting nilable_parent : Parent?
setting things : String | Int32
setting nilable_things : String | Int32 | Nil
setting bomb : Bomb
setting fruit : Fruit(Bomb)
setting nilable_bomb : Bomb?
setting generic : Fruit(Bomb)
setting nilable_generic : Fruit(Bomb)?
setting nilable_generic_of_nilable : Fruit(Bomb?)?
end
end

Expand All @@ -169,7 +178,7 @@ describe Habitat do
settings.mod = Fruit(String).new
settings.things = ["hi", 1].sample
settings.bomb = Tacos.new
settings.fruit = Fruit(Bomb).new
settings.generic = Fruit(Bomb).new
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/habitat.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Habitat
{% for type in TYPES_WITH_HABITAT %}
{% for setting in type.constant(:HABITAT_SETTINGS) %}
{% if !setting[:decl].type.is_a?(Union) ||
(setting[:decl].type.is_a?(Union) && !setting[:decl].type.types.any? { |t| t.is_a?(ProcNotation) ? false : t.names.includes?(Nil.id) }) %}
(setting[:decl].type.is_a?(Union) && !setting[:decl].type.types.any? { |t| (t.is_a?(ProcNotation) || t.is_a?(Generic)) ? false : t.names.includes?(Nil.id) }) %}
if {{ type }}.settings.{{ setting[:decl].var }}?.nil?
raise MissingSettingError.new {{ type }}, setting_name: {{ setting[:decl].var.stringify }}, example: {{ setting[:example] }}
end
Expand Down Expand Up @@ -231,7 +231,7 @@ class Habitat
# NOTE: We can't use the macro level `type.resolve.nilable?` here because
# there's a few declaration types that don't respond to it which would make the logic
# more complex. Metaclass, and Proc types are the main, but there may be more.
{% if decl.type.is_a?(Union) && decl.type.types.any? { |t| t.is_a?(ProcNotation) ? false : t.names.includes?(Nil.id) } %}
{% if decl.type.is_a?(Union) && decl.type.types.any? { |t| (t.is_a?(ProcNotation) || t.is_a?(Generic)) ? false : t.names.includes?(Nil.id) } %}
{% nilable = true %}
{% else %}
{% nilable = false %}
Expand Down

0 comments on commit f55b1d4

Please sign in to comment.