monthly_calendar 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 23e1dee51357a860d3f5549a3921f478e22cc88e
4
+ data.tar.gz: 5bab1c5bdad9709f2a2afefe63457682aa72ae5e
5
+ SHA512:
6
+ metadata.gz: ce183f403982f9fcc0a0196a8368fb5271728f7979bffdc53609501117e5a29d75e0b335eb7b680905b0f77dabbdd9a31dcf5d367046f191993c49fadc5b41f0
7
+ data.tar.gz: f733c759380f7f566e86fb08c4279a833b6024512d94fca5555c2b1da3f1a5be21be79c1c3c09357e6fe5eba849ac19293c1740c6dd2b4f035c3635a58a8994f
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at brandoncc@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Brandon Conway
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 ADDED
@@ -0,0 +1,69 @@
1
+ # Monthly Calendar Generator
2
+
3
+ This is a simple library which allows you to create monthly calendars which have
4
+ space to write notes.
5
+
6
+ It allows you to create PDF files like [this one](samples/calendar.pdf).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'monthly_calendar'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install monthly_calendar
23
+
24
+ ## Standard Usage
25
+
26
+ ```ruby
27
+ require 'monthly_calendar'
28
+
29
+ calendar = MonthlyCalendar.new(start_date: "January 2018", pages: 2)
30
+ calendar.save("calendar.pdf")
31
+ ```
32
+
33
+ This example is the exact code which was used to generate the sample calendar. The `start_date` value can be any string that `Date#parse` can parse, and defaults to `Date.today` if it isn't provided. `pages` is probably self explanatory, but that is how many months will be generated, starting with `start_date`.
34
+
35
+ ## Advanced Usage
36
+
37
+ The API offers a `#to_s` method which will return the pdf as a string (using [Prawn::Document#render](http://prawnpdf.org/docs/0.11.1/Prawn/Document.html#method-i-render)).
38
+
39
+ The API also offers a `#stream` method which will yield the pdf as a string to the block you supply.
40
+
41
+ ```ruby
42
+ require 'monthly_calendar'
43
+
44
+ calendar = MonthlyCalendar.new(start_date: "January 2018", pages: 2)
45
+ calendar.stream do |calendar_string|
46
+ puts calendar_string
47
+ end
48
+
49
+ # => %PDF-1.3
50
+ # => %����
51
+ # => 1 0 obj
52
+ # => << /Creator <feff0050007200610077006e>
53
+ # => /Producer <feff0050007200610077006e>
54
+ # ...
55
+ ```
56
+
57
+ These methods will allow you to easily do something like render the value as a response from a web server.
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/brandoncc/monthly_calendar. 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.
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
66
+
67
+ ## Code of Conduct
68
+
69
+ Everyone interacting in the MonthlyCalendar project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/brandoncc/monthly_calendar/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "monthly_calendar"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,105 @@
1
+ require "monthly_calendar/version"
2
+ require "date"
3
+ require "prawn"
4
+ require "prawn/measurement_extensions"
5
+ require_relative "./ui"
6
+
7
+ class MonthlyCalendar
8
+ include UI
9
+
10
+ attr_reader :pdf
11
+
12
+ def initialize(options = {})
13
+ @start_date = (options[:start_date] && Date.parse(options[:start_date])) ||
14
+ Date.today
15
+ @pages_count = options[:pages] || 1
16
+
17
+ @pdf = Prawn::Document.new(page_layout: :landscape, top_margin: 1.in, skip_page_creation: true)
18
+ create
19
+ end
20
+
21
+ def save(file_name = "calendar.pdf")
22
+ pdf.render_file File.expand_path(file_name)
23
+ end
24
+
25
+ def stream
26
+ yield(to_s)
27
+ end
28
+
29
+ def to_s
30
+ pdf.render
31
+ end
32
+
33
+ private
34
+
35
+ def create
36
+ calendar_contents.each do |(month, weeks)|
37
+ pdf.start_new_page
38
+
39
+ days_in_month = weeks.flatten.compact.max
40
+ first_day_of_week = weeks[0].index(1)
41
+
42
+ weeks_count = ((days_in_month + first_day_of_week) / 7.0).ceil
43
+
44
+ pdf.define_grid(rows: weeks_count, columns: 7)
45
+
46
+ draw_header(month)
47
+
48
+ weeks.each_with_index do |week, week_index|
49
+ draw_notes(week_index,
50
+ find_nil_cells(week),
51
+ weeks_count == 6 ? :close : :far)
52
+
53
+ week.each_with_index do |day, day_index|
54
+ draw_day_square(week_index, day_index, day)
55
+ end
56
+ end
57
+
58
+ # The last thing drawn on each page isn't rendered for some reason so this
59
+ # just "flushes the queue" (causes the last thing previous to be drawn)
60
+ pdf.stroke
61
+ end
62
+ end
63
+
64
+ def calendar_contents
65
+ months = []
66
+
67
+ current_month = @start_date
68
+
69
+ @pages_count.times do |page_index|
70
+ month_text = current_month.strftime("%B %Y")
71
+
72
+ days_in_month = Date.new(current_month.year, current_month.month, -1).mday
73
+ first_day_of_week = Date.new(current_month.year, current_month.month, 1).wday
74
+
75
+ weeks_count = ((days_in_month + first_day_of_week) / 7.0).ceil
76
+
77
+ days = []
78
+ days << [] until days.count == weeks_count
79
+
80
+ first_day_of_week.times do
81
+ days[0] << nil
82
+ end
83
+
84
+ days_in_month.times do |day|
85
+ week = (day + first_day_of_week) / 7
86
+ days[week] << day + 1
87
+ end
88
+
89
+ days[4] << nil until days[4].count == 7
90
+
91
+ unless days[5].nil?
92
+ days[5] << nil until days[5].count == 7
93
+ end
94
+
95
+ months << [month_text, days]
96
+ current_month = current_month.next_month
97
+ end
98
+
99
+ months
100
+ end
101
+
102
+ def find_nil_cells(week)
103
+ { start: week.index(nil), count: week.count(nil) }
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ class MonthlyCalendar
2
+ VERSION = "0.2.3"
3
+ end
data/lib/ui.rb ADDED
@@ -0,0 +1,67 @@
1
+ module UI
2
+ private
3
+
4
+ def draw_day_square(week_index, day_index, day_text)
5
+ return unless day_text
6
+
7
+ pdf.grid([week_index, day_index], [week_index, day_index]).bounding_box do
8
+ pdf.stroke_bounds
9
+
10
+ pdf.rectangle [0, pdf.bounds.height], 15, 15
11
+ pdf.text_box day_text.to_s,
12
+ at: [0, pdf.bounds.height],
13
+ height: 15,
14
+ width: 15,
15
+ size: 8,
16
+ align: :center,
17
+ valign: :center
18
+ end
19
+ end
20
+
21
+
22
+ def draw_header(month)
23
+ pdf.rectangle [0, pdf.bounds.height + 0.5.in], pdf.bounds.width, 0.5.in
24
+ pdf.text_box month,
25
+ at: [0, pdf.bounds.height + 0.5.in],
26
+ width: pdf.bounds.width,
27
+ height: 0.25.in,
28
+ align: :center,
29
+ valign: :center
30
+
31
+ weekdays = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
32
+ weekdays.each_with_index do |weekday, index|
33
+ width = pdf.bounds.width / 7
34
+ left = width * index
35
+ pdf.rectangle [left, pdf.bounds.height + 0.25.in], width, 0.25.in
36
+ pdf.text_box weekday,
37
+ at: [left, pdf.bounds.height + 0.23.in],
38
+ height: 15,
39
+ width: width,
40
+ size: 8,
41
+ align: :center,
42
+ valign: :center
43
+ end
44
+ end
45
+
46
+ def draw_notes(row, nil_cells, lines_distance)
47
+ return if nil_cells[:count].zero?
48
+
49
+ pdf.grid([row, nil_cells[:start]],
50
+ [row, nil_cells[:start] + nil_cells[:count] - 1]).bounding_box do
51
+ pdf.stroke_bounds
52
+
53
+ pdf.draw_text "Notes",
54
+ at: [10, pdf.bounds.height - 12],
55
+ size: 5
56
+
57
+ starting_line_top = 10
58
+ line_position_increment = lines_distance == :close ? 17 : 22
59
+
60
+ 4.times do |i|
61
+ top = starting_line_top + (i * line_position_increment)
62
+ pdf.line [10, top],
63
+ [pdf.bounds.width - 10, top]
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "monthly_calendar/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "monthly_calendar"
8
+ spec.version = MonthlyCalendar::VERSION
9
+ spec.authors = ["Brandon Conway"]
10
+ spec.email = ["brandoncc@gmail.com"]
11
+
12
+ spec.summary = %q{A simple monthly calendar generator with space for notes}
13
+ spec.homepage = "http://github.com/brandoncc/monthly_calendar"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.15"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_dependency "prawn", "~> 2.2"
27
+ end