简体   繁体   English

红宝石时间和变量的比较

[英]ruby comparison of time and variable

I have two models, being an Employee and a WorkingPattern. 我有两个模型,分别是Employee和WorkingPattern。 An instance of an Employee belongs_to an Working Pattern. Employee的一个实例属于工作模式。

The Working Pattern looks like this 工作模式如下所示

        :id => :integer,
      :name => :string,
       :mon => :boolean,
       :tue => :boolean,
       :wed => :boolean,
       :thu => :boolean,
       :fri => :boolean,
       :sat => :boolean,
       :sun => :boolean

I need to know if an Employee should be at work today. 我需要知道某个员工今天是否应该上班。 So, if today is a Tuesday and that employee's working pattern record reports that :tue = true then return true, etc. I don't have the option of renaming the fields on the WorkingPattern model to match the days names. 因此,如果今天是星期二,并且该员工的工作模式记录报告::tue = true,然后返回true,等等。我无法选择重命名WorkingPattern模型上的字段以匹配日期名称。

I know that 我知道

Time.now.strftime("%A")

will return the name of the day. 将返回当天的名称。 Then I figured out I can get the first 3 characters of the string by doing 然后我发现我可以通过执行以下操作来获取字符串的前三个字符

Time.now.strftime("%A")[0,3]

so now I have "Tue" returned as a string. 所以现在我将“ Tue”作为字符串返回了。 Add in a downcase 加小写

Time.now.strftime("%A")[0,3].downcase

and now I have "tue", which matches the symbol for :tue on the WorkingPattern. 现在我有了“ tue”,它与WorkingPattern上的:tue符号匹配。

Now I need a way of checking the string against the correct day, ideally in a manner that doesn't mean 7 queries against the database for each employee! 现在,我需要一种根据正确的日期检查字符串的方法,理想情况下,这种方法不需要对每个员工的数据库进行7次查询!

Can anyone advise? 有人可以建议吗?

You can use %a for the abbreviated weekday name. 您可以将%a用作缩写的工作日名称。 And use send to dynamically invoke a method 并使用send动态调用方法

employee.working_pattern.send(Time.now.strftime("%a").downcase)

Use send to invoke a method, stored in a variable, on an object. 使用send可以调用对象中存储在变量中的方法。

Both of these are identical: 这两个都是相同的:

user.tue # true
user.send('tue') # true

You can access an attibute using [] , just like a Hash. 您可以使用[]来访问attibute,就像哈希一样。 No need to use send or even attributes . 无需使用send甚至attributes

day = Time.now.strftime("%a").downcase
employee.working_pattern[day]

Butchering strings makes me feel a little uneasy 弦乐使我感到不安

Construct a hash of day numbers: 构造日期数字的哈希:

{0 => :sun
 1 => :mon,
 2 => :tue,
  ...}

Then use Time.now.wday (or Time.zone.now.wday if you want to be timezone aware) to select the appropriate value from the hash 然后使用Time.now.wday (或Time.zone.now.wday如果您想知道时区)从哈希中选择适当的值

You can then use that string on employee.working_pattern in any of the ways described by the other answers: 然后,您可以通过其他答案描述的任何一种方式,在employee.working_pattern上使用该字符串:

employee.working_pattern.send(day_name)

employee.working_pattern.read_attribute[day_name] enployee.working_pattern[day_name] employee.working_pattern.read_attribute [day_name] enployee.working_pattern [day_name]

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

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