Module: ResourceGenerator::ClassMethods
- Defined in:
- lib/svrless/resource_generator.rb
Constant Summary collapse
- DEFAULT_ACTIONS =
%i[create show update destroy].freeze
Instance Method Summary collapse
- #full_klass_name(klass) ⇒ Object
- #generate_cloudformation(klasses:, actions:) ⇒ Object
- #generate_function_file(klass:, actions:) ⇒ Object
- #generate_function_files(klasses:, actions:) ⇒ Object
- #generate_svrless_controller ⇒ Object
- #plural_klass_name(klass) ⇒ Object
- #resources(*args) ⇒ Object
Instance Method Details
#full_klass_name(klass) ⇒ Object
25 26 27 |
# File 'lib/svrless/resource_generator.rb', line 25 def full_klass_name(klass) klass.to_s.camelize.pluralize end |
#generate_cloudformation(klasses:, actions:) ⇒ Object
15 16 17 18 19 |
# File 'lib/svrless/resource_generator.rb', line 15 def generate_cloudformation(klasses:, actions:) output = File.open("template.yaml", "w") output << CloudFormation::MainGenerator.new(klasses: klasses, actions: actions).representation output.close end |
#generate_function_file(klass:, actions:) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/svrless/resource_generator.rb', line 35 def generate_function_file(klass:, actions:) Dir.mkdir("app") unless Dir.exist?("app") Dir.mkdir("app/functions") unless Dir.exist?("app/functions") unless Dir.exist?("app/functions/#{plural_klass_name(klass)}") Dir.mkdir("app/functions/#{plural_klass_name(klass)}") end FileUtils.touch("app/functions/#{plural_klass_name(klass)}/controller.rb") File.open("app/functions/#{plural_klass_name(klass)}/controller.rb", "w") do |f| f.write "require 'svrless_controller'" f.write "\n" f.write "\n" f.write "class #{full_klass_name(klass)} < SvrlessController" f.write "\n" actions.each_with_index do |method, index| f.write " def self.#{method}(event:, context:)\n" f.write " {\n" f.write " statusCode: 200,\n" f.write " body: { hello: 'world' }.to_json\n" f.write " }\n" f.write " end\n" f.write "\n" unless actions.size == index + 1 end f.write "end" f.write "\n" end end |
#generate_function_files(klasses:, actions:) ⇒ Object
29 30 31 32 33 |
# File 'lib/svrless/resource_generator.rb', line 29 def generate_function_files(klasses:, actions:) klasses.each do |klass| generate_function_file(klass: klass, actions: actions) end end |
#generate_svrless_controller ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/svrless/resource_generator.rb', line 62 def generate_svrless_controller Dir.mkdir("app") unless Dir.exist?("app") Dir.mkdir("app/shared") unless Dir.exist?("app/shared") Dir.mkdir("app/shared/ruby") unless Dir.exist?("app/shared/ruby") Dir.mkdir("app/shared/ruby/lib") unless Dir.exist?("app/shared/ruby/lib") File.open("app/shared/ruby/lib/svrless_controller.rb", "w") do |f| f.write "class SvrlessController\n" f.write " def self.router(event:, context:)\n" f.write " case event['httpMethod']\n" f.write " when 'GET'\n" f.write " show(event: event, context: context)\n" f.write " when 'POST'\n" f.write " create(event: event, context: context)\n" f.write " when 'DELETE'\n" f.write " destroy(event: event, context: context)\n" f.write " else\n" f.write " update(event: event, context: context)\n" f.write " end\n" f.write " end\n" f.write "end\n" end end |
#plural_klass_name(klass) ⇒ Object
21 22 23 |
# File 'lib/svrless/resource_generator.rb', line 21 def plural_klass_name(klass) klass.to_s.pluralize end |
#resources(*args) ⇒ Object
9 10 11 12 13 |
# File 'lib/svrless/resource_generator.rb', line 9 def resources(*args) generate_svrless_controller generate_cloudformation(klasses: args, actions: DEFAULT_ACTIONS) generate_function_files(klasses: args, actions: DEFAULT_ACTIONS) end |