Class: Dehenzify::ExtractedSource

Inherits:
Struct
  • Object
show all
Defined in:
lib/dehenzify/extracted_source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#namespace_modulesObject

Returns the value of attribute namespace_modules

Returns:

  • (Object)

    the current value of namespace_modules



2
3
4
# File 'lib/dehenzify/extracted_source.rb', line 2

def namespace_modules
  @namespace_modules
end

#nodeObject

Returns the value of attribute node.



4
5
6
# File 'lib/dehenzify/extracted_source.rb', line 4

def node
  @node
end

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



2
3
4
# File 'lib/dehenzify/extracted_source.rb', line 2

def source
  @source
end

#target_file_pathObject

Returns the value of attribute target_file_path

Returns:

  • (Object)

    the current value of target_file_path



2
3
4
# File 'lib/dehenzify/extracted_source.rb', line 2

def target_file_path
  @target_file_path
end

Instance Method Details

#delete!Object



38
39
40
# File 'lib/dehenzify/extracted_source.rb', line 38

def delete!
  File.delete(target_file_path) if exists?
end

#empty?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/dehenzify/extracted_source.rb', line 24

def empty?
  source.blank? || Dehenzify::Extractor.empty_node?(Parser::CurrentRuby.parse(source))
end

#exists?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/dehenzify/extracted_source.rb', line 28

def exists?
  File.exists?(target_file_path)
end

#namespaced_sourceObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dehenzify/extracted_source.rb', line 6

def namespaced_source
  raw_namespaced_source = source

  namespace_modules.reverse_each.with_index(1) do |module_name, index|
    space_offset = '  ' * (namespace_modules.size - index)

    modulized_source = ''
    modulized_source << "module #{module_name}" << "\n"
    modulized_source << space_offset << '  '
    modulized_source << raw_namespaced_source << "\n"
    modulized_source << space_offset << 'end'
    raw_namespaced_source = modulized_source
  end if namespace_modules

  raw_namespaced_source << "\n" unless raw_namespaced_source.end_with?("\n")
  raw_namespaced_source
end

#write!Object



32
33
34
35
36
# File 'lib/dehenzify/extracted_source.rb', line 32

def write!
  FileUtils.mkdir_p(File.dirname(target_file_path))
  File.write(target_file_path, namespaced_source)
  target_file_path
end