Strip Attributes 开源项目教程

Strip Attributes 开源项目教程

strip_attributes:hocho: An ActiveModel extension that automatically strips all attributes of leading and trailing whitespace before validation. If the attribute is blank, it strips the value to nil.项目地址:https://gitcode.com/gh_mirrors/st/strip_attributes

1. 项目的目录结构及介绍

Strip Attributes 是一个用于从 ActiveRecord 模型中移除空白属性的 Ruby gem。以下是其基本的目录结构:

strip_attributes/
├── Gemfile
├── Gemfile.lock
├── LICENSE
├── README.md
├── Rakefile
├── lib/
│   ├── strip_attributes.rb
│   └── strip_attributes/
│       ├── version.rb
├── strip_attributes.gemspec
├── test/
    ├── helper.rb
    └── strip_attributes_test.rb

目录结构介绍

  • GemfileGemfile.lock: 用于管理项目的依赖。
  • LICENSE: 项目的许可证文件。
  • README.md: 项目说明文档。
  • Rakefile: 用于定义 Rake 任务。
  • lib/: 包含项目的主要代码。
    • strip_attributes.rb: 主文件,包含 StripAttributes 模块的定义。
    • strip_attributes/version.rb: 定义项目的版本号。
  • strip_attributes.gemspec: gem 的规范文件,包含 gem 的元数据和依赖。
  • test/: 包含测试文件。
    • helper.rb: 测试辅助文件。
    • strip_attributes_test.rb: 测试用例文件。

2. 项目的启动文件介绍

Strip Attributes 的启动文件是 lib/strip_attributes.rb。这个文件定义了 StripAttributes 模块,并包含了主要的逻辑和方法。

require "active_record"
require "active_support/concern"

module StripAttributes
  extend ActiveSupport::Concern

  included do
    before_validation :strip_attributes
  end

  module ClassMethods
    def strip_attributes(options = {})
      define_method(:strip_attributes) do
        self.class.columns.each do |column|
          if column.type == :string || column.type == :text
            value = send(column.name)
            if value.respond_to?(:strip)
              send("#{column.name}=", value.strip)
            end
          end
        end
      end
    end
  end
end

ActiveRecord::Base.send(:include, StripAttributes)

启动文件介绍

  • require "active_record"require "active_support/concern": 引入 ActiveRecord 和 ActiveSupport::Concern。
  • module StripAttributes: 定义 StripAttributes 模块。
  • extend ActiveSupport::Concern: 使用 ActiveSupport::Concern 来简化模块的定义。
  • included do: 在包含模块时执行的代码块,设置 before_validation 回调。
  • module ClassMethods: 定义类方法 strip_attributes,用于定义 strip_attributes 方法。
  • ActiveRecord::Base.send(:include, StripAttributes): 将 StripAttributes 模块包含到 ActiveRecord::Base 中。

3. 项目的配置文件介绍

Strip Attributes 的配置文件是 strip_attributes.gemspec。这个文件包含了 gem 的元数据和依赖。

# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "strip_attributes/version"

Gem::Specification.new do |s|
  s.name        = "strip_attributes"
  s.version     = StripAttributes::VERSION
  s.authors     = ["Ryan McGeary"]
  s.email       = ["ryan@mcgeary.org"]
  s.homepage    = "https://github.com/rmm5t/strip_attributes"
  s.summary     = %q{Whitespace cleanup for ActiveModel attributes}
  s.description = %q{StripAttributes automatically strips all ActiveRecord model attributes of leading and trailing whitespace before validation. If the attribute is blank, it strips the value to nil.}

  s.files         = `git ls-files`.split("\n")
  s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
  s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
  s.require_paths = ["lib"]

  s.add_

strip_attributes:hocho: An ActiveModel extension that automatically strips all attributes of leading and trailing whitespace before validation. If the attribute is blank, it strips the value to nil.项目地址:https://gitcode.com/gh_mirrors/st/strip_attributes

  • 10
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

魏鹭千Peacemaker

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

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

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

打赏作者

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

抵扣说明:

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

余额充值