简体   繁体   English

常量文件的类vs模块

[英]Class vs module for constants file

I have a large number of constants: the average length of a domestic flight, the co2 emissions of different diet types, and co2/kwH conversions for different states, etc. I'd thought to put them in a separate file in my models and controllers, but wasn't quite sure what the best practice would be. 我有很多常数:国内航班的平均时长,不同饮食类型的CO2排放量以及不同州的CO2 / kwH转化率,等等。控制器,但不确定最佳做法是什么。 Should this file be a class, a module, or neither? 该文件应该是类,模块还是两者都不是? Should I place them in the models directory? 我应该将它们放置在models目录中吗?

Examples: 例子:

class CalculatorConstants
  AVERAGE_DOMESTIC_FLIGHT_LENGTH = 1070
  class DietType
    def initialize(name, co2e)
      @name = name
      @co2e = co2e
    end
    attr_reader (:name, :co2e)
  end

  average_diet = DietType.new('average', 684.915)
  meaty = DietType.new('meaty', 867.964)
  no_beef = DietType.new('no beef', 450.585)
  pescatarian = DietType.new('pescatarian', 612.074)
  vegetarian = DietType.new('vegetarian', 420.482)
  vegan = DietType.new('vegan', 245.793)
  DIET_TYPES = [average_diet, meaty, no_beef, pescatarian, vegetarian, vegan]
end

or 要么

module CalculatorConstants
  #Above Code
end

or neither? 还是都不? Where would be the best to put them? 放在哪里最好?

I would prefer a module as namespace for the constants. 我更喜欢将模块作为常量的名称空间。

module CalculatorConstants
  AVERAGE_DOMESTIC_FLIGHT_LENGTH = 1070
  DietType = Struct.new(:name, :co2e)

  AVERAGE_DIET = DietType.new('average', 684.915)
  # ...
  DIET_TYPES = [AVERAGE_DIET]
end

p CalculatorConstants::DIET_TYPES

I usually use the config gem to manage constants. 我通常使用config gem来管理常量。 It is very easy to use: just store all constants to a YAML file, then these constants will be available in your app. 它非常易于使用:只需将所有常量存储到YAML文件中,然后这些常量将在您的应用程序中可用。 For example, 例如,

# config/settings.yml
flight_length: 1070
diet_types:
  average: 684.915
  meaty: 867.964

You can access those constants in your app by 您可以通过以下方式在应用程序中访问这些常量

Settings.flight_length # => 1070
Settings.diet_types.average # => 684.915

I usually solve such problems by storing the constants in then yaml file, and then load them and access them through a hash. 我通常通过在yaml文件中存储常量,然后加载它们并通过哈希访问它们来解决此类问题。

example of the yaml file: yaml文件的示例:

flight_length:
  domestic: 1070
  international: 3000
diet_types:
  - type1
  - type2
  - type3

Ruby code example: Ruby代码示例:

#loading
constants = YAML.load_file(path)

#saving
File.open(path, 'w') { |f| YAML.dump(constants, f) }

#use
puts constants[:flight_length].inspect
# => { domestic: 1070, international: 3000 }

puts constants[:flight_length][:domestic]
# => 1070

puts constants[:diet_types].inspect
# => [type1, type2, type3]

It is nicely structured, it is independent from the ruby app, it can easily be edited outside the app context, it can be easily reused or converted, for example to json for use in javascript code. 它结构良好,独立于ruby应用程序,可以在应用程序上下文之外轻松进行编辑,可以轻松地重用或转换,例如,转换为json以在javascript代码中使用。

You could wrap the methods for accessing the values in the yaml file, and various helper either as class methods of a class or instance methods, depending on your needs. 您可以根据需要将访问值的方法包装在yaml文件中,并将各种帮助程序包装为类的类方法或实例方法。 If you will just load and save, then perhaps class methods are better. 如果只加载和保存,则类方法可能更好。 If you would have many helper methods, then instance methods are better. 如果您有很多辅助方法,那么实例方法会更好。 I would not go with a module. 我不会选择模块。

Official yaml web site yaml官方网站

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM