Class: Lookbook::CodeIndenter

Inherits:
Service
  • Object
show all
Defined in:
lib/lookbook/services/code/code_indenter.rb

Instance Method Summary collapse

Methods inherited from Service

call

Constructor Details

#initialize(source) ⇒ CodeIndenter

Returns a new instance of CodeIndenter.



3
4
5
# File 'lib/lookbook/services/code/code_indenter.rb', line 3

def initialize(source)
  @source = source.to_s
end

Instance Method Details

#callObject



7
8
9
10
11
12
# File 'lib/lookbook/services/code/code_indenter.rb', line 7

def call
  source = @source.chomp
  last = source.split(/\r?\n/).last
  indent = last ? last[/^([ \t]*)/, 1].length : 0
  source.gsub(/^[ \t]{#{indent}}/, "")
end