Browse Source

Move helper functions to a separate library

tags/v2.0.0
Adrian Short 6 years ago
parent
commit
bcf9b61b16
2 changed files with 51 additions and 51 deletions
  1. +1
    -51
      bin/build
  2. +50
    -0
      lib/helpers.rb

+ 1
- 51
bin/build View File

@@ -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)


+ 50
- 0
lib/helpers.rb View File

@@ -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

Loading…
Cancel
Save