module Ginny

Ruby code generator.

Constants

INDENT
QUOTE
VERSION

Public Class Methods

get_extension(path) click to toggle source

@param path [String] Path of the file to determine the extension of. @return [String]

# File lib/ginny/load.rb, line 25
def self.get_extension(path)
  file = Pathname.new(path)
  return file.extname.downcase
end
load_file(path) click to toggle source

Load data from a YAML or JSON file.

@param path [String] @return [Hash<Symbol>]

# File lib/ginny/load.rb, line 34
def self.load_file(path)
  case Pathname.new(path).extname.downcase
  when ".yml", ".yaml"
    return self.load_yaml(path)
  when ".json"
    return self.load_json(path)
  else
    raise Ginny::Error "invalid file type"
  end
end
load_json(path) click to toggle source

Load data from a JSON file.

@param path [String] @return [Hash<Symbol>]

# File lib/ginny/load.rb, line 19
def self.load_json(path)
  return JSON.parse(File.read(File.expand_path(path)), symbolize_names: true)
end
load_yaml(path) click to toggle source

Load data from a YAML file.

@param path [String] @return [Hash<Symbol>]

# File lib/ginny/load.rb, line 11
def self.load_yaml(path)
  return Coolkit.symbolize_keys(YAML.load_file(File.expand_path(path)))
end
mod(body, *names) click to toggle source

Used to generate a [module](ruby-doc.org/core-2.6.5/doc/syntax/modules_and_classes_rdoc.html).

More accurately, wrap the `body` (first argument) with any following module definitions (additional arguments).

@example

Ginny.mod("puts('Hello World')", "Level1", "Level2")
#=> module Level1
      module Level2
        puts('Hello World')
      end
    end

@param body [String] Name of module namespaces. @param names [String,Array<String>] Name of module namespaces. @return [String]

# File lib/ginny/mod.rb, line 18
def self.mod(body, *names)
  names.flatten!
  count = names.length
  return body unless count.positive?()
  level = 0
  head = []
  tail = []
  names.each do |name|
    head.push("module #{name}".indent(level))
    tail.unshift("end".indent(level))
    level += 2
  end
  return (head + [body&.indent(level)] + tail).compact.join("\n")
end