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

source: allow optionally specifying an extension for looking up source files #17

Merged
merged 1 commit into from
Apr 11, 2016

Conversation

kellyredding
Copy link
Member

This helps when you have two files names the same thing but with
different extensions. Previously, only the first file globbed would
be rendered. This allows you to prefer rendering one over the other.

This scenario should have been covered in PR 16 but was missed.

Note: this also changes to have Nm complain if asked to render a named
template that doesn't exist (whether specifying an extension or not).
This makes for more friendly errors in this scenario.

See #16 for reference.

@jcredding ready for review.

@kellyredding kellyredding force-pushed the kr-opt-preferred-ext branch 3 times, most recently from 8e0c34a to bb55eab Compare April 8, 2016 19:20
jcredding added a commit to redding/sanford that referenced this pull request Apr 8, 2016
This updates template sources to pass the extension that was used
to register an engine to the engine as an option. The goal is to
allow the engine to choose templates that match the extension that
was used when configuring the extension. This is also part of
fixing an issue with the engines not knowing which template file
to pick because they no longer hard-code an extension that the
templates have to use.

The template source now always sets an ext option and passes it
to the engines when they are registered. The ext will always be
set and can't be different from how the engine is registered. It
would be non-sensical to register with one engine ext and pass a
different engine ext as an option. Sanford would either not use
the registered engine to render the template or the template
engine wouldn't find the file. Either case wouldn't produce the
desired result.

