Class: DirectoryStructure

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/directory_structure.rb

Overview

Class to represent and process a directory structure. It can be initialized with a hash or a YAML file.

Using HASH

The following example shows how to use DirectoryStructure with a hash.

hash = {
  'lib' => {
    'version.rb' => nil,
    'source' => [
      'file_1.rb',
      'file_2.rb',
      'file_3.rb'
    ],
    'resource' => {
      'images' => [
        'potato.png',
        'fries.jpeg',
        'franch_fries.png'
      ],
      'scripts' => {
        validation: 'teste.rb',
        sorting: [
          'shellsort.rb',
          'quicksort.rb'
        ],
        'build.rb' => nil
      }
    },
    generated: 'file.generated.rb'
  }
}
directory_structure = DirectoryStructure.new(hash)
puts "That is my directory structure:\n#{directory_structure}"
# Output:
# That is my directory structure:
# lib
# ╠═ version.rb
# ╠═ source
# ║  ╠═ file_1.rb
# ║  ╠═ file_2.rb
# ║  ╚═ file_3.rb
# ╠═ resource
# ║  ╠═ images
# ║  ║  ╠═ potato.png
# ║  ║  ╠═ fries.jpeg
# ║  ║  ╚═ franch_fries.png
# ║  ╚═ scripts
# ║     ╠═ validation
# ║     ║  ╚═ teste.rb
# ║     ╠═ sorting
# ║     ║  ╠═ shellsort.rb
# ║     ║  ╚═ quicksort.rb
# ║     ╚═ build.rb
# ╚═ generated
#    ╚═ file.generated.rb

Using a YAML/YML File

First, you need to create a yml file.

Example:

lib:
  version.rb:
  source:
  - file_1.rb
  - file_2.rb
  - file_3.rb
  resource:
    images:
    - potato.png
    - fries.jpeg
    - franch_fries.png
    scripts:
      validation: teste.rb
      sorting:
      - shellsort.rb
      - quicksort.rb
      build.rb:
  generated: file.generated.rb

Considering that the file path is passed as a reference.

You can initialize the class as in the example below:

path = 'Replace/By/Your/YAML/FILE/PATH'
directory_structure = DirectoryStructure.new(path)
puts "That is my directory structure:\n#{directory_structure}"
# Output:
# That is my directory structure:
# lib
# ╠═ version.rb
# ╠═ source
# ║  ╠═ file_1.rb
# ║  ╠═ file_2.rb
# ║  ╚═ file_3.rb
# ╠═ resource
# ║  ╠═ images
# ║  ║  ╠═ potato.png
# ║  ║  ╠═ fries.jpeg
# ║  ║  ╚═ franch_fries.png
# ║  ╚═ scripts
# ║     ╠═ validation
# ║     ║  ╚═ teste.rb
# ║     ╠═ sorting
# ║     ║  ╠═ shellsort.rb
# ║     ║  ╚═ quicksort.rb
# ║     ╚═ build.rb
# ╚═ generated
#    ╚═ file.generated.rb

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ DirectoryStructure

Initializes the DirectoryStructure object.

Parameters:

  • content (Hash, String)

    A hash representing the directory structure or a path to a YAML file.

Raises:

  • (RuntimeError)

    If the content is not a Hash or a valid YAML file path.



126
127
128
129
130
131
132
133
134
# File 'lib/utils/directory_structure.rb', line 126

def initialize(content)
  if content.is_a?(Hash)
    @dir_hash = content
  elsif File.exist?(content)
    @dir_hash = YAML.load_file(content)
  else
    raise 'Need be initialized with a Hash or yaml file path'
  end
end

Instance Method Details

#to_sString

Converts the directory structure to a string representation.

Returns:

  • (String)

    The string representation of the directory structure.



139
140
141
142
143
# File 'lib/utils/directory_structure.rb', line 139

def to_s
  @output = String.new('')
  process_node(@dir_hash)
  @output
end