Hanami Controller 开源项目教程

Hanami Controller 开源项目教程

controllerComplete, fast and testable actions for Rack and Hanami项目地址:https://gitcode.com/gh_mirrors/contr/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.rbconfiguration.rb
  • spec/hanami/controller/:包含项目的测试文件,如 action_spec.rbconfiguration_spec.rb
  • .gitignore:Git 忽略文件列表。
  • .rspec:RSpec 配置文件。
  • .travis.yml:Travis CI 配置文件。
  • GemfileGemfile.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 开源项目的教程,涵盖了项目的目录结构、启动文件和配置文件的介绍。希望对您有所帮助!

controllerComplete, fast and testable actions for Rack and Hanami项目地址:https://gitcode.com/gh_mirrors/contr/controller

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

管雅姝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值