Class: Ali::Template
- Inherits:
-
Object
- Object
- Ali::Template
- Defined in:
- lib/ali/template.rb
Instance Attribute Summary collapse
-
#task ⇒ Object
readonly
Returns the value of attribute task.
Instance Method Summary collapse
-
#initialize(task) ⇒ Template
constructor
A new instance of Template.
- #liquid_path ⇒ Object
- #liquid_template ⇒ Object
- #render(liquid_template = self.liquid_template) ⇒ Object
- #render_source ⇒ Object
- #source_location ⇒ Object
- #task_path ⇒ Object
Constructor Details
#initialize(task) ⇒ Template
Returns a new instance of Template.
10 11 12 |
# File 'lib/ali/template.rb', line 10 def initialize(task) @task = task end |
Instance Attribute Details
#task ⇒ Object (readonly)
Returns the value of attribute task.
8 9 10 |
# File 'lib/ali/template.rb', line 8 def task @task end |
Instance Method Details
#liquid_path ⇒ Object
22 23 24 25 |
# File 'lib/ali/template.rb', line 22 def liquid_path path = File.join(task_path.dirname, "..", "templates", task_path.basename.sub_ext(".liquid")) Pathname File.(path) end |
#liquid_template ⇒ Object
27 28 29 30 |
# File 'lib/ali/template.rb', line 27 def liquid_template return nil unless liquid_path.file? Liquid::Template.parse File.read(liquid_path) end |
#render(liquid_template = self.liquid_template) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/ali/template.rb', line 32 def render(liquid_template = self.liquid_template) context = { tasks: Ali::Registry.instance.to_a.map(&:to_h), task: task, quote: Ali::Quotes.sample, version: Ali::VERSION } liquid_template&.render(context.stringify_keys)&.strip end |
#render_source ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/ali/template.rb', line 42 def render_source source = task.method(:perform).source indent = source.scan(/\s+/).first&.size.to_i formatter = Rouge::Formatters::Terminal256.new(theme: Rouge::Themes::Monokai.new) lexer = Rouge::Lexers::Ruby.new Rouge.highlight "\n#{"".ljust(indent, " ")}# #{task_path}:#{source_location.last}\n#{source}", lexer, formatter end |
#source_location ⇒ Object
14 15 16 |
# File 'lib/ali/template.rb', line 14 def source_location task.method(:perform).source_location end |
#task_path ⇒ Object
18 19 20 |
# File 'lib/ali/template.rb', line 18 def task_path Pathname.new(source_location.first) end |