Specifically, this is part of fixing an issue with the nm template
engine no longer forcing templates extension to be `nm` (see
redding/nm#16). By not expecting an extension, the nm engine can't
pick the correct file as the template if there are multiple files
with the same name except for their extensions. For example, a
common pattern is to name a handler file and the template the same
only the handler will have `.rb` for its extension and the template
would use `.nm`. In this scenario, it's possible for the nm engine
to try and use the ruby file as its template. Nm is being updated
to use a custom extension (the one sanford will now pass to the
engine) to address this issue (redding/nm#17).

Finally, this also renames "path" variables to "name" in the
template source and engine. The variables are expected to be names
and not full paths. This doesn't change any functionality it just
clarifies the variable names.
@jcredding
Copy link
Member

@kellyredding - Looks good 💥

jcredding added a commit to redding/sanford that referenced this pull request Apr 8, 2016
This updates template sources to pass the extension that was used
to register an engine to the engine as an option. The goal is to
allow the engine to choose templates that match the extension that
was used when configuring the extension. This is also part of
fixing an issue with the engines not knowing which template file
to pick because they no longer hard-code an extension that the
templates have to use.

The template source now always sets an ext option and passes it
to the engines when they are registered. The ext will always be
set and can't be different from how the engine is registered. It
would be non-sensical to register with one engine ext and pass a
different engine ext as an option. Sanford would either not use
the registered engine to render the template or the template
engine wouldn't find the file. Either case wouldn't produce the
desired result.

Specifically, this is part of fixing an issue with the nm template
engine no longer forcing templates extension to be `nm` (see
redding/nm#16). By not expecting an extension, the nm engine can't
pick the correct file as the template if there are multiple files
with the same name except for their extensions. For example, a
common pattern is to name a handler file and the template the same
only the handler will have `.rb` for its extension and the template
would use `.nm`. In this scenario, it's possible for the nm engine
to try and use the ruby file as its template. Nm is being updated
to use a custom extension (the one sanford will now pass to the
engine) to address this issue (redding/nm#17).

Finally, this also renames "path" variables to "name" in the
template source and engine. The variables are expected to be names
and not full paths. This doesn't change any functionality it just
clarifies the variable names.
jcredding added a commit to redding/sanford-nm that referenced this pull request Apr 8, 2016
This updates the template engine to pass its ext to nm source. The
template engine ext is set by sanford when the engine is
registered. This makes it so nm will use templates with the
extension that was used when registering the engine with sanford.
This is the expected behavior and avoids confusion with nm randomly
not using a template that matches the registered extension.

This is part of fixing an issue with sanford not rendering the
expected nm template. This is because the nm engine no longer
forced templates extension to be `nm` (see redding/nm#16). By not
expecting an extension, the nm engine can't pick the correct file
as the template if there are multiple files with the same name
except for their extensions. For example, a common pattern is to
name a handler file and the template the same only the handler will
have `.rb` for its extension and the template would use `.nm`. In
this scenario, it's possible for the nm engine to try and use the
ruby file as its template. Nm is being updated to use a custom
extension (the one sanford-nm will now pass to it) to address this
issue (redding/nm#17).
jcredding added a commit to redding/deas-nm that referenced this pull request Apr 8, 2016
This updates the template engine to pass its ext to nm source. The
template engine ext is set by deas when the engine is registered.
This makes it so nm will use templates with the extension that was
used when registering the engine with deas. This is the expected
behavior and avoids confusion with nm randomly not using a template
that matches the registered extension.

This is part of fixing an issue with deas not rendering the
expected nm template. This is because the nm engine no longer
forced templates extension to be `nm` (see redding/nm#16). By not
expecting an extension, the nm engine can't pick the correct file
as the template if there are multiple files with the same name
except for their extensions. For example, a common pattern is to
name a handler file and the template the same only the handler will
have `.rb` for its extension and the template would use `.nm`. In
this scenario, it's possible for the nm engine to try and use the
ruby file as its template. Nm is being updated to use a custom
extension (the one deas-nm will now pass to it) to address this
issue (redding/nm#17).
jcredding added a commit to redding/sanford-nm that referenced this pull request Apr 8, 2016
This updates the template engine to pass its ext to nm source. The
template engine ext is set by sanford when the engine is
registered. This makes it so nm will use templates with the
extension that was used when registering the engine with sanford.
This is the expected behavior and avoids confusion with nm randomly
not using a template that matches the registered extension.

This is part of fixing an issue with sanford not rendering the
expected nm template. This is because the nm engine no longer
forced templates extension to be `nm` (see redding/nm#16). By not
expecting an extension, the nm engine can't pick the correct file
as the template if there are multiple files with the same name
except for their extensions. For example, a common pattern is to
name a handler file and the template the same only the handler will
have `.rb` for its extension and the template would use `.nm`. In
this scenario, it's possible for the nm engine to try and use the
ruby file as its template. Nm is being updated to use a custom
extension (the one sanford-nm will now pass to it) to address this
issue (redding/nm#17).
jcredding added a commit to redding/deas-nm that referenced this pull request Apr 8, 2016
This updates the template engine to pass its ext to nm source. The
template engine ext is set by deas when the engine is registered.
This makes it so nm will use templates with the extension that was
used when registering the engine with deas. This is the expected
behavior and avoids confusion with nm randomly not using a template
that matches the registered extension.

This is part of fixing an issue with deas not rendering the
expected nm template. This is because the nm engine no longer
forced templates extension to be `nm` (see redding/nm#16). By not
expecting an extension, the nm engine can't pick the correct file
as the template if there are multiple files with the same name
except for their extensions. For example, a common pattern is to
name a handler file and the template the same only the handler will
have `.rb` for its extension and the template would use `.nm`. In
this scenario, it's possible for the nm engine to try and use the
ruby file as its template. Nm is being updated to use a custom
extension (the one deas-nm will now pass to it) to address this
issue (redding/nm#17).
jcredding added a commit to redding/deas-nm that referenced this pull request Apr 8, 2016
This updates the template engine to pass its ext to nm source. The
template engine ext is set by deas when the engine is registered.
This makes it so nm will use templates with the extension that was
used when registering the engine with deas. This is the expected
behavior and avoids confusion with nm randomly not using a template
that matches the registered extension.

This is part of fixing an issue with deas not rendering the
expected nm template. This is because the nm engine no longer
forced templates extension to be `nm` (see redding/nm#16). By not
expecting an extension, the nm engine can't pick the correct file
as the template if there are multiple files with the same name
except for their extensions. For example, a common pattern is to
name a handler file and the template the same only the handler will
have `.rb` for its extension and the template would use `.nm`. In
this scenario, it's possible for the nm engine to try and use the
ruby file as its template. Nm is being updated to use a custom
extension (the one deas-nm will now pass to it) to address this
issue (redding/nm#17).
jcredding added a commit to redding/sanford-nm that referenced this pull request Apr 8, 2016
This updates the template engine to pass its ext to nm source. The
template engine ext is set by sanford when the engine is
registered. This makes it so nm will use templates with the
extension that was used when registering the engine with sanford.
This is the expected behavior and avoids confusion with nm randomly
not using a template that matches the registered extension.

This is part of fixing an issue with sanford not rendering the
expected nm template. This is because the nm engine no longer
forced templates extension to be `nm` (see redding/nm#16). By not
expecting an extension, the nm engine can't pick the correct file
as the template if there are multiple files with the same name
except for their extensions. For example, a common pattern is to
name a handler file and the template the same only the handler will
have `.rb` for its extension and the template would use `.nm`. In
this scenario, it's possible for the nm engine to try and use the
ruby file as its template. Nm is being updated to use a custom
extension (the one sanford-nm will now pass to it) to address this
issue (redding/nm#17).
@kellyredding
Copy link
Member Author

@jcredding I've reworked this to have the source expect its files to end with any optional extension if one is given and to also be aware if the given template name already ends in the extension.

I'll make comments on what I've changed since you've looked at this and you can tell me what you think. Thanks man.

def source_file_path(template_name)
Dir.glob(self.root.join("#{template_name}*")).first
def source_file_path(name)
Dir.glob(self.root.join(name.end_with?(@ext) ? name : "#{name}*#{@ext}")).first
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcredding Here is where I make the glob expect the the template to end with the ext if one is present. Notice I default the ext to nil above. This allows me to not have to check whether an ext is present of not. If no extension is present, end_with? is false the the string interpolations means the glob ends up being name*.

Does this work for you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kellyredding - Yeah this looks like what we discussed. I didn't know end_with? was a thing. Nice.

template_desc += " with #{@ext.inspect} as an extension"
end
raise ArgumentError, "#{template_desc} does not exist"
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcredding here I add checking that a filename exists and raising a friendly argument error if not. I made the template description aware of any specified ext and if the given template name already ended with it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kellyredding - I'm good with the better error here.

@jcredding
Copy link
Member

@kellyredding - I'm good with the changes. Thanks for working through what we discussed 💥

…e files

This helps when you have two files names the same thing but with
different extensions.  Previously, only the first file globbed would
be rendered.  This allows you to prefer rendering one over the other.

This scenario should have been covered in PR 16 but was missed.

Note: this also changes to have Nm complain if asked to render a named
template that doesn't exist (whether specifying an extension or not).
This makes for more friendly errors in this scenario.
jcredding added a commit to redding/sanford-nm that referenced this pull request Apr 11, 2016
This updates the template engine to pass its ext to nm source. The
template engine ext is set by sanford when the engine is
registered. This makes it so nm will use templates with the
extension that was used when registering the engine with sanford.
This is the expected behavior and avoids confusion with nm randomly
not using a template that matches the registered extension.

This is part of fixing an issue with sanford not rendering the
expected nm template. This is because the nm engine no longer
forced templates extension to be `nm` (see redding/nm#16). By not
expecting an extension, the nm engine can't pick the correct file
as the template if there are multiple files with the same name
except for their extensions. For example, a common pattern is to
name a handler file and the template the same only the handler will
have `.rb` for its extension and the template would use `.nm`. In
this scenario, it's possible for the nm engine to try and use the
ruby file as its template. Nm is being updated to use a custom
extension (the one sanford-nm will now pass to it) to address this
issue (redding/nm#17).
jcredding added a commit to redding/deas-nm that referenced this pull request Apr 11, 2016
This updates the template engine to pass its ext to nm source. The
template engine ext is set by deas when the engine is registered.
This makes it so nm will use templates with the extension that was
used when registering the engine with deas. This is the expected
behavior and avoids confusion with nm randomly not using a template
that matches the registered extension.

This is part of fixing an issue with deas not rendering the
expected nm template. This is because the nm engine no longer
forced templates extension to be `nm` (see redding/nm#16). By not
expecting an extension, the nm engine can't pick the correct file
as the template if there are multiple files with the same name
except for their extensions. For example, a common pattern is to
name a handler file and the template the same only the handler will
have `.rb` for its extension and the template would use `.nm`. In
this scenario, it's possible for the nm engine to try and use the
ruby file as its template. Nm is being updated to use a custom
extension (the one deas-nm will now pass to it) to address this
issue (redding/nm#17).
jcredding added a commit to redding/sanford-nm that referenced this pull request Apr 11, 2016
This updates the template engine to pass its ext to nm source. The
template engine ext is set by sanford when the engine is
registered. This makes it so nm will use templates with the
extension that was used when registering the engine with sanford.
This is the expected behavior and avoids confusion with nm randomly
not using a template that matches the registered extension.

This is part of fixing an issue with sanford not rendering the
expected nm template. This is because the nm engine no longer
forced templates extension to be `nm` (see redding/nm#16). By not
expecting an extension, the nm engine can't pick the correct file
as the template if there are multiple files with the same name
except for their extensions. For example, a common pattern is to
name a handler file and the template the same only the handler will
have `.rb` for its extension and the template would use `.nm`. In
this scenario, it's possible for the nm engine to try and use the
ruby file as its template. Nm is being updated to use a custom
extension (the one sanford-nm will now pass to it) to address this
issue (redding/nm#17).
@kellyredding kellyredding merged commit 1f6d760 into master Apr 11, 2016
@kellyredding kellyredding deleted the kr-opt-preferred-ext branch April 11, 2016 15:22
kellyredding added a commit that referenced this pull request Apr 11, 2016
* source: allow optionally specifying an extension for looking up source files #17
jcredding added a commit to redding/sanford-nm that referenced this pull request Apr 11, 2016
This updates the template engine to pass its ext to nm source. The
template engine ext is set by sanford when the engine is
registered. This makes it so nm will use templates with the
extension that was used when registering the engine with sanford.
This is the expected behavior and avoids confusion with nm randomly
not using a template that matches the registered extension.

This is part of fixing an issue with sanford not rendering the
expected nm template. This is because the nm engine no longer
forced templates extension to be `nm` (see redding/nm#16). By not
expecting an extension, the nm engine can't pick the correct file
as the template if there are multiple files with the same name
except for their extensions. For example, a common pattern is to
name a handler file and the template the same only the handler will
have `.rb` for its extension and the template would use `.nm`. In
this scenario, it's possible for the nm engine to try and use the
ruby file as its template. Nm is being updated to use a custom
extension (the one sanford-nm will now pass to it) to address this
issue (redding/nm#17).
jcredding added a commit to redding/deas-nm that referenced this pull request Apr 11, 2016
This updates the template engine to pass its ext to nm source. The
template engine ext is set by deas when the engine is registered.
This makes it so nm will use templates with the extension that was
used when registering the engine with deas. This is the expected
behavior and avoids confusion with nm randomly not using a template
that matches the registered extension.

This is part of fixing an issue with deas not rendering the
expected nm template. This is because the nm engine no longer
forced templates extension to be `nm` (see redding/nm#16). By not
expecting an extension, the nm engine can't pick the correct file
as the template if there are multiple files with the same name
except for their extensions. For example, a common pattern is to
name a handler file and the template the same only the handler will
have `.rb` for its extension and the template would use `.nm`. In
this scenario, it's possible for the nm engine to try and use the
ruby file as its template. Nm is being updated to use a custom
extension (the one deas-nm will now pass to it) to address this
issue (redding/nm#17).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants