From bcf9b61b16ecf4a0946440889861a5d4ece493a6 Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Mon, 20 Aug 2018 13:26:02 +0100 Subject: [PATCH] Move helper functions to a separate library --- bin/build | 52 +------------------------------------------------- lib/helpers.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 51 deletions(-) create mode 100644 lib/helpers.rb diff --git a/bin/build b/bin/build index 14e28f0..60265dc 100755 --- a/bin/build +++ b/bin/build @@ -7,6 +7,7 @@ t_start = Process.clock_gettime(Process::CLOCK_MONOTONIC) require 'logger' require 'haml' require_relative '../models' +require_relative '../lib/helpers' OUTPUT_DIR = '_site' VIEWS_DIR = File.join('..', 'views') @@ -19,57 +20,6 @@ LAYOUT_FN = File.join(VIEWS_DIR, 'layout.haml') @pages = 0 -class String - def pluralize(num) - if num == 1 - return self - end - - case self[-1] - when 'y' - self[0..-2] + 'ies' - when 's' - self + "es" - else - self + "s" - end - end -end - -def commify(num) - num.to_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,'\1,').reverse -end - -# From http://snippets.dzone.com/posts/show/593 -def to_ordinal(num) - num = num.to_i - if (10...20) === num - "#{num}th" - else - g = %w{ th st nd rd th th th th th th } - a = num.to_s - c = a[-1..-1].to_i - a + g[c] - end -end - -def format_percent(num) - sprintf("%.0f%%", num) -end - -def short_date(d) - d.strftime("%e %b %Y") -end - -def long_date(d) - d.strftime("%e %B %Y") -end - -# Exception for Labour/Co-operative candidacies -def party_name(labcoop, party_name) - labcoop ? "Labour and Co-operative Party" : party_name -end - def write_page(path_items, template, locals = {}) dir = File.join(path_items) FileUtils.mkdir_p(dir) diff --git a/lib/helpers.rb b/lib/helpers.rb new file mode 100644 index 0000000..6429499 --- /dev/null +++ b/lib/helpers.rb @@ -0,0 +1,50 @@ +class String + def pluralize(num) + if num == 1 + return self + end + + case self[-1] + when 'y' + self[0..-2] + 'ies' + when 's' + self + "es" + else + self + "s" + end + end +end + +def commify(num) + num.to_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,'\1,').reverse +end + +# From http://snippets.dzone.com/posts/show/593 +def to_ordinal(num) + num = num.to_i + if (10...20) === num + "#{num}th" + else + g = %w{ th st nd rd th th th th th th } + a = num.to_s + c = a[-1..-1].to_i + a + g[c] + end +end + +def format_percent(num) + sprintf("%.0f%%", num) +end + +def short_date(d) + d.strftime("%e %b %Y") +end + +def long_date(d) + d.strftime("%e %B %Y") +end + +# Exception for Labour/Co-operative candidacies +def party_name(labcoop, party_name) + labcoop ? "Labour and Co-operative Party" : party_name +end