1

I'm using RubyMotion, ProMotion, and MotionModel for an iOS app. We'll be storing multiple date/time sensitive entries per day and listing them in a large TableView. Right now the entries appear in one large list:

enter image description here

What I'd like to do is have entries segmented by date for better readability:

enter image description here

I assume I can get this working by looping through my model entries once, extracting an array of all unique dates, then run through the date array and extract entries that match. This would work, but I can't help but think there might be a better way to tackle this with some Ruby magic that I'm not familiar with. Any suggestions are much appreciated!

In case someone reading this knows Ruby or RubyMotion but not familiar with ProMotion, the data format I need to return is in this format:

[{
  title: "June 12, 2013",
  cells: [{
    title: "10:00am"
  },{
    title: "3:15pm"
  },{
    title: "7:00pm"
  },{
    title: "10:15pm"
  }]
},{
  title: "June 06, 2013",
  cells: [{
    title: "10:00am"
  },{
    title: "3:15pm"
  },{
    title: "7:00pm"
  },{
    title: "10:15pm"
  }]
}]

EDIT:

The current code that generates table cells in reverse chronological order:

[{
  cells: Entries.order{|one, two| two.entry_date <=> one.entry_date}.all.map{ |f|
    {
    title: Time.at(f.entry_date).strftime("%I:%M %p"), #"%m/%d/%Y"
    action: :show_entry,
    arguments: 
      {
        entry: f 
      }
    }
  }
}]
Denny
  • 319
  • 2
  • 8

1 Answers1

1

Assuming times has your array of Time instances:

df=NSDateFormatter.new.tap { |f| f.setDateStyle(NSDateFormatterMediumStyle) }
tf=NSDateFormatter.new.tap { |f| f.setTimeStyle(NSDateFormatterMediumStyle) }
times.inject({}) { |a, t|  (a[df.stringFromDate(t)] ||= []) << 
  tf.stringFromDate(t); a }.map { |k, v| Hash[title: k, cells: v.map { |t| {title: t} } ] }
aceofspades
  • 7,568
  • 1
  • 35
  • 48
  • Wow. Tried to make sense of that but I've got some learning to do I guess :) So I tried instead to update it to integrate info from my model and then I crashed things. I should've included it before but have now edited the post to show my current table generating code. Know of any way to integrate the two? And any resources you suggest to get up to speed on this advanced data mapping stuff? It's amazing to get help when I need it but I'd love to be able to understand what's going on as well. – Denny Jun 28 '13 at 03:28
  • Break it up into pieces, i.e. before the map call, and see what you get. Understanding how to use inject, map, and building hashes dynamically is time well spent when learning ruby. – aceofspades Jun 28 '13 at 14:34
  • EDIT: Never mind, he's already doing this. (previous) Yeah, you'll want to loop through the hash that @aceofspades creates here and then format your groups into an array of hashes accordingly. Something like `time_hash.each { |title, cells| { title: title, cells: cells } }` – Jamon Holmgren Jun 28 '13 at 19:18
  • Appreciate the help, guys. Got some studying to do but will catch on sooner or later :) – Denny Jun 28 '13 at 23:57