Skip to content

Instantly share code, notes, and snippets.

@jonelf
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonelf/223638855633144d9799 to your computer and use it in GitHub Desktop.
Save jonelf/223638855633144d9799 to your computer and use it in GitHub Desktop.
Range cover example
require 'nokogiri'
xml = %{
<Employments>
<Employment>
<From>2013-01-01</From>
<To>1900-01-01</To>
<MainEmployment>0</MainEmployment>
</Employment>
<Employment>
<From>2014-11-01</From>
<To>2014-11-30</To>
<MainEmployment>1</MainEmployment>
</Employment>
</Employments>}
doc = Nokogiri::XML(xml)
employments = []
today = Date.today
ongoing = Date.new(1900, 01, 01)
doc.search('//Employment').each do |emp|
from = Date.strptime(emp.at('From').text, '%Y-%m-%d')
to = Date.strptime(emp.at('To').text, '%Y-%m-%d')
main = emp.at('MainEmployment').text
if to == ongoing
to = today + 1
end
if (from..to).cover?(today)
employments << {main: main, from: from, to: to}
end
end
current_employment = employments.empty? ? nil : employments.sort_by{|emp| emp[:main]}.reverse.first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment