Module: RubyCodeAPI::Manipulation::Insert

Included in:
RubyCodeAPI::Manipulation
Defined in:
lib/manipulation/insert/api.rb

Defined Under Namespace

Classes: InsertError

Instance Method Summary collapse

Instance Method Details

#insert(position, code, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/manipulation/insert/api.rb', line 11

def insert(position, code, &block)
  case position
  when :after
    s = append_code(code)
  when :before
    s = prepend_code(code)
  else
    raise InsertError, "Invalid position given: #{position}, must be either :before or :after"
  end  
    
  if block_given?
    block.arity < 1 ? s.instance_eval(&block) : block.call(s)      
  else
    s
  end    
end

#insert_comment(position, text, &block) ⇒ Object



7
8
9
# File 'lib/manipulation/insert/api.rb', line 7

def insert_comment(position, text, &block)
  insert(position, "# #{text}", &block)    
end