Module: FoodTruck

Defined in:
lib/food_truck.rb,
lib/food_truck/mod.rb,
lib/food_truck/load.rb,
lib/food_truck/error.rb,
lib/food_truck/version.rb,
lib/food_truck/symbolize.rb,
lib/food_truck/models/attr.rb,
lib/food_truck/models/func.rb,
lib/food_truck/models/class.rb,
lib/food_truck/models/param.rb

Overview

Ruby code generator.

Defined Under Namespace

Classes: Attr, Class, Error, Func, Param

Constant Summary collapse

QUOTE =
'"'.freeze
VERSION =
"0.5.0".freeze

Class Method Summary collapse

Class Method Details

.get_extension(path) ⇒ String

Parameters:

  • path (String)

    Path of the file to determine the extension of.

Returns:



25
26
27
28
# File 'lib/food_truck/load.rb', line 25

def self.get_extension(path)
  file = Pathname.new(path)
  return file.extname.downcase
end

.load_file(path) ⇒ Hash<Symbol>

Load data from a YAML or JSON file.

Parameters:

Returns:

  • (Hash<Symbol>)


34
35
36
37
38
39
40
41
42
43
# File 'lib/food_truck/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 FoodTruck::Error "invalid file type"
  end
end

.load_json(path) ⇒ Hash<Symbol>

Load data from a JSON file.

Parameters:

Returns:

  • (Hash<Symbol>)


19
20
21
# File 'lib/food_truck/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) ⇒ Hash<Symbol>

Load data from a YAML file.

Parameters:

Returns:

  • (Hash<Symbol>)


11
12
13
# File 'lib/food_truck/load.rb', line 11

def self.load_yaml(path)
  return FoodTruck.symbolize_keys(YAML.load_file(File.expand_path(path)))
end

.mod(body, *names) ⇒ String

Used to generate a module.

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

Examples:

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

Parameters:

  • body (String)

    Name of module namespaces.

  • names (String, Array<String>)

    Name of module namespaces.

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/food_truck/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

.symbolize_keys(arg) ⇒ Hash, Array

Recursively convert keys in a Hash or Array to symbols.

See:

Parameters:

  • obj (Hash, Array)

Returns:

  • (Hash, Array)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/food_truck/symbolize.rb', line 10

def self.symbolize_keys(arg)
  if arg.is_a?(Hash)
    arg.inject({}) do |result, (key, value)|
      new_key = case key
                when String then key.to_sym()
                else key
                end
      new_value = case value
                  when Hash then symbolize_keys(value)
                  when Array then symbolize_keys(value)
                  else value
                  end
      result[new_key] = new_value
      result
    end
  elsif arg.is_a?(Array)
    arg.map { |e| symbolize_keys(e) }
  else
    arg
  end
end