你如何在 Rails 4 中存储自定义常量? [英] How do you store custom constants in Rails 4?

查看:29
本文介绍了你如何在 Rails 4 中存储自定义常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为电子邮件、比特消息等制作了一些正则表达式,并将它们作为常量放在

#config/initializers/regexps.rbREGEXP_EMAIL =/A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})z/iREGEXP_BITMESSAGE =/ABM-[a-zA-Z1-9&&[^OIl]]{32,34}z/

并像使用它一样

if @user.contact =~ REGEXP_EMAILelsif @user.contact =~ REGEXP_BITMESSAGE

这是好的做法吗?存储它们的最佳方式是什么?

解决方案

这是有道理的,这是可能的方法之一.这种方法的唯一缺点是常量会污染全局命名空间.

我通常更喜欢的方法是在应用程序命名空间内定义它们.

假设您的应用程序名为 Fooapp,那么您已经有一个由 Rails 定义的 Fooapp 模块(请参阅 config/application).>

我通常在 lib 中创建一个 fooapp.rb 文件,如下所示

模块Fooapp结尾

然后我将常量放在里面.还要确保在 application.rb 文件的底部需要它

需要'fooapp'

延迟加载文件在这种情况下不起作用,因为 Fooapp 模块已经定义.

当常量的数量足够大时,您可以将它们添加到一个单独的文件中,例如 /lib/fooapp/constants.rb.最后一步只是将所有常量分组到一个简单位置的微不足道的改进(尽管 Ruby 2.1 Frozen String 字面量改进 可能会让我删除几个常量.

还有一件事.在您的情况下,如果正则表达式特定于一个模型,您可以将其存储在模型本身中并创建一个模型方法

类用户REGEXP_EMAIL =/A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})z/iREGEXP_BITMESSAGE =/ABM-[123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ]{32,34}z/def contact_is_email?联系 =~ REGEXP_EMAIL结尾结尾

I made some regular expressions for email, bitmessage etc. and put them as constants to

#config/initializers/regexps.rb
REGEXP_EMAIL = /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})z/i
REGEXP_BITMESSAGE = /ABM-[a-zA-Z1-9&&[^OIl]]{32,34}z/

and use it like

if @user.contact =~ REGEXP_EMAIL
elsif @user.contact =~ REGEXP_BITMESSAGE

Is that's good practice? What's the best way to store them?

解决方案

It makes sense, that's one of the possible approaches. The only downside of this approach, is that the constants will pollute the global namespace.

The approach that I normally prefer is to define them inside the application namespace.

Assuming your application is called Fooapp, then you already have a Fooapp module defined by Rails (see config/application).

I normally create a fooapp.rb file inside lib like the following

module Fooapp
end

and I drop the constants inside. Also make sure to require it at the bottom of you application.rb file

require 'fooapp'

Lazy-loading of the file will not work in this case, because the Fooapp module is already defined.

When the number of constants become large enough, you can more them into a separate file, for example /lib/fooapp/constants.rb. This last step is just a trivial improvement to group all the constants into one simple place (I tend to use constants a lot to replace magic numbers or for optimization, despite Ruby 2.1 Frozen String literal improvements will probably let me remove several constants).

One more thing. In your case, if the regexp is specific to one model, you can store it inside the model itself and create a model method

class User

  REGEXP_EMAIL = /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})z/i
  REGEXP_BITMESSAGE = /ABM-[123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ]{32,34}z/

  def contact_is_email?
    contact =~ REGEXP_EMAIL
  end

end

这篇关于你如何在 Rails 4 中存储自定义常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