Hanami Controller 开源项目教程
1. 项目的目录结构及介绍
Hanami Controller 项目的目录结构如下:
hanami/
├── lib/
│ ├── hanami/
│ │ ├── controller/
│ │ │ ├── action.rb
│ │ │ ├── configuration.rb
│ │ │ ├── version.rb
│ │ │ └── ...
│ └── hanami.rb
├── spec/
│ ├── hanami/
│ │ ├── controller/
│ │ │ ├── action_spec.rb
│ │ │ ├── configuration_spec.rb
│ │ │ └── ...
│ └── spec_helper.rb
├── .gitignore
├── .rspec
├── .travis.yml
├── Gemfile
├── Gemfile.lock
├── LICENSE.md
├── README.md
└── hanami-controller.gemspec
目录结构介绍
lib/hanami/controller/
:包含 Hanami Controller 的核心文件,如action.rb
和configuration.rb
。spec/hanami/controller/
:包含项目的测试文件,如action_spec.rb
和configuration_spec.rb
。.gitignore
:Git 忽略文件列表。.rspec
:RSpec 配置文件。.travis.yml
:Travis CI 配置文件。Gemfile
和Gemfile.lock
:项目的依赖管理文件。LICENSE.md
:项目许可证。README.md
:项目说明文档。hanami-controller.gemspec
:Gem 规范文件。
2. 项目的启动文件介绍
Hanami Controller 的启动文件主要是 lib/hanami/controller.rb
,该文件负责加载 Hanami Controller 的核心组件和配置。
require 'hanami/utils'
require 'hanami/controller/version'
require 'hanami/controller/configuration'
require 'hanami/controller/action'
module Hanami
# Hanami Controller
module Controller
def self.configure(&block)
configuration.instance_eval(&block)
end
def self.configuration
@configuration ||= Configuration.new
end
end
end
启动文件介绍
require 'hanami/utils'
:加载 Hanami 工具库。require 'hanami/controller/version'
:加载版本信息。require 'hanami/controller/configuration'
:加载配置模块。require 'hanami/controller/action'
:加载动作模块。Hanami::Controller
模块定义了配置方法和获取配置实例的方法。
3. 项目的配置文件介绍
Hanami Controller 的配置文件主要是 lib/hanami/controller/configuration.rb
,该文件定义了 Hanami Controller 的配置选项和默认值。
module Hanami
module Controller
class Configuration
attr_accessor :default_headers, :default_format, :handle_exceptions, :handled_exceptions, :public_directory
def initialize
@default_headers = {}
@default_format = :html
@handle_exceptions = true
@handled_exceptions = []
@public_directory = 'public'
end
def finalize!
# Finalize configuration
end
end
end
end
配置文件介绍
default_headers
:默认的 HTTP 头信息。default_format
:默认的响应格式。handle_exceptions
:是否处理异常。handled_exceptions
:处理的异常列表。public_directory
:公共目录路径。finalize!
方法用于最终确定配置。
以上是 Hanami Controller 开源项目的教程,涵盖了项目的目录结构、启动文件和配置文件的介绍。希望对您有所帮助!