Skip to content

Commit

Permalink
allow templates to directly reference scoped context with method_miss…
Browse files Browse the repository at this point in the history
…ing (lets skip assigning a var when asking questions)
  • Loading branch information
wr0ngway committed Sep 11, 2018
1 parent 7257229 commit 8cf203e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/simplygenius/atmos/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ def lookup_context(varname)
def track_context(varname, value)
varname.blank? || value.nil? ? nil: tmpl.scoped_context[varname] = value
end

def respond_to_missing?(method_name, *args)
scoped_context.respond_to_missing?(method_name, *args)
end

def method_missing(method_name, *args, &blk)
scoped_context.method_missing(method_name, *args, &blk)
end

end

desc "ask <question string> [varname: name]", "Asks a question, allowing context to provide answer using varname"
Expand Down
15 changes: 15 additions & 0 deletions spec/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,21 @@ def with_sourcepaths
end
end

it "uses context for varname lookups" do
with_sourcepaths do |sp_dir, app_dir|
tmpl = SourcePath.find_template('template1')
tmpl.scoped_context.merge!({foo: "answer"})
thor = gen.apply_template(tmpl)

result = nil
expect { simulate_stdin("other") { result = thor.ask("question ", varname: :askfoo) } }.to output("question ").to_stdout
expect(result).to eq("other")
expect(tmpl.scoped_context[:askfoo]).to eq("other")
expect(thor.askfoo).to eq("other")
expect(thor.foo).to eq("answer")
end
end

end

describe "raw_configs" do
Expand Down

0 comments on commit 8cf203e

Please sign in to comment.