carender 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 88e0c15f59567a53434dd82e22782af60a5a39f49fc3bf83a972dfb05e954b69
4
- data.tar.gz: efbe7f96ea6f269c3f5675337b002889496919f44a5c2ecdf7fcc6f91e3b81eb
3
+ metadata.gz: 7379eda822195f2ef52d2ac57b570d17377e657cd3d04d6c255444c5e9ee3cdc
4
+ data.tar.gz: de4daaf6023e59a2a1de788bdbcfd8797277afaa5609427625cc80bddfdd50c2
5
5
  SHA512:
6
- metadata.gz: 47d6ca78188311e16aed2bfbf22435403f6f8e2db8180c0bafe8e341603717e7eef957627f92e870653c56ce17fdefcf97bea57de2c3efcd8f6d30b31db888b1
7
- data.tar.gz: af6874f2e6600f1ecc3ed6295cd9718f270dd53c00dd5ddfe0e237056ef0c0c4f01afe97564287df86252d0b7d3c940a9e7c832ceaa9f2267aa4521936eed3b8
6
+ metadata.gz: 508865b87955b57885f00126df9753a3221de3d2dcdfb39794686c900c5aab62178c560f04ed670797e1e03c72472a7b8794343e447747ce73e4766c610a98a1
7
+ data.tar.gz: 4b23d027758b708d5cc56163748b18b684225c556088625e857366b454e7f93612f373e6c536445d759e542fae63b072361fa3a535b7cf743add128977879ab2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 kami-zh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # Carender
2
2
 
3
- [WIP] A simple calendar gem for Rails application.
3
+ A simple calendar gem for Rails application.
4
4
 
5
- ## Synopsis
5
+ ## Usage
6
6
 
7
- This library automatically renders the calendar by reading `params[:year]` and `params[:month]`.
7
+ This library automatically renders the calendar as HTML table by reading `params[:year]` and `params[:month]`.
8
8
 
9
- A simple calendar:
9
+ A code of simple calendar:
10
10
 
11
11
  ```erb
12
12
  <%= carender do |date| %>
@@ -14,6 +14,20 @@ A simple calendar:
14
14
  <% end %>
15
15
  ```
16
16
 
17
+ Above code renders following table.
18
+ The string passed as a block is rendered in each cell.
19
+
20
+ ```html
21
+ <table>
22
+ <tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>
23
+ <tr><td></td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
24
+ <tr><td>7</td><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td></tr>
25
+ <tr><td>14</td><td>15</td><td>16</td><td>17</td><td>18</td><td>19</td><td>20</td></tr>
26
+ <tr><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td><td>26</td><td>27</td></tr>
27
+ <tr><td>28</td><td>29</td><td>30</td><td>31</td><td></td><td></td><td></td></tr>
28
+ </table>
29
+ ```
30
+
17
31
  With a collection which is grouped by specified column:
18
32
 
19
33
  ```erb
@@ -38,6 +52,10 @@ And then execute:
38
52
  $ bundle
39
53
  ```
40
54
 
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kami-zh/carender. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
58
+
41
59
  ## License
42
60
 
43
61
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,27 +1,10 @@
1
- begin
2
- require 'bundler/setup'
3
- rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
- end
6
-
7
- require 'rdoc/task'
8
-
9
- RDoc::Task.new(:rdoc) do |rdoc|
10
- rdoc.rdoc_dir = 'rdoc'
11
- rdoc.title = 'Carender'
12
- rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.md')
14
- rdoc.rdoc_files.include('lib/**/*.rb')
15
- end
16
-
17
1
  require 'bundler/gem_tasks'
18
-
19
2
  require 'rake/testtask'
20
3
 
21
4
  Rake::TestTask.new(:test) do |t|
22
5
  t.libs << 'test'
23
6
  t.pattern = 'test/**/*_test.rb'
24
- t.verbose = false
7
+ t.warning = false
25
8
  end
26
9
 
27
10
  task default: :test
@@ -1,5 +1,7 @@
1
1
  require 'carender/calendar'
2
- require 'carender/collection'
3
2
  require 'carender/core'
4
3
  require 'carender/helper'
5
- require 'carender/railtie'
4
+
5
+ if defined?(Rails)
6
+ require 'carender/railtie'
7
+ end
@@ -1,13 +1,16 @@
1
+ require 'date'
2
+ require 'active_support/core_ext/date'
3
+ require 'active_support/core_ext/time'
4
+
1
5
  module Carender
2
6
  class Calendar
3
- def initialize(year, month, collection)
7
+ def initialize(year, month)
4
8
  @year = year
5
9
  @month = month
6
- @collection = collection
7
10
  end
8
11
 
9
- def render(view_context, &block)
10
- html = <<-EOS
12
+ def render(view_context, collection, &block)
13
+ html = <<-HTML
11
14
  <table>
12
15
  <tr>
13
16
  <th>Sun</th>
@@ -18,10 +21,10 @@ module Carender
18
21
  <th>Fri</th>
19
22
  <th>Sat</th>
20
23
  </tr>
21
- EOS
24
+ HTML
22
25
 
23
26
  (date..date.end_of_month).each do |d|
24
- if d.wday == 0
27
+ if d.wday == 0 || (d == date && d.wday != 0)
25
28
  html += '<tr>'
26
29
  end
27
30
 
@@ -30,14 +33,18 @@ module Carender
30
33
  end
31
34
 
32
35
  html += '<td>'
33
- html += view_context.capture { block.call(d, collection[d] || []) }
36
+
37
+ if block
38
+ html += view_context.capture { block.call(d, collection[d]) }.to_s
39
+ end
40
+
34
41
  html += '</td>'
35
42
 
36
43
  if d == date.end_of_month
37
44
  (6 - d.wday).times { html += '<td></td>' }
38
45
  end
39
46
 
40
- if d.wday == 6
47
+ if d.wday == 6 || (d == date.end_of_month && d.wday != 6)
41
48
  html += '</tr>'
42
49
  end
43
50
  end
@@ -48,7 +55,7 @@ module Carender
48
55
 
49
56
  private
50
57
 
51
- attr_reader :year, :month, :collection
58
+ attr_reader :year, :month
52
59
 
53
60
  def date
54
61
  @date ||= Date.new(year, month)
@@ -2,16 +2,35 @@ module Carender
2
2
  class Core
3
3
  def initialize(view_context, options)
4
4
  @view_context = view_context
5
- @collection = Carender::Collection.new(*options.values_at(:collection, :column)).grouped
5
+ @collection = options[:collection] || []
6
+ @column = options[:column]
6
7
  end
7
8
 
8
9
  def render(&block)
9
- calendar = Carender::Calendar.new(*view_context.params.values_at(:year, :month).map(&:to_i), collection)
10
- calendar.render(view_context, &block).html_safe
10
+ calendar = Carender::Calendar.new(year, month)
11
+ calendar.render(view_context, grouped_collection, &block).html_safe
11
12
  end
12
13
 
13
14
  private
14
15
 
15
- attr_reader :view_context, :collection
16
+ attr_reader :view_context, :collection, :column
17
+
18
+ def year
19
+ view_context.params[:year].to_i
20
+ end
21
+
22
+ def month
23
+ view_context.params[:month].to_i
24
+ end
25
+
26
+ def grouped_collection
27
+ Hash.new([]).tap do |hash|
28
+ collection.each do |c|
29
+ date = c.send(column).to_date
30
+ hash[date] = [] if hash[date].empty?
31
+ hash[date] << c
32
+ end
33
+ end
34
+ end
16
35
  end
17
36
  end
@@ -1,3 +1,3 @@
1
1
  module Carender
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,31 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kami-zh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-17 00:00:00.000000000 Z
11
+ date: 2018-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 5.2.0.beta2
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionview
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
25
32
  - !ruby/object:Gem::Version
26
- version: 5.2.0.beta2
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
- name: sqlite3
42
+ name: oga
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - ">="
@@ -45,17 +59,15 @@ executables: []
45
59
  extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
- - MIT-LICENSE
62
+ - LICENSE.txt
49
63
  - README.md
50
64
  - Rakefile
51
65
  - lib/carender.rb
52
66
  - lib/carender/calendar.rb
53
- - lib/carender/collection.rb
54
67
  - lib/carender/core.rb
55
68
  - lib/carender/helper.rb
56
69
  - lib/carender/railtie.rb
57
70
  - lib/carender/version.rb
58
- - lib/tasks/carender_tasks.rake
59
71
  homepage: https://github.com/kami-zh/carender
60
72
  licenses:
61
73
  - MIT
@@ -1,20 +0,0 @@
1
- Copyright 2018 kami-zh
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,25 +0,0 @@
1
- module Carender
2
- class Collection
3
- def initialize(collection, column)
4
- @collection = collection || []
5
- @column = column
6
- @grouped = {}
7
- end
8
-
9
- def grouped
10
- return @grouped unless @grouped.empty?
11
-
12
- collection.each do |c|
13
- date = c.send(column).to_date
14
- @grouped[date] = [] unless @grouped[date]
15
- @grouped[date] << c
16
- end
17
-
18
- @grouped
19
- end
20
-
21
- private
22
-
23
- attr_reader :collection, :column
24
- end
25
- end
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :carender do
3
- # # Task goes here
4
- # end