Class: Whoosh::ClientGen::BaseGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/client_gen/base_generator.rb

Constant Summary collapse

TYPE_MAPS =
{
  typescript: {
    string: "string", integer: "number", number: "number",
    boolean: "boolean", array: "any[]", object: "Record<string, any>"
  },
  swift: {
    string: "String", integer: "Int", number: "Double",
    boolean: "Bool", array: "[Any]", object: "[String: Any]"
  },
  dart: {
    string: "String", integer: "int", number: "double",
    boolean: "bool", array: "List<dynamic>", object: "Map<String, dynamic>"
  },
  ruby: {
    string: "String", integer: "Integer", number: "Float",
    boolean: "Boolean", array: "Array", object: "Hash"
  },
  html: {
    string: "text", integer: "number", number: "number",
    boolean: "checkbox", array: "text", object: "text"
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ir:, output_dir:, platform:) ⇒ BaseGenerator

Returns a new instance of BaseGenerator.



35
36
37
38
39
# File 'lib/whoosh/client_gen/base_generator.rb', line 35

def initialize(ir:, output_dir:, platform:)
  @ir = ir
  @output_dir = output_dir
  @platform = platform
end

Instance Attribute Details

#irObject (readonly)

Returns the value of attribute ir.



33
34
35
# File 'lib/whoosh/client_gen/base_generator.rb', line 33

def ir
  @ir
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



33
34
35
# File 'lib/whoosh/client_gen/base_generator.rb', line 33

def output_dir
  @output_dir
end

#platformObject (readonly)

Returns the value of attribute platform.



33
34
35
# File 'lib/whoosh/client_gen/base_generator.rb', line 33

def platform
  @platform
end

Instance Method Details

#camelize(name) ⇒ Object



75
76
77
# File 'lib/whoosh/client_gen/base_generator.rb', line 75

def camelize(name)
  name.to_s.split(/[-_]/).map(&:capitalize).join
end

#classify(name) ⇒ Object



55
56
57
58
# File 'lib/whoosh/client_gen/base_generator.rb', line 55

def classify(name)
  singular = singularize(name.to_s)
  singular.split(/[-_]/).map(&:capitalize).join
end

#generateObject

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/whoosh/client_gen/base_generator.rb', line 41

def generate
  raise NotImplementedError, "Subclasses must implement #generate"
end

#singularize(word) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/whoosh/client_gen/base_generator.rb', line 60

def singularize(word)
  w = word.to_s
  if w.end_with?("ies")
    w[0..-4] + "y"
  elsif w.end_with?("ses") || w.end_with?("xes") || w.end_with?("zes") || w.end_with?("ches") || w.end_with?("shes")
    w[0..-3]
  elsif w.end_with?("sses")
    w[0..-3]
  elsif w.end_with?("s") && !w.end_with?("ss") && !w.end_with?("us")
    w[0..-2]
  else
    w
  end
end

#snake_case(name) ⇒ Object



79
80
81
# File 'lib/whoosh/client_gen/base_generator.rb', line 79

def snake_case(name)
  name.to_s.gsub(/([A-Z])/, '_\1').downcase.sub(/^_/, "")
end

#type_for(ir_type) ⇒ Object



45
46
47
# File 'lib/whoosh/client_gen/base_generator.rb', line 45

def type_for(ir_type)
  TYPE_MAPS.dig(@platform, ir_type.to_sym) || "string"
end

#write_file(relative_path, content) ⇒ Object



49
50
51
52
53
# File 'lib/whoosh/client_gen/base_generator.rb', line 49

def write_file(relative_path, content)
  full_path = File.join(@output_dir, relative_path)
  FileUtils.mkdir_p(File.dirname(full_path))
  File.write(full_path, content)
end