Skip to content

Commit

Permalink
Generate getters right away
Browse files Browse the repository at this point in the history
We were generating getters at the very end, which meant it would
overwrite similarly named methods, which is not expected behavior.
  • Loading branch information
paulcsmith committed Mar 13, 2020
1 parent cb0527e commit d961739
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
14 changes: 14 additions & 0 deletions spec/lucky/assignable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ class PageWithMetaclass
end
end

class OverrideGetterPage
include Lucky::HTMLPage
needs name : String = "Oops! Not set"

def render
text name
end

def name
"Joe"
end
end

describe "Assigns within multiple pages with the same name" do
it "should only appear once in the initializer" do
PageOne.new build_context, title: "foo", name: "Paul", second: "second"
Expand All @@ -77,5 +90,6 @@ describe "Assigns within multiple pages with the same name" do
PageWithDefaultsFirst.new(build_context, required: "thing", title: "foo").perform_render.to_s.should contain("special foo")
PageWithMetaclass.new(build_context, string_class: String)
.perform_render.to_s.should contain("called from an auto-generated getter")
OverrideGetterPage.new(build_context).perform_render.to_s.should eq("Joe")
end
end
7 changes: 7 additions & 0 deletions src/lucky/assignable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ module Lucky::Assignable
{% if declaration.var.stringify.ends_with?("?") %}
{% raise "Using '?' in a 'needs' var name is no longer supported. Now Lucky generates a method ending in '?' if the type is 'Bool'." %}
{% end %}

{% if declaration.type.stringify == "Bool" %}
getter? {{ declaration }}
{% else %}
getter {{ declaration }}
{% end %}

{% ASSIGNS << declaration %}
{% end %}
end
Expand Down
13 changes: 0 additions & 13 deletions src/lucky/html_builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module Lucky::HTMLBuilder
macro setup_initializer_hook
macro finished
generate_needy_initializer
generate_getters
end

macro included
Expand Down Expand Up @@ -70,18 +69,6 @@ module Lucky::HTMLBuilder
{% end %}
end

macro generate_getters
{% if !@type.abstract? %}
{% for declaration in ASSIGNS %}
{% if declaration.type.stringify == "Bool" %}
getter? {{ declaration }}
{% else %}
getter {{ declaration }}
{% end %}
{% end %}
{% end %}
end

def perform_render : IO
render
view
Expand Down

0 comments on commit d961739

Please sign in to comment.