Module: Ritsu::TemplatePolicies::FlexibleBlockMatchingAndCreateMissingBlockButLeaveUserTextBe

Includes:
FlexibleBlockMatching
Included in:
SrcFiles::TargetCmakeLists::Template
Defined in:
lib/ritsu/template_policies.rb

Instance Method Summary collapse

Methods included from FlexibleBlockMatching

#match_child_blocks

Instance Method Details

#position_to_insert(block, new_block) ⇒ Integer

Returns the position in block.contents to insert the given new block.

Returns:

  • (Integer)

    the position in block.contents to insert the given new block

Raises:

  • (NotImplementedError)


129
130
131
# File 'lib/ritsu/template_policies.rb', line 129

def position_to_insert(block, new_block)
  raise NotImplementedError
end

#update_block(block, options = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ritsu/template_policies.rb', line 102

def update_block(block, options={})
  options = {:new_line_after_block => true}.merge(options)
  matching_child_blocks = match_child_blocks(block)
  
  child_templates.each do |child_template|
    if matching_child_blocks[child_template].nil?
      new_block = child_template.create_block(options)
      
      position = position_to_insert(block, new_block)
      if position
        block.contents.insert(position, new_block)
        if options[:new_line_after_block]
          block.contents.insert(position+1, "")
        end
      else
        raise ArgumentError.new("cannot find position to insert '#{new_block.name}'")
      end
    else
      matching = matching_child_blocks[child_template]
      child_template.update_block(matching)
    end
  end
end