Class: Ali::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/ali/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#taskObject (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_pathObject



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.expand_path(path)
end

#liquid_templateObject



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_sourceObject



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_locationObject



14
15
16
# File 'lib/ali/template.rb', line 14

def source_location
  task.method(:perform).source_location
end

#task_pathObject



18
19
20
# File 'lib/ali/template.rb', line 18

def task_path
  Pathname.new(source_location.first)
end