Website/lib/nsfcp.rb

163 lines
5.3 KiB
Ruby

require "prawn"
require "prawn/measurement_extensions"
require "util.rb"
require "text.rb"
class NSFCnP
@@box_heights = [ 8.66.in, 7.085.in, 5.52.in, 3.97.in, 2.42.in ]
@@debug = false
def initialize(me, grants)
@me = me
@grants = grants.symbolize_json
end
def NSFCnP::commitment_string(record, category)
# pick out the first commitment field.
commitment =
record
.fetch(:commitment)
.fetch(category, 0)
.to_s
commitment =
case commitment
when String then
commitment
.gsub(/\u00BC/, ".25") # 1/4 symbol
.gsub(/\u00BD/, ".5") # 1/2 symbol
.gsub(/\u00BE/, ".75") # 3/4 symbol
.split(/, */)
when Number then ["#{commitment}"]
when Array then commitment
else raise "Invalid #{category} commitment for #{record[:title]}: '#{commitment}'"
end
(commitment.map { |x| x if x.to_f > 0 }.compact + [0]).first.to_s
end
def print
grants = @grants
pi_name = @me
grants.each do |record|
status = record.fetch(:status, record.fetch("status", :pending))
puts("Project/Proposal Title: #{record.fetch(:title)}")
puts("Status of Support: #{status}")
puts("Proposal/Award Number (if available): #{record.fetch(:agency_id, "n/a")}")
puts("Source of Support: #{record.fetch(:agency)}")
puts("Primary Place of Performance: University at Buffalo")
puts("Project/Proposal Start Date (MM/YYYY) (if available): #{record.fetch(:start)}")
puts("Project/Proposal End Date (MM/YYYY) (if available): #{record.fetch(:end)}")
puts("Total Award Amount (including Indirect Costs): $#{record.fetch(:amount)}")
puts("Person-Month(s) (or Partial Person-Months) Per Year Committed to the Project:")
record.fetch(:commitment)
.fetch(:by_year)
.to_a
.sort { |a, b| a[0] <=> b[0] }
.each do |year, amount|
puts(" #{year} : #{amount}")
end
puts("Overall Objectives: \n#{record.fetch(:summary)}\n\n")
# puts " * #{record.fetch(:title)} (#{status})"
# puts " #{record.fetch(:start)} -- #{record.fetch(:end)}; Calendar: #{NSFCnP::commitment_string(record, :calendar)} mo, School: #{NSFCnP::commitment_string(record, :schoolyear)} mo, Summer: #{NSFCnP::commitment_string(record, :summer)} mo"
# puts(" Submitted to #{record.fetch(:agency)}"+( then " (#{record.fetch(:agency_id)})" else "" end))
end
end
def render(target)
grants = @grants
pi_name = @me
Prawn::Font::AFM.hide_m17n_warning = true
Prawn::Document.generate(target,
background: "lib/NSFCP.png",
background_scale: 0.5
) do
stroke_color 'ff0000' if @@debug
grants.take_groups(@@box_heights.length)
.each.with_index do |page_records, page|
puts "Rendering Page #{page}"
start_new_page unless page == 0;
bounding_box([0.8.in, 8.85.in], width: 2.5.in, height: 0.2.in) do
text pi_name
stroke_bounds if @@debug
end
page_records.zip(@@box_heights.take(page_records.length)).
each do |record, height|
bounding_box([-0.17.in, height], width: 7.85.in, height: 1.59.in) do
stroke_bounds if @@debug
######## Support Type ########
offset =
case record.fetch(:status, record.fetch("status", :pending))
when :pending, "submitted" then 2.21.in
when :current, "accepted" then 1.16.in
when :planned, "planned" then 3.32.in
when :transfer then 6.07.in
else raise "Unknown status: #{record[:support]}"
end
# puts "Offset: #{record.fetch(:title)} -> #{offset}; #{record[:status]} -- #{record["status"]}"
### Make the checkbox ###
bounding_box([offset,1.57.in], width: 0.2.in, height: 0.2.in) do
text "X"
end
######## Title ########
bounding_box([0.1.in, 1.20.in], width: 7.6.in, height: 0.38.in) do
stroke_bounds if @@debug
text record.fetch(:title)
end
######## Agency ########
bounding_box([1.23.in, 0.8.in], width: 6.47.in, height: 0.19.in) do
stroke_bounds if @@debug
text record.fetch(:agency)
end
######## Amount ########
bounding_box([1.5.in, 0.6.in], width: 1.4.in, height: 0.19.in) do
stroke_bounds if @@debug
text TextUtils.format_money(record.fetch(:amount), currency: "")
end
######## Period ########
bounding_box([4.7.in, 0.6.in], width: 3.in, height: 0.19.in) do
stroke_bounds if @@debug
text "#{record.fetch(:start)} -- #{record.fetch(:end)}"
end
######## Location ########
bounding_box([1.23.in, 0.4.in], width: 6.47.in, height: 0.19.in) do
stroke_bounds if @@debug
text record.fetch(:location, "University at Buffalo, Buffalo, NY")
end
######## Commitment ########
commitment = record.fetch(:commitment)
bounding_box([4.4.in, 0.2.in], width: 0.6.in, height: 0.19.in) do
stroke_bounds if @@debug
text NSFCnP::commitment_string(record, :calendar), align: :center
end
bounding_box([5.45.in, 0.2.in], width: 0.55.in, height: 0.19.in) do
stroke_bounds if @@debug
text NSFCnP::commitment_string(record, :schoolyear), align: :center
end
bounding_box([6.4.in, 0.2.in], width: 1.in, height: 0.19.in) do
stroke_bounds if @@debug
text NSFCnP::commitment_string(record, :summer), align: :center
end
end
end
end
end
end
end