ruby on rails 3.2 - How to write a function for the day AFTER Thanksgiving? Holiday gem, YAML files -
i'm writing app tracks time-off via date ranges, , part of calculations make sure exclude weekends , holidays. in order keep track of holidays i'm using forked version of holidays gem(https://github.com/alexdunae/holidays), editing recognize holidays company recognizes. fork: (https://github.com/rclark4/holidays).
so far has worked fine me, i've run problem day after thanksgiving, addition original gem. excerpt of yaml file looks like:
11: - name: thanksgiving week: 4 regions: [us] wday: 4 - name: day after thanksgiving week: 4 regions: [us] wday: 5
while works 2008-2012, doesn't work in 2013 (and won't every 6-7 years), because november starts on friday, right it's saying day after thanksgiving week before is. i've tried @ syntax of other holidays see if there precedent holidays happened day after floating holiday, couldn't find 1 (the closest thing found friday easter, easter has it's own separate method altogether). ideas on how write function this?
let me know if need more information!
shawn's answer correct supposed put region specific methods in original file. idea is, shouldn't need touch .rb files make new definition, .yaml files. lib/holidays.rb general code rules (like to_weekday_if_weekend, etc.), not rules specific region. know it's kind of confusing works if don't follow rules exactly, upcoming release (hopefully) summer change soon.
so in us.yaml november should be:
11: - name: thanksgiving week: 4 regions: [us] wday: 4 - name: day after thanksgiving function: day_after_thanksgiving(year) regions: [us]
and under methods in same file (where us_inauguration_day defined, @ least in master version) should be:
methods: us_inauguration_day: | # january 20, every fourth year, following presidential election def self.us_inauguration_day(year) year % 4 == 1 ? 20 : nil end day_after_thanksgiving: | def self.day_after_thanksgiving(year) date.civil(year,11,date.calculate_mday(year,11,:fourth,:thursday)+1) end
then run rake defs:build_all (run rake defs:manifest when add new region file) , pulled copy match against test, reference:
tests: | { date.civil(2008,11,27) => 'thanksgiving', date.civil(2008,11,28) => 'day after thanksgiving'}.each |date, name| assert_equal name, (holidays.on(date, :us)[0] || {})[:name] end
Comments
Post a Comment