Class: Molder::Template

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, name:, indexes:, attributes: {}, command:) ⇒ Template

Returns a new instance of Template.



6
7
8
9
10
11
12
# File 'lib/molder/template.rb', line 6

def initialize(config:, name:, indexes:, attributes: {}, command:)
  self.config     = config
  self.name       = name
  self.indexes    = indexes
  self.command    = command
  self.attributes = self.class.normalize(attributes)
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/molder/template.rb', line 4

def attributes
  @attributes
end

#commandObject

Returns the value of attribute command.



4
5
6
# File 'lib/molder/template.rb', line 4

def command
  @command
end

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/molder/template.rb', line 4

def config
  @config
end

#indexesObject

Returns the value of attribute indexes.



4
5
6
# File 'lib/molder/template.rb', line 4

def indexes
  @indexes
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/molder/template.rb', line 4

def name
  @name
end

Class Method Details

.normalize(attrs) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/molder/template.rb', line 24

def self.normalize(attrs)
  override = {}
  attrs.each_pair do |key, value|
    if value.is_a?(Hash) && value.values.compact.empty?
      override[key] = value.keys.to_a.join(',')
    end
  end
  attrs.merge!(override)
end

Instance Method Details

#each_commandObject



14
15
16
17
18
19
20
21
22
# File 'lib/molder/template.rb', line 14

def each_command
  indexes.map do |i|
    self.attributes[:number] = i
    self.attributes[:formatted_number] = sprintf(config.global.index_format, i)
    ::Molder::Renderer.new(command.args).render(attributes.dup).tap do |cmd|
      yield(cmd) if block_given?
    end
  end
end