listview 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/listview.rb +50 -0
  2. metadata +45 -0
data/lib/listview.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'date'
2
+
3
+ class DateTime
4
+ def to_i
5
+ seconds_since_unix_epoch.to_i
6
+ end
7
+
8
+ private
9
+ def seconds_since_unix_epoch
10
+ seconds_per_day = 86_400
11
+ (self - ::DateTime.civil(1970)) * seconds_per_day
12
+ end
13
+ end
14
+
15
+ class ListView
16
+
17
+ class << self
18
+ def get_events vars
19
+ get_events_that_havent_happened_yet(vars[:events], vars[:events_per_page], (vars[:page].to_i)*vars[:events_per_page])
20
+ end
21
+ private
22
+ def get_events_that_havent_happened_yet events, number_of_events_per_page, offset=0
23
+ events_that_havent_happened_yet = events
24
+ events_that_havent_happened_yet.sort_by! {|event| event.date}
25
+ closest_to_today = get_event_closed_to_today(events_that_havent_happened_yet)
26
+ return [] if closest_to_today.nil?
27
+
28
+ start_event_index = [closest_to_today + offset, 0].max
29
+ return events_that_havent_happened_yet[start_event_index, number_of_events_per_page]
30
+ end
31
+
32
+ def get_event_closed_to_today events
33
+ today = DateTime.now
34
+ closest_to_today = nil
35
+ events.each_with_index do |event, index|
36
+ if event.date.to_i - today.to_i > 0
37
+ if closest_to_today == nil
38
+ closest_to_today = index
39
+ else
40
+ if event.date.to_i - today.to_i < events[closest_to_today].date.to_i - today.to_i
41
+ closest_to_today = index
42
+ end
43
+ end
44
+ end
45
+ end
46
+ return closest_to_today
47
+ end
48
+ end
49
+
50
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: listview
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zoltan Kiss
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'Example: http://freefoodumn.com/view_by_list'
15
+ email: kissx012@umn.edu
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/listview.rb
21
+ homepage: http://rubygems.org/gems/listview
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Show stored events in chronological order, like http://freefoodumn.com/view_by_list
45
+ test_files: []