Class: Whoosh::CLI::Generators

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/cli/generators.rb

Constant Summary collapse

TYPE_MAP =
{
  "string" => "String", "integer" => "Integer", "float" => "Float",
  "boolean" => "Whoosh::Types::Bool", "text" => "String",
  "datetime" => "Time"
}.freeze

Class Method Summary collapse

Class Method Details

.classify(name) ⇒ Object



128
129
130
# File 'lib/whoosh/cli/generators.rb', line 128

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

.endpoint(name, fields = [], root: Dir.pwd) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/whoosh/cli/generators.rb', line 15

def self.endpoint(name, fields = [], root: Dir.pwd)
  cn = classify(name)
  FileUtils.mkdir_p(File.join(root, "endpoints"))
  FileUtils.mkdir_p(File.join(root, "schemas"))
  FileUtils.mkdir_p(File.join(root, "test", "endpoints"))

  # Generate schema with fields
  schema(name, fields, root: root) unless fields.empty?

  unless fields.empty?
    # schema already generated above, skip the blank one
  else
    File.write(File.join(root, "schemas", "#{name}.rb"),
      "# frozen_string_literal: true\n\nclass #{cn}Request < Whoosh::Schema\n  # Add fields here\nend\n\nclass #{cn}Response < Whoosh::Schema\n  # Add fields here\nend\n")
  end

  File.write(File.join(root, "endpoints", "#{name}.rb"),
    "# frozen_string_literal: true\n\nclass #{cn}Endpoint < Whoosh::Endpoint\n  post \"/#{name}\", request: #{cn}Request\n\n  def call(req)\n    { message: \"#{cn} endpoint\" }\n  end\nend\n")

  File.write(File.join(root, "test", "endpoints", "#{name}_test.rb"),
    "# frozen_string_literal: true\n\nrequire \"test_helper\"\n\nRSpec.describe #{cn}Endpoint do\n  it \"responds to POST /#{name}\" do\n    post \"/#{name}\"\n    expect(last_response.status).to eq(200)\n  end\nend\n")

  puts "Created endpoints/#{name}.rb, schemas/#{name}.rb, test/endpoints/#{name}_test.rb"
end

.migration(name, root: Dir.pwd) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/whoosh/cli/generators.rb', line 83

def self.migration(name, root: Dir.pwd)
  ts = Time.now.strftime("%Y%m%d%H%M%S")
  FileUtils.mkdir_p(File.join(root, "db", "migrations"))
  File.write(File.join(root, "db", "migrations", "#{ts}_#{name}.rb"),
    "Sequel.migration do\n  change do\n    # Add migration code here\n  end\nend\n")
  puts "Created db/migrations/#{ts}_#{name}.rb"
end

.model(name, fields = [], root: Dir.pwd) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/whoosh/cli/generators.rb', line 56

def self.model(name, fields = [], root: Dir.pwd)
  cn = classify(name)
  table = "#{name.downcase}s"
  ts = Time.now.strftime("%Y%m%d%H%M%S")

  FileUtils.mkdir_p(File.join(root, "models"))
  FileUtils.mkdir_p(File.join(root, "db", "migrations"))

  File.write(File.join(root, "models", "#{name.downcase}.rb"),
    "# frozen_string_literal: true\n\nclass #{cn} < Sequel::Model(:#{table})\nend\n")

  cols = fields.map { |f|
    col, type = f.split(":")
    st = { "string" => "String", "integer" => "Integer", "float" => "Float", "boolean" => "TrueClass", "text" => "String, text: true", "datetime" => "DateTime" }[type] || "String"
    "      #{st} :#{col}, null: false"
  }.join("\n")

  File.write(File.join(root, "db", "migrations", "#{ts}_create_#{table}.rb"),
    "Sequel.migration do\n  change do\n    create_table(:#{table}) do\n      primary_key :id\n#{cols}\n      DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP\n      DateTime :updated_at\n    end\n  end\nend\n")

  FileUtils.mkdir_p(File.join(root, "test", "models"))
  File.write(File.join(root, "test", "models", "#{name.downcase}_test.rb"),
    "# frozen_string_literal: true\n\nrequire \"test_helper\"\n\nRSpec.describe #{cn} do\n  it \"exists\" do\n    expect(#{cn}).to be_a(Class)\n  end\nend\n")

  puts "Created models/#{name.downcase}.rb, db/migrations/#{ts}_create_#{table}.rb"
end

.plugin(name, root: Dir.pwd) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/whoosh/cli/generators.rb', line 91

def self.plugin(name, root: Dir.pwd)
  cn = classify(name)
  FileUtils.mkdir_p(File.join(root, "lib"))

  File.write(File.join(root, "lib", "#{name}_plugin.rb"), <<~RUBY)
    # frozen_string_literal: true

    class #{cn}Plugin < Whoosh::Plugins::Base
      gem_name "#{name}"
      accessor_name :#{name.tr("-", "_")}

      def self.initialize_plugin(config)
        # Initialize and return plugin instance
        nil
      end
    end
  RUBY
  puts "Created lib/#{name}_plugin.rb"
end

.proto(name, root: Dir.pwd) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/whoosh/cli/generators.rb', line 111

def self.proto(name, root: Dir.pwd)
  FileUtils.mkdir_p(File.join(root, "protos"))
  msg_name = name.match?(/[_-]/) ? classify(name) : name

  File.write(File.join(root, "protos", "#{name.downcase}.proto"), <<~PROTO)
    syntax = "proto3";

    package whoosh;

    message #{msg_name} {
      // Add fields here
      // string name = 1;
    }
  PROTO
  puts "Created protos/#{name.downcase}.proto"
end

.schema(name, fields = [], root: Dir.pwd) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/whoosh/cli/generators.rb', line 40

def self.schema(name, fields = [], root: Dir.pwd)
  cn = classify(name)
  FileUtils.mkdir_p(File.join(root, "schemas"))

  field_lines = fields.map { |f|
    col, type = f.split(":")
    ruby_type = TYPE_MAP[type] || "String"
    "  field :#{col}, #{ruby_type}, required: true"
  }.join("\n")
  field_lines = "  # field :name, String, required: true, desc: \"Description\"" if field_lines.empty?

  File.write(File.join(root, "schemas", "#{name}.rb"),
    "# frozen_string_literal: true\n\nclass #{cn}Schema < Whoosh::Schema\n#{field_lines}\nend\n")
  puts "Created schemas/#{name}.rb"
end