Sha256: eb967488ab3987ae9fa9f2c3de249e650382998beefe09fc92a45771660d2a37

Contents?: true

Size: 1.69 KB

Versions: 5

Compression:

Stored size: 1.69 KB

Contents

require 'csv'

module Podpisy
  class Signatures
    def initialize(fn, **opts)
      @fn = fn
      @resolve_areas = opts[:areas] or false
      if @resolve_areas
        @kody = Postcodes.new 
      else
        @kody = nil
      end
      @area_types = [:m, :p, :w]
    end

    def remove_special_chars(s)
      s = s.gsub(/[~_]/, '-')
      s = s.gsub(/&/, ' i ')
      s = s.gsub(/[%${}^\\#]/, '')
      s
    end

    def process_row(row)
      row.headers.inject({}) do |m, h|
        if @resolve_areas && h == 'postcode'
          m['postcode'] = @kody.unambiguous(row[h], *@area_types)
        else
          m[h] = row[h]
        end
        m
      end
    end

    def to_csv(o = STDOUT)
      headers = nil
      CSV(o) do |out|
        CSV.foreach(@fn, headers: true, col_sep: ',') do |row|
          if headers.nil?
            headers = row.headers if headers.nil?
            out << headers
          end

          colvals = process_row(row)
          out << headers.map { |h| colvals[h] }
        end
      end
    end

    def first_last_name(row)
      fn = row['first_name'] || row['name'].split(' ')[0...-1].join(' ')
      ln = row['last_name'] || row['name'].split(' ')[-1]
      [fn, ln]
    end

    def to_tex(o = STDOUT)
      i = 1
      CSV.foreach(@fn, headers: true, col_sep: ',') do |row|
        if @resolve_areas
          city = @kody.unambiguous(row['postcode'], :m, :p, :w)
        else
          city = row['postcode']
        end
        fn, ln = first_last_name(row)
        f = remove_special_chars(fn || '').capitalize
        l = remove_special_chars(fn || '').capitalize
        o.puts "#{i}. & \\first{#{f}} \\last{#{l}} & \\city{#{city}} \\\\"
        i+=1
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
podpisy-0.1.6 lib/podpisy/signatures.rb
podpisy-0.1.5 lib/podpisy/signatures.rb
podpisy-0.1.4 lib/podpisy/signatures.rb
podpisy-0.1.3 lib/podpisy/signatures.rb
podpisy-0.1.2 lib/podpisy/signatures.rb