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

[Breaking change] generate getters for needs in pages #1034

Merged
merged 2 commits into from
Mar 7, 2020
Merged
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
12 changes: 9 additions & 3 deletions spec/lucky/assignable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ end

class PageWithQuestionMark
include Lucky::HTMLPage
needs signed_in? : Bool
needs signed_in : Bool

def render
text @signed_in.to_s
text signed_in?.to_s
end
end

Expand All @@ -61,15 +61,21 @@ end
class PageWithMetaclass
include Lucky::HTMLPage
needs string_class : String.class
needs access_me_with_a_getter : String = "called from an auto-generated getter"

def render
text access_me_with_a_getter
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"
PageTwo.new build_context, title: "foo", name: "Paul"
PageThree.new build_context, name: "Paul", admin_name: "Pablo", title: "Admin"
PageWithQuestionMark.new(build_context, signed_in?: true).perform_render.to_s.should contain("true")
PageWithQuestionMark.new(build_context, signed_in: true).perform_render.to_s.should contain("true")
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")
end
end
30 changes: 28 additions & 2 deletions src/lucky/assignable.cr
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
# :nodoc:
module Lucky::Assignable
# Declare what a page needs in order to be initialized.
#
# This will declare an instance variable and getter automatically. It will
# also add arguments to an `initialize` method at the end of compilation.
#
# ### Examples
#
# ```crystal
# class Users::IndexPage < MainLayout
# # This page needs a `User` or it will fail to compile
# # You can access it with `@user` or the getter method `user`
# needs user : User
#
# # This page can take an optional `ProductQuery`. This means you can
# # Leave `products` off when rendering from an Action.
# needs products : ProductQuery?
#
# # When using a `Bool` Lucky will generate a method ending with `?`
# # So in this case you can call `should_show_sidebar?` in the page.
# needs should_show_sidebar : Bool = true
# end
# ```
macro needs(*type_declarations)
{% for declaration in type_declarations %}
{% unless declaration.is_a?(TypeDeclaration) %}
{% raise "needs expected a type declaration like 'name : String', instead got: '#{declaration}'" %}
{% raise "'needs' expects a type declaration like 'name : String', instead got: '#{declaration}'" %}
{% end %}
{% 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 %}
{% ASSIGNS << declaration %}
{% end %}
end

# :nodoc:
macro included
SETTINGS = {} of Nil => Nil
ASSIGNS = [] of Nil
Expand All @@ -22,6 +47,7 @@ module Lucky::Assignable
end
end

# :nodoc:
macro inherit_page_settings
SETTINGS = {} of Nil => Nil
ASSIGNS = [] of Nil
Expand Down
15 changes: 14 additions & 1 deletion src/lucky/html_builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Lucky::HTMLBuilder
macro setup_initializer_hook
macro finished
generate_needy_initializer
generate_getters
end

macro included
Expand Down Expand Up @@ -61,14 +62,26 @@ module Lucky::HTMLBuilder
{% value = declaration.value %}
{% value = nil if type.stringify.ends_with?("Nil") && !value %}
{% has_default = value || value == false || value == nil %}
{% if false || var.stringify.ends_with?("?") %}{{ var }}{% end %} @{{ var.stringify.gsub(/\?/, "").id }} : {{ type }}{% if has_default %} = {{ value }}{% end %},
@{{ var.id }} : {{ type }}{% if has_default %} = {{ value }}{% end %},
{% end %}
**unused_exposures
)
end
{% 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