Module: RubyCodeAPI::Manipulation::Insert
- Included in:
- RubyCodeAPI::Manipulation
- Defined in:
- lib/manipulation/insert/api.rb
Instance Method Summary collapse
- #append_code(code) ⇒ Object
- #append_code_simple(code) ⇒ Object
- #insert(position, code, &block) ⇒ Object
- #insert_comment(position, text, &block) ⇒ Object
- #prepend_code(code) ⇒ Object
Instance Method Details
#append_code(code) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/manipulation/insert/api.rb', line 4 def append_code(code) return append_code_simple(code) if !elemental? obj = object indentation = obj.last_indent code = "\n#{code}\n".indent(' ', indentation) ruby_code = Ripper::RubyBuilder.build(code) inject_code = ruby_code.elements[0] obj.get_elements << inject_code inject_code end |
#append_code_simple(code) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/manipulation/insert/api.rb', line 15 def append_code_simple(code) indentation = position.col code = "\n#{code}\n".indent(' ', indentation) ruby_code = Ripper::RubyBuilder.build(code) inject_code = ruby_code.elements[0] index = parent.find_index(self) parent.get_elements.insert(index+1, inject_code) inject_code end |
#insert(position, code, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/manipulation/insert/api.rb', line 39 def insert(position, code, &block) case position when :after s = append_code(code) when :before s = prepend_code(code) else raise Error, "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
35 36 37 |
# File 'lib/manipulation/insert/api.rb', line 35 def insert_comment(position, text, &block) insert(position, "# #{text}", &block) end |
#prepend_code(code) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/manipulation/insert/api.rb', line 25 def prepend_code(code) obj = object indentation = obj.first_indent code = "\n#{code}\n".indent(' ', indentation) ruby_code = Ripper::RubyBuilder.build(code) inject_code = ruby_code.elements[0] obj.get_elements.insert(0, inject_code) obj end |