-
Notifications
You must be signed in to change notification settings - Fork 7
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
template
accessor inside tools
#50
Comments
I agree, this is a bit awkward. The challenge, though, is that the scope of a template is lexical (that is, they generate code), whereas the scope of a getter in a tool is dynamic (that is, it exists during a tool's execution). As a result, there isn't a clean mapping from one to the other, so it's not easy to decide which template a getter should return. For example, suppose we wanted to define a getter called class Template1
include Toys::Template
to_expand do
def template1_class
current_template.class
end
end
end
class Template2
include Toys::Template
to_expand do
def template2_class
current_template.class
end
end
end
tool "foo" do
expand Template1
expand Template2
def run
# What would we expect to see here?
puts template1_class
puts template2_class
puts current_template.class
end
end So while I agree that I wish there was a simpler way to pass information into a template, it's not clear to me what that should look like. If you want to avoid using class MyTemplate
include Toys::Template
def initialize(foo: nil, bar: nil)
@foo = foo
@bar = bar
end
attr_reader :foo, :bar
to_expand do |template|
tool "display-my-template" do
# Create a static context value with a getter method
static :my_template, template
def run
# Now you can use it here.
puts "foo is #{my_template.foo}"
puts "bar is #{my_template.bar}"
end
end
end
end I know your next question is, can you do that inside class LeakyTemplate
include Toys::Template
def initialize(foo: nil, bar: nil)
@foo = foo
@bar = bar
end
attr_reader :foo, :bar
to_expand do |template|
subtool_apply do
# Create this getter method for all tools
static :the_template, template
end
tool "a-generated-tool" do
def run
puts "foo is #{the_template.foo}"
end
end
tool "another-generated-tool" do
def run
puts "bar is #{the_template.bar}"
end
end
end
end
tool "namespace" do
# this defines "a-generated-tool" and "another-generated-tool"
expand LeakyTemplate, foo: 1, bar: 2
tool "unrelated-tool" do
# Because LeakyTemplate used subtool_apply, the getter
# method is added to this subtool also.
def run
puts "leaked foo: #{the_template.foo}"
end
end
end |
Some unified template. Hm… Maybe these things (templates) should be more like
OK, thank you, I'll try. |
Hello.
I use
Toys::Template
. It's a good way to create gems with a set of toys tools which can be expanded.But it's hard to configure: you have to receive options (of
expand
) inTemplate#initialize
, then you have to define tools insideTemplate.on_expand
block, and if you want to access options fromTemplate#initialize
— you have to accesstemplate
argument ofon_expand
block and pass it to tools code. Also, you have to writeto_run do
instead ofdef run
for accessing local variable. And there is many problems andtemplate
variable passing through methods if you split code to modules, files, like separate tools or a module for common code between tools.#46 can help a bit, but there is another idea and a way to simplify a code:
Instead of receiving
template
argument insideon_expand
block make it available through getter inside tools.Some work-around via instance variable you can see here: https://github.com/AlexWayfer/gem_toys/blob/715dd1e/lib/gem_toys/template.rb
Do I explain the subject clearly? What do you think about this?
The text was updated successfully, but these errors were encountered: