Ruby-Trailblazer构建在Rails之让提供一个直观的代码结构并给你一个面向对象的架构

  • s2_558754
    了解作者
  • 88.6KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-14 07:50
    上传日期
Trailblazer 构建在Rails之让,提供一个直观的代码结构并给你一个面向对象的架构
Ruby-Trailblazer构建在Rails之让提供一个直观的代码结构并给你一个面向对象的架构.zip
  • trailblazer-master
  • lib
  • trailblazer
  • version.rb
    73B
  • trailblazer.rb
    127B
  • doc
  • operation-2017.png
    16.8KB
  • trb.jpg
    17.6KB
  • Trb-The-Stack.png
    27.5KB
  • test
  • test_helper.rb
    852B
  • .rubocop_todo.yml
    14.4KB
  • .rubocop-https---raw-githubusercontent-com-trailblazer-meta-master-rubocop-yml
    2.2KB
  • CHANGES.md
    13.9KB
  • LICENSE.txt
    1.2KB
  • trailblazer.gemspec
    1.2KB
  • THOUGHTS
    510B
  • Gemfile
    96B
  • Rakefile
    255B
  • CONTRIBUTING.md
    7.4KB
  • .travis.yml
    129B
  • COMM-LICENSE
    16KB
  • README.md
    16.5KB
  • .rubocop.yml
    768B
  • .gitignore
    173B
内容介绍
# Trailblazer _Trailblazer provides new high-level abstractions for Ruby frameworks. It gently enforces encapsulation, an intuitive code structure and gives you an object-oriented architecture._ [![Gitter Chat](https://badges.gitter.im/trailblazer/chat.svg)](https://gitter.im/trailblazer/chat) [![TRB Newsletter](https://img.shields.io/badge/TRB-newsletter-lightgrey.svg)](http://trailblazer.to/newsletter/) [![Gem Version](https://badge.fury.io/rb/trailblazer.svg)](http://badge.fury.io/rb/trailblazer) [![Open Source Helpers](https://www.codetriage.com/trailblazer/trailblazer/badges/users.svg)](https://www.codetriage.com/trailblazer/trailblazer) **This document discusses Trailblazer 2.1.** An overview about the additions are [on our website](http://trailblazer.to/blog/2017-12-trailblazer-2-1-what-you-need-to-know.html). The [1.x documentation is here](http://trailblazer.to/gems/operation/1.1/). ## Trailblazer In A Nutshell 1. All business logic is encapsulated in [operations](#operation) (service objects). * Optional [validation objects](#validation) (Reform and/or Dry-validation) in the operation deserialize and validate input. The form object can also be used for rendering. * An optional [policy](#policies) object blocks unauthorized users from running the operation. * Optional [callback](#callbacks) objects allow declaring post-processing logic. 3. [Controllers](#controllers) instantly delegate to an operation. No business code in controllers, only HTTP-specific logic. 4. [Models](#models) are persistence-only and solely define associations and scopes. No business code is to be found here. No validations, no callbacks. 5. The presentation layer offers optional [view models](#views) (Cells) and [representers](#representers) for document APIs. Trailblazer is designed to handle different contexts like user roles by applying [inheritance](#inheritance) between and [composing](#composing) of operations, form objects, policies, representers and callbacks. Want code? Jump [right here](#controllers)! ## Mission While _Trailblazer_ offers you abstraction layers for all aspects of Ruby On Rails, it does _not_ missionize you. Wherever you want, you may fall back to the "Rails Way" with fat models, monolithic controllers, global helpers, etc. This is not a bad thing, but allows you to step-wise introduce Trailblazer's encapsulation in your app without having to rewrite it. Trailblazer is all about structure. It helps re-organize existing code into smaller components where different concerns are handled in separated classes. Again, you can pick which layers you want. Trailblazer doesn't impose technical implementations, it offers mature solutions for recurring problems in all types of Rails applications. Trailblazer is no "complex web of objects and indirection". It solves many problems that have been around for years with a cleanly layered architecture. Only use what you like. And that's the bottom line. ## Concepts over Technology Trailblazer offers you a new, more intuitive file layout in applications. ``` app ├── concepts │ ├── song │ │ ├── operation │ │ │ ├── create.rb │ │ │ ├── update.rb │ │ ├── contract │ │ │ ├── create.rb │ │ │ ├── update.rb │ │ ├── cell │ │ │ ├── show.rb │ │ │ ├── index.rb │ │ ├── view │ │ │ ├── show.haml │ │ │ ├── index.rb │ │ │ ├── song.css.sass ``` Instead of grouping by technology, classes and views are structured by *concept*, and then by technology. A concept can relate to a model, or can be a completely abstract concern such as `invoicing`. Within a concept, you can have any level of nesting. For example, `invoicing/pdf/` could be one. The file structure is implemented by the [`trailblazer-loader` gem](https://github.com/trailblazer/trailblazer-loader). [Learn more.](http://trailblazer.to/gems/trailblazer/loader.html) ## Architecture Trailblazer extends the conventional MVC stack in Rails. Keep in mind that adding layers doesn't necessarily mean adding more code and complexity. The opposite is the case: Controller, view and model become lean endpoints for HTTP, rendering and persistence. Redundant code gets eliminated by putting very little application code into the right layer. ![The Trailblazer stack.](https://raw.github.com/apotonick/trailblazer/master/doc/operation-2017.png) ## Routing Trailblazer uses Rails routing to map URLs to controllers, because it works. ```ruby Rails.application.routes.draw do resources :songs end ``` ## Controllers Controllers are lean endpoints for HTTP. They do not contain any business logic. Actions immediately dispatch to an operation. ```ruby class SongsController < ApplicationController def create run Song::Create # Song::Create is an operation class. end end ``` The `#run` method invokes the operation. It allows you to run a conditional block of logic if the operation was successful. ```ruby class SongsController < ApplicationController def create run Song::Create do |op| return redirect_to(song_path op.model) # success! end render :new # invalid. re-render form. end end ``` Again, the controller only dispatchs to the operation and handles successful/invalid processing on the HTTP level. For instance by redirecting, setting flash messages, or signing in a user. [Learn more.](http://trailblazer.to/gems/operation/controller.html) ## Operation Operations encapsulate business logic and are the heart of a Trailblazer architecture. The bare bones operation without any Trailblazery is implemented in [the `trailblazer-operation` gem](https://github.com/trailblazer/trailblazer-operation) and can be used without our stack. Operations don't know about HTTP or the environment. You could use an operation in Rails, Hanami, or Roda, it wouldn't know. An operation is not just a monolithic replacement for your business code. It's a simple orchestrator between the form objects, models, your business code and all other layers needed to get the job done. ```ruby class Song::Create < Trailblazer::Operation step :process! def process!(options) # do whatever you feel like. end end ``` Operations only need to define and implement steps, like the `#process!` steps. Those steps receive the arguments from the caller. You cannot instantiate them per design. The only way to invoke them is `call`. ```ruby Song::Create.call(whatever: "goes", in: "here") # same as Song::Create.(whatever: "goes", in: "here") ``` Their high degree of encapsulation makes them a [replacement for test factories](#test), too. [Learn more.](http://trailblazer.to/gems/operation) ### Contract The Contract Macro, covers the contracts for Trailblazer, they are basically Reform objects that you can define and validate inside an operation. Reform is a fantastic tool for deserializing and validating deeply nested hashes, and then, when valid, writing those to the database using your persistence layer such as ActiveRecord. ```ruby # app/concepts/song/contract/create.rb module Song::Contract class Create < Reform::Form property :title property :length validates :title, length: 2..33 validates :length, numericality: true end end ``` The Contract then gets hooked into the operation. using this Macro. ```ruby # app/concepts/song/operation/create.rb class Song::Create < Trailblazer::Operation step Model( Song, :new ) step Contract::Build( constant: Song::Contract::Create ) step Contract::Validate() step Contract::Persist() end ``` As you can see, using contracts consists of five steps. Define the contract class (or multiple of them) for the operation. Plug the contract creation into the operation’s pipe using Contract::Build. Run the contract’s validation for the params using Contract::Validate. If s
评论
    相关推荐
    • trailblazer:Ruby的高级架构
      Trailblazer为Ruby框架提供了新的高级抽象。 它轻柔地执行封装,直观的代码结构,并以功能性思维方式对复杂的业务工作流进行建模。 文献资料 本文档讨论Trailblazer 2.1。 有关新增功能的概述。 我们正在开发几个新...
    • oxford_dictionary:牛津字典API的Ruby包装器
      Ruby包装器使用 入门 $ gem install oxford_dictionary # To use in your script/application require 'oxford_dictionary' 注册API密钥后,请设置客户端: client = OxfordDictionary :: Client . new ( app_id ...
    • Ruby_向ruby之父学程序设计(第二版)(经典入门)
      第一部分:通过简单的Ruby程序来介绍程序的基本架构。  第二部分:介绍基础语法规则,以及类、模块等面向对象程序设计的思考方法与用词。   第三部分:对一些基础类逐一介绍其功能与用法。 第四部分:介绍一些...
    • 为使用和弦输入的ANSI键盘设计的Rime Nihongo输入架构。-Ruby开发
      日本和弦是一款为ANSI、ISO、60%、40% 键盘设计的多键并击输入日本语的方法, 日本和弦日本语コード配方:℞ nihonchord Rime 日本和弦输入方案简介日本和弦是一款为ANSI 、ISO、60%、40% 键盘设计的多键并击输入...
    • ruby2600:一个实验性的 Atari:trade_mark: 2600 模拟器,100% 用 Ruby 编写
      这些展示了一般架构和基本原理。 当前状态 没有明显的毛刺工作游戏包括: 陷阱! 太空侵略者 河流突袭 吃豆人 网球 大金刚 您可以加载大多数 2K 和 4K 推车并试用它们。 速度非常低:在 2.3Ghz 的计算机上大约 ...
    • dotenv-schema:为 dotenv 定义架构
      dotenv 模式 Dotenv-schema 使模式化。 安装 编写.env和.env_schema : $ cat .env ...DB_PORT=3306 $ cat .env_schema DB_HOST: ...Sinatra 或Plain ol' Ruby $ gem install dotenv require 'dotenv-
    • 从RSpec请求规范生成OpenAPI模式-Ruby开发
      根据RSpec请求规范生成OpenAPI架构。 rspec-openapi根据RSpec请求规范生成OpenAPI架构。 这是什么? 有一些宝石可以根据RSpec请求规范生成OpenAPI规范。 但是,它们需要专用于这些gem的特殊DSL,并且我们无法按原样...
    • parametric:Ruby应用程序的声明式输入模式
      Ruby对象中声明性地定义数据模式,并使用它们将白名单,验证或转换为程序的输入。 对于构建自定义API,搜索或表单对象很有用。 或者可以替代Rails的强参数(它不依赖于Rails,可以独立使用)。 架构图 定义架构 ...
    • 榴弹炮:基于Ruby的验收测试框架
      快速安装和配置完整的测试基础架构(不到5分钟)。 下方是优雅,直观且功能强大的Ruby语言。 可以选择您喜欢的BDD工具(Cucumber,RSpec或芜菁)。 与SauceLabs,Testingbot,BrowserStack,CrossBrowserTesting...
    • GaussDB_100_1.0.1-DATABASE-REDHAT-64bit.tar.gz
      guassdb100在redhat上安装包,单机部署的包,安装步骤请看我的文中介绍,经过大量实验搭建总结出来的文档