Skip to content

Commit

Permalink
don't inherit settings from classes with generics (#34)
Browse files Browse the repository at this point in the history
* don't inherit from settings from classes with generics

fixes #29

* add spec for settings in generics

* fix syntax error after rebase
  • Loading branch information
stakach authored and paulcsmith committed Mar 9, 2019
1 parent 8cc4005 commit 1e06048
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions spec/habitat_spec.cr
Expand Up @@ -23,6 +23,12 @@ class FakeServer
end
end

class Generics < Hash(String, String)
Habitat.create do
setting should_work : String = "with generics"
end
end

class Parent
Habitat.create do
setting parent_setting : Bool = true
Expand Down Expand Up @@ -98,6 +104,10 @@ describe Habitat do
FakeServer.configure { |settings| settings.nilable_with_default = nil }
FakeServer.settings.nilable_with_default.should be_nil
end

it "works with generics" do
Generics.settings.should_work.should eq "with generics"
end

it "can set and reset config using a block" do
setup_server(port: 3000)
Expand Down
4 changes: 2 additions & 2 deletions src/habitat.cr
Expand Up @@ -164,7 +164,7 @@ class Habitat
end

macro inherit_habitat_settings_from_superclass
{% if @type.superclass && @type.superclass.constant(:HABITAT_SETTINGS) %}
{% if @type.superclass && @type.superclass.type_vars.size == 0 && @type.superclass.constant(:HABITAT_SETTINGS) %}
{% for decl in @type.superclass.constant(:HABITAT_SETTINGS) %}
{% HABITAT_SETTINGS << decl %}
{% end %}
Expand All @@ -177,7 +177,7 @@ class Habitat
{% type_with_habitat = type_with_habitat.resolve %}

class Settings
{% if type_with_habitat.superclass && type_with_habitat.superclass.constant(:HABITAT_SETTINGS) %}
{% if type_with_habitat.superclass && type_with_habitat.superclass.type_vars.size == 0 && type_with_habitat.superclass.constant(:HABITAT_SETTINGS) %}
{% for decl in type_with_habitat.superclass.constant(:HABITAT_SETTINGS).map { |setting| setting[:decl] } %}
def self.{{ decl.var }}
::{{ type_with_habitat.superclass }}::Settings.{{ decl.var }}
Expand Down

0 comments on commit 1e06048

Please sign in to comment.