首页
/ Chanko 技术文档

Chanko 技术文档

2024-12-26 04:46:06作者:虞亚竹Luna

1. 安装指南

在开始使用 Chanko 前,请确保您的开发环境已满足以下要求:

  • Ruby >= 3.0.0
  • Rails >= 6.1.0

将 Chanko 添加到您的 Gemfile 中:

gem "chanko"

然后执行以下命令安装:

$ bundle install

2. 项目使用说明

Chanko 提供了一个简单的框架,用于快速且安全地在生产 Rails 应用中原型化新功能,并将这些原型暴露给指定的用户群体。

创建 Unit

使用以下命令创建 Unit 模板:

$ rails generate chanko:unit example_unit

调用 Unit 逻辑

在控制器中,您可以通过 invokeunit 方法调用 Unit 中的逻辑。此外,在控制器类上下文中,还提供了 unit_action 工具方法。

以下是一个示例:

# app/controllers/users_controller.rb
class UsersController < ApplicationController
  unit_action :example_unit, :show

  def index
    invoke(:example_unit, :index) do
      @users = User.all
    end
  end
end

在视图中,您可以这样调用:

# app/views/examples/index.html.slim
= unit.helper_method
= invoke(:example_unit, :render_example)

定义 Unit 模块

您可以在 Unit 模块中定义 MVC 代码。例如:

# app/units/example_unit/example_unit.rb
module ExampleUnit
  include Chanko::Unit
  ...
end

设置 Unit 激活条件

使用 active_if 块来决定 Unit 是否激活:

active_if do |context, options|
  true
end

强制抛出错误

默认情况下,生产环境中抛出的任何错误都会被忽略。使用 raise_error 强制 Unit 抛出调用中发生的错误:

raise_error

定义函数

在控制器或视图上下文中,您可以通过 function 定义函数,并通过 invoke 调用它们:

scope(:controller) do
  function(:show) do
    @user = User.find(params[:id])
  end

  function(:index) do
    @users = User.active
  end
end

定义帮助方法

在视图上下文中,您可以通过 helpers 定义帮助方法,并通过 Unit 代理调用它们:

helpers do
  def helper_method
    "helper method"
  end
end

3. 项目 API 使用文档

由于 Chanko 是一个框架,不直接提供 API,而是通过 Rails 的 MVC 结构与您的应用集成,具体的 API 使用方式将在上述使用说明中体现。

4. 项目安装方式

如安装指南所述,您需要将 Chanko 添加到 Gemfile 中,然后执行 bundle install 命令进行安装。

登录后查看全文
热门项目推荐