最近我一直在尝试 Roda + Sequel。我创建了具有以下文件结构的简单 API:
├── CHANGELOG.md
├── Gemfile
├── Gemfile.lock
├── Procfile
├── README.md
├── Rakefile
├── app
│ ├── models
│ │ ├── category.rb
│ │ ├── organization.rb
│ │ └── user.rb
│ ├── schemas
│ │ ├── api
│ │ │ └── v1
│ │ │ ├── login_schema.rb
│ │ │ ├── send_reset_password_instructions_schema.rb
│ │ │ └── update_password_schema.rb
│ │ └── application_schema.rb
│ ├── serializers
│ │ ├── api
│ │ │ └── v1
│ │ │ ├── categories_serializer.rb
│ │ │ ├── login_serializer.rb
│ │ │ └── refresh_token_serializer.rb
│ │ └── application_serializer.rb
│ └── services
│ └── users
│ └── authenticator.rb
├── app.rb
├── bin
│ ├── console
│ └── rake
├── config
│ ├── active_support.rb
│ ├── bcrypt.rb
│ ├── boot.rb
│ ├── database.rb
│ ├── dry_rb.rb
│ ├── environment_variables.rb
│ ├── freeze.rb
│ ├── i18n.rb
│ ├── locales
│ │ ├── errors.pl.yml
│ │ └── pl.yml
│ ├── logger.rb
│ ├── models.rb
│ ├── oj.rb
│ └── puma.rb
├── config.ru
├── db
│ ├── migrate
│ │ ├── 001_enable_uuid_ossp_extension.rb
│ │ ├── 002_create_organizations_table.rb
│ │ ├── 003_drop_organizations_name_unique_index.rb
│ │ ├── 004_create_categories_table.rb
│ │ ├── 005_enable_citext_extension.rb
│ │ ├── 006_create_users_table.rb
│ │ └── 007_add_authentication_token_to_users.rb
│ ├── seeds
│ │ ├── categories.rb
│ │ ├── organizations.rb
│ │ └── users.rb
│ ├── seeds.rb
│ └── structure.sql
├── lib
│ ├── authentication_token_generator.rb
│ ├── authorization_token_validator.rb
│ ├── authorization_tokens_generator.rb
│ ├── constants.rb
│ ├── exceptions.rb
│ ├── message_verifier.rb
│ └── update_password_token_generator.rb
└── spec
├── lib
│ ├── authentication_token_generator_spec.rb
│ ├── authorization_token_validator_spec.rb
│ ├── authorization_tokens_generator_spec.rb
│ ├── constants_spec.rb
│ ├── message_verifier_spec.rb
│ └── update_password_token_generator_spec.rb
├── models
│ ├── category_spec.rb
│ ├── organization_spec.rb
│ └── user_spec.rb
├── requests
│ ├── api
│ │ └── v1
│ │ ├── categories
│ │ │ └── index_spec.rb
│ │ ├── login_spec.rb
│ │ ├── logout_spec.rb
│ │ ├── refresh_token_spec.rb
│ │ ├── send_reset_password_instructions_spec.rb
│ │ └── update_password_spec.rb
│ └── heartbeat_spec.rb
├── schemas
│ └── api
│ └── v1
│ ├── login_schema_spec.rb
│ ├── send_reset_password_instructions_schema_spec.rb
│ └── update_password_schema_spec.rb
├── services
│ └── users
│ └── authenticator_spec.rb
├── spec_helper.rb
└── support
├── api_helpers.rb
└── shared_examples
└── unauthorized.rb
我有这个config/boot.rb
文件,它负责加载我的所有依赖项:
# frozen_string_literal: true
# This file is responsible for loading all configuration files.
require 'pry'
require 'securerandom'
# Require config files.
require_relative './environment_variables'
require_relative './database'
require_relative './models'
require_relative './i18n'
require_relative './dry_rb'
require_relative './oj'
require_relative './active_support'
require_relative './bcrypt'
# Require all files in lib folder.
require './lib/constants'
require './lib/exceptions'
require './lib/authentication_token_generator'
require './lib/authorization_tokens_generator'
require './lib/authorization_token_validator'
require './lib/update_password_token_generator'
require './lib/message_verifier'
# Require all files in app/models folder.
require './app/models/organization'
require './app/models/category'
require './app/models/user'
# Require all files in app/schemas folder.
require './app/schemas/application_schema'
require './app/schemas/api/v1/login_schema'
require './app/schemas/api/v1/update_password_schema'
require './app/schemas/api/v1/send_reset_password_instructions_schema'
# Require all files in app/services folder.
require './app/services/users/authenticator'
# Require all files in app/serializers folder.
require './app/serializers/application_serializer'
require './app/serializers/api/v1/categories_serializer'
require './app/serializers/api/v1/login_serializer'
require './app/serializers/api/v1/refresh_token_serializer'
require_relative './logger'
require_relative './freeze'
一切都很好,但我想知道是否有更好的方法来做到这一点。
Dir
我尝试使用辅助方法来清理它:
# frozen_string_literal: true
# This file is responsible for loading all configuration files.
require 'pry'
require 'securerandom'
# Require config files.
require_relative './environment_variables'
require_relative './database'
require_relative './models'
require_relative './i18n'
require_relative './dry_rb'
require_relative './oj'
require_relative './active_support'
require_relative './bcrypt'
# Require all files in the lib folder.
Dir['./lib/**/**/*.rb'].each { |f| require f }
# Require all files in the app folder.
Dir['./app/**/**/*.rb'].each { |f| require f }
require_relative './logger'
require_relative './freeze'
但是使用这种方法,我的测试失败了:
An error occurred while loading spec_helper.
Failure/Error:
class LoginSchema < ::ApplicationSchema
# @!method params
# It stores rules for validating POST /api/v1/login endpoint params using dry-validation DSL.
params do
required(:email).filled(:string).value(format?: Constants::EMAIL_REGEX)
required(:password).filled(:string)
end
NameError:
uninitialized constant ::ApplicationSchema
# ./app/schemas/api/v1/login_schema.rb:11:in `<module:V1>'
# ./app/schemas/api/v1/login_schema.rb:4:in `<module:Api>'
# ./app/schemas/api/v1/login_schema.rb:3:in `<top (required)>'
# ./config/boot.rb:23:in `require'
# ./config/boot.rb:23:in `block in <top (required)>'
# ./config/boot.rb:23:in `each'
# ./config/boot.rb:23:in `<top (required)>'
# ./app.rb:5:in `require_relative'
# ./app.rb:5:in `<top (required)>'
# ./spec/spec_helper.rb:8:in `require_relative'
# ./spec/spec_helper.rb:8:in `<top (required)>'
No examples found.
No examples found.
Finished in 0.00004 seconds (files took 1.08 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
Finished in 0.00004 seconds (files took 1.08 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
似乎测试失败了,因为Api::V1::LoginSchema
之前需要ApplicationSchema
。
我将不胜感激任何建议。