calendar_builder 0.0.1.rc

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ .DS_Store
2
+ .*.swp
3
+ *.orig
4
+ *.cp
5
+ *~
6
+ *.gem
7
+ *.rbc
8
+ .bundle
9
+ .config
10
+ .yardoc
11
+ .rvmrc
12
+ Gemfile.lock
13
+ InstalledFiles
14
+ _yardoc
15
+ coverage
16
+ doc/
17
+ lib/bundler/man
18
+ pkg
19
+ rdoc
20
+ spec/reports
21
+ test/tmp
22
+ test/version_tmp
23
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in calendar_builder.gemspec
4
+ gemspec
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :cli => '--color' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/calendar_builder/(.+)\.rb$}) { |m| "spec/calendar_builder/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Marcos G. Zimmermann
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ CalendarBuilder
2
+ ===============
3
+
4
+ Simple calendar helper
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'calendar_builder'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install calendar_builder
19
+
20
+ ## Usage
21
+
22
+ ~> In development
23
+
24
+ ## Reporting Bugs / Feature Requests
25
+ Please [open an Issue on GitHub](https://github.com/marcosgz/calendar_builder/issues) if you have feedback, new feature requests, or want to report a bug. Thank you!
26
+
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.pattern = "spec/calendar_builder/**/*_spec.rb"
7
+ end
8
+
9
+
10
+ task :default => :spec
11
+ task :test => :spec
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/calendar_builder/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Marcos G. Zimmermann"]
6
+ gem.email = ["mgzmaster@gmail.com"]
7
+ gem.description = %q{Simple calendar helper}
8
+ gem.summary = %q{Simple calendar}
9
+ gem.homepage = "http://github.com/marcosgz/calendar_builder"
10
+ gem.license = %q{MIT}
11
+
12
+ gem.add_development_dependency "rspec"
13
+ gem.add_development_dependency "guard-rspec"
14
+
15
+
16
+ gem.files = `git ls-files`.split($\)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.name = "calendar_builder"
20
+ gem.require_paths = ["lib"]
21
+ gem.version = CalendarBuilder::VERSION
22
+ end
@@ -0,0 +1,75 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rubygems'
3
+ require 'date'
4
+
5
+ require "calendar_builder/version"
6
+ require "calendar_builder/calendar"
7
+ require "calendar_builder/calendar_date"
8
+
9
+ require 'calendar_builder/railtie' if defined?(Rails::Railtie)
10
+
11
+ module CalendarBuilder
12
+ def self.build(*args, &block)
13
+ calendar = Calendar.new(*args)
14
+ block.arity < 1 ? calendar.instance_eval(&block) : block.call(calendar) if block_given?
15
+ return calendar
16
+ end
17
+
18
+
19
+ def self.render_table(*args, &block)
20
+ options = args.last.is_a?(::Hash) ? args.pop : {}
21
+
22
+ [].tap do |html|
23
+ html << "<table class=\"#{ [options[:table_class] || 'calendar'].flatten.compact.join(' ') }\">"
24
+
25
+ Calendar.new(*args) do |calendar|
26
+ # Setup weekstart
27
+ calendar.week_start = options[:week_start] if options.has_key?(:week_start)
28
+ # Table Header
29
+ html << '<thead>'
30
+ unless options[:hide_month_names]
31
+ html << '<tr class="month_names">'
32
+ html << "<th class=\"previous #{ Date::MONTHNAMES[calendar.calendar_date.prev_month].to_s.downcase }\" colspan=\"2\">#{ options[:previous_month] }</th>"
33
+ html << "<th class=\"current #{ Date::MONTHNAMES[calendar.calendar_date.month].to_s.downcase }\" colspan=\"3\">#{ Date::MONTHNAMES[calendar.calendar_date.month] }</th>"
34
+ html << "<th class=\"next #{ Date::MONTHNAMES[calendar.calendar_date.next_month].to_s.downcase }\" colspan=\"2\">#{ options[:next_month] }</th>"
35
+ html << '</tr>'
36
+ end
37
+ unless options[:hide_weekday_names]
38
+ html << '<tr class="weekday_names">'
39
+ calendar.weekday_names("%a").each do |key, value|
40
+ html << "<th class=\"#{ key.downcase }\" scope=\"col\"><abbr title=\"#{ key }\">#{value}</abbr></th>"
41
+ end
42
+ html << '</tr>'
43
+ end
44
+ html << '</thead>'
45
+ # Table Body
46
+ html << "<tbody>"
47
+ arr = calendar.dates
48
+ # calendar.dates.in_groups_of(7, false)
49
+ (0..arr.size/7 - 1).collect{|i| arr[i*7, 7]}.each do |dates|
50
+ tr_classes = ['week']
51
+ dates.each do |d|
52
+ tr_classes << 'current_week' if d.week == calendar.calendar_date.week
53
+ end
54
+ html << "<tr class=\"#{tr_classes.uniq.join(' ')}\">"
55
+ dates.each do |d|
56
+ td_classes = ['day']
57
+ td_classes << 'prev_month' if d.prev_month?
58
+ td_classes << 'next_month' if d.next_month?
59
+ td_classes << 'otherMonth' if d.prev_month? || d.next_month?
60
+ td_classes << 'weekend' if d.weekend?
61
+ td_classes << 'current' if d.current?
62
+ td_classes << 'today' if d.today?
63
+ html << "<td class=\"#{td_classes.join(' ')}\">"
64
+ html << (block_given? ? block.call(d) : "<span>#{d.day}</span>")
65
+ html << "</td>"
66
+ end
67
+ html << "</tr>"
68
+ end
69
+ html << "</tbody>"
70
+ end
71
+ html << "</table>"
72
+ end.join("\n")
73
+
74
+ end
75
+ end
@@ -0,0 +1,83 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module CalendarBuilder
3
+ class Calendar
4
+ attr_reader :date
5
+
6
+ def initialize(*args, &block)
7
+ @date = Date.new(*args.map(&:to_i).reject{|x| x.zero? })
8
+ block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
9
+ self
10
+ end
11
+
12
+
13
+ # :sunday, :monday
14
+ def week_start=(val='Sunday')
15
+ @week_start = \
16
+ case val.class.name
17
+ when 'String', 'Symbol'
18
+ Date::DAYNAMES[0,2].each_with_index.select do |day_name, index|
19
+ day_name =~ /^#{val.to_s}$/i
20
+ end.map{|d, i| i }.first
21
+ when 'Fixnum'
22
+ val if [0, 1].include?(val)
23
+ end || 0
24
+ end
25
+
26
+
27
+ def week_start
28
+ @week_start || 0
29
+ end
30
+
31
+
32
+ def calendar_date
33
+ @calendar_date ||= CalendarDate.new(self, self.date)
34
+ end
35
+
36
+ def first_date
37
+ @first_date ||= begin
38
+ y = self.date.year
39
+ m = self.date.month
40
+ d = (1..31).detect{|x| Date.valid_date?(y, m, x)}
41
+ fd = Date.new(y, m, d)
42
+ fd -= (fd.jd - self.week_start+1) % 7
43
+ fd
44
+ end
45
+ end
46
+
47
+
48
+ def last_date
49
+ @last_date ||= begin
50
+ y = self.date.year
51
+ m = self.date.month
52
+ d = (1..31).to_a.reverse.detect{|x| Date.valid_date?(y, m, x)}
53
+ ld = Date.new(y, m, d)
54
+ ld += (ld.wday != week_start ? 6-(ld.wday-week_start) : 0)
55
+ ld
56
+ end
57
+ end
58
+
59
+
60
+ def all_dates
61
+ (first_date..last_date).to_a
62
+ end
63
+
64
+
65
+ def dates(&block)
66
+ self.all_dates.map do |date|
67
+ obj = CalendarDate.new(self, date)
68
+ block.arity < 1 ? obj.instance_eval(&block) : block.call(obj) if block_given?
69
+ obj
70
+ end
71
+ end
72
+
73
+
74
+ # %a - The abbreviated weekday name ("Sun")
75
+ # %A - The full weekday name ("Sunday")
76
+ def weekday_names(str='%a')
77
+ all_dates[0, 7].inject({}) do |r, x|
78
+ r.merge Hash[x.strftime('%A'), x.strftime(str.to_s)]
79
+ end
80
+ end
81
+ end
82
+
83
+ end
@@ -0,0 +1,67 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module CalendarBuilder
4
+ class CalendarDate
5
+ attr_accessor :calendar, :date
6
+
7
+ def initialize(calendar, date)
8
+ @calendar = calendar
9
+ @date = date
10
+ end
11
+
12
+ def year
13
+ date.year
14
+ end
15
+
16
+ def month
17
+ date.month
18
+ end
19
+
20
+ def week
21
+ date.strftime( calendar.week_start==1 ? '%W' : '%U' ).to_i
22
+ end
23
+
24
+ # 0 = Sunday, 6 = Saturday
25
+ def weekend?
26
+ [0, 6].include?(date.wday)
27
+ end
28
+
29
+ def day
30
+ date.day
31
+ end
32
+
33
+ # Prev/Next
34
+ def next_month
35
+ self.month == 12 ? 1 : (self.month + 1)
36
+ end
37
+
38
+ def prev_month
39
+ self.month == 1 ? 12 : (self.month - 1)
40
+ end
41
+
42
+ def prev_month?
43
+ self.calendar_date.prev_month == self.month
44
+ end
45
+
46
+ def current_month?
47
+ self.calendar_date.month == self.month
48
+ end
49
+
50
+ def next_month?
51
+ self.calendar_date.next_month == self.month
52
+ end
53
+
54
+ def current?
55
+ self.calendar_date.date == self.date
56
+ end
57
+
58
+ def today?
59
+ self.date == Date.today
60
+ end
61
+
62
+ def calendar_date
63
+ calendar.calendar_date
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,8 @@
1
+ require 'calendar_builder/view_helpers'
2
+ module CalendarBuilder
3
+ class Railtie < Rails::Railtie
4
+ initializer "calendar_builder.view_helpers" do
5
+ ActionView::Base.send :include, ViewHelpers
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module CalendarBuilder
3
+ VERSION = "0.0.1.rc"
4
+ end
@@ -0,0 +1,7 @@
1
+ module CalendarBuilder
2
+ module ViewHelpers
3
+ def render_calendar_table(*args, &block)
4
+ CalendarBuilder.render_table(*args, &block).html_safe
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe CalendarBuilder do
4
+ pending :build
5
+
6
+ pending :render_table
7
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe CalendarBuilder::CalendarDate do
4
+ pending :year
5
+
6
+ pending :month
7
+
8
+ pending :week
9
+
10
+ pending :weekend?
11
+
12
+ pending :day
13
+
14
+ pending :next_month
15
+
16
+ pending :prev_month
17
+
18
+ pending :prev_month?
19
+
20
+ pending :current_month?
21
+
22
+ pending :next_month?
23
+
24
+ pending :current?
25
+
26
+ pending :today?
27
+
28
+ pending :calendar_date
29
+
30
+ end
@@ -0,0 +1,150 @@
1
+ require 'spec_helper'
2
+
3
+ describe CalendarBuilder::Calendar do
4
+ describe :week_start do
5
+ let(:model) { CalendarBuilder::Calendar.new(Date.today.year, Date.today.mon)}
6
+ context "integer" do
7
+ [0, 1].each do |val|
8
+ it "allows '#{val}'" do
9
+ model.week_start = val
10
+ expect(model.week_start).to eq(val)
11
+ end
12
+ end
13
+
14
+ (2..6).each do |val|
15
+ it "defaults 0 with '#{val}'" do
16
+ model.week_start = val
17
+ expect(model.week_start).to eq(0)
18
+ end
19
+ end
20
+
21
+ it "defaults 0 with invalid values" do
22
+ model.week_start = 7
23
+ expect(model.week_start).to eq(0)
24
+ end
25
+ end
26
+
27
+ context "string" do
28
+ Date::DAYNAMES[0..1].map(&:downcase).each_with_index do |val, num|
29
+ it "allows '#{val}'" do
30
+ model.week_start = val
31
+ expect(model.week_start).to eq(num)
32
+ end
33
+ end
34
+
35
+ Date::DAYNAMES[2..-1].map(&:downcase).each_with_index do |val, num|
36
+ it "defaults 0 with '#{val}'" do
37
+ model.week_start = val
38
+ expect(model.week_start).to eq(0)
39
+ end
40
+ end
41
+
42
+ it "defaults 0 with invalid values" do
43
+ model.week_start = 'Foo'
44
+ expect(model.week_start).to eq(0)
45
+ end
46
+ end
47
+ end
48
+
49
+
50
+ describe :date do
51
+ context "without args" do
52
+ subject { CalendarBuilder::Calendar.new.date }
53
+ it { should eq(Date.new)}
54
+ end
55
+
56
+ context "only year" do
57
+ subject { CalendarBuilder::Calendar.new(2013).date }
58
+ it { should eq(Date.new(2013))}
59
+ end
60
+
61
+ context "year and month" do
62
+ subject { CalendarBuilder::Calendar.new(2013, 1).date }
63
+ it { should eq(Date.new(2013, 1))}
64
+ end
65
+
66
+ context "year, month and day" do
67
+ subject { CalendarBuilder::Calendar.new(2013, 1, 31).date }
68
+ it { should eq(Date.new(2013, 1, 31))}
69
+ end
70
+
71
+ context "String arguments" do
72
+ subject { CalendarBuilder::Calendar.new("2013", "1", "31").date }
73
+ it { should eq(Date.new(2013, 1, 31))}
74
+ end
75
+ end
76
+
77
+
78
+ describe :first_date do
79
+ let(:model) { CalendarBuilder::Calendar.new(2013, 8, 26) }
80
+
81
+ context ":sunday week_start" do
82
+ before(:each) do
83
+ model.week_start = 'Sunday'
84
+ end
85
+ it { expect(model.first_date).to eq(Date.new(2013, 7, 28)) }
86
+ end
87
+
88
+ context ":monday week_start" do
89
+ before(:each) do
90
+ model.week_start = 'Monday'
91
+ end
92
+ it { expect(model.first_date).to eq(Date.new(2013, 7, 29)) }
93
+ end
94
+ end
95
+
96
+
97
+ describe :last_date do
98
+ describe "July 2013" do
99
+ let(:model) { CalendarBuilder::Calendar.new(2013, 7, 10) }
100
+
101
+ context ":sunday week_start" do
102
+ before(:each) do
103
+ model.week_start = 'Sunday'
104
+ end
105
+ it { expect(model.last_date).to eq(Date.new(2013, 8, 3)) }
106
+ end
107
+
108
+ context ":monday week_start" do
109
+ before(:each) do
110
+ model.week_start = 'Monday'
111
+ end
112
+ it { expect(model.last_date).to eq(Date.new(2013, 8, 4)) }
113
+ end
114
+ end
115
+
116
+ describe "August 2013" do
117
+ let(:model) { CalendarBuilder::Calendar.new(2013, 8, 10) }
118
+
119
+ context ":sunday week_start" do
120
+ before(:each) do
121
+ model.week_start = 'Sunday'
122
+ end
123
+ it { expect(model.last_date).to eq(Date.new(2013, 8, 31)) }
124
+ end
125
+
126
+ context ":monday week_start" do
127
+ before(:each) do
128
+ model.week_start = 'Monday'
129
+ end
130
+ it { expect(model.last_date).to eq(Date.new(2013, 9, 1)) }
131
+ end
132
+
133
+ end
134
+ end
135
+
136
+
137
+ describe :all_dates do
138
+ before(:each) do
139
+ @model = CalendarBuilder::Calendar.new(2013, 8)
140
+ @model.instance_variable_set(:@first_date, @first_date=Date.new(2013,8,1))
141
+ @model.instance_variable_set(:@last_date, @last_date=Date.new(2013,8,10))
142
+ end
143
+ subject { @model.all_dates}
144
+ it { should eq((@first_date..@last_date).to_a)}
145
+ end
146
+
147
+ pending :dates
148
+
149
+ pending :weekday_names
150
+ end
@@ -0,0 +1,5 @@
1
+ require "spec_helper"
2
+
3
+ describe CalendarBuilder do
4
+ it { expect(CalendarBuilder).to be_const_defined('VERSION') }
5
+ end
@@ -0,0 +1,6 @@
1
+ require "bundler"
2
+ Bundler.require :default, :development
3
+
4
+ RSpec.configure do |config|
5
+ # Configs
6
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: calendar_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.rc
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Marcos G. Zimmermann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: guard-rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Simple calendar helper
47
+ email:
48
+ - mgzmaster@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - Guardfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - calendar_builder.gemspec
60
+ - lib/calendar_builder.rb
61
+ - lib/calendar_builder/calendar.rb
62
+ - lib/calendar_builder/calendar_date.rb
63
+ - lib/calendar_builder/railtie.rb
64
+ - lib/calendar_builder/version.rb
65
+ - lib/calendar_builder/view_helpers.rb
66
+ - spec/calendar_builder/calendar_builder_spec.rb
67
+ - spec/calendar_builder/calendar_date_spec.rb
68
+ - spec/calendar_builder/calendar_spec.rb
69
+ - spec/calendar_builder/version_spec.rb
70
+ - spec/spec_helper.rb
71
+ homepage: http://github.com/marcosgz/calendar_builder
72
+ licenses:
73
+ - MIT
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>'
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.1
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.23
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Simple calendar
96
+ test_files:
97
+ - spec/calendar_builder/calendar_builder_spec.rb
98
+ - spec/calendar_builder/calendar_date_spec.rb
99
+ - spec/calendar_builder/calendar_spec.rb
100
+ - spec/calendar_builder/version_spec.rb
101
+ - spec/spec_helper.rb