aboutsummaryrefslogtreecommitdiffstats
path: root/lib/public_body_csv.rb
blob: afb5d904303103c564b2a956b144d42991c20a1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'csv'

# Public: Generate a CSV representation of PublicBody instances
#
# Examples
#
#   bodies = PublicBody.search('useless')
#
#   csv = PublicBodyCSV.new(:fields => [:name, :calculated_home_page],
#                           :headers => ['Name', 'Home Page'])
#
#   bodies.each { |body| csv << body }
#
#   csv.generate
#   # => Name,Home Page
#        Department for Humpadinking,http://localhost
#        Ministry of Silly Walks,http://www.localhost
#        Department of Loneliness,http://localhost
class PublicBodyCSV

    def self.default_fields
        [:name,
         :short_name,
         :url_name,
         :tag_string,
         :calculated_home_page,
         :publication_scheme,
         :disclosure_log,
         :notes,
         :created_at,
         :updated_at,
         :version]
    end

    # TODO: Generate headers from fields
    def self.default_headers
        ['Name',
         'Short name',
         'URL name',
         'Tags',
         'Home page',
         'Publication scheme',
         'Disclosure log',
         'Notes',
         'Created at',
         'Updated at',
         'Version']
    end

    attr_reader :fields, :headers, :rows

    def initialize(args = {})
        @fields = args.fetch(:fields, self.class.default_fields)
        @headers = args.fetch(:headers, self.class.default_headers)
        @rows = []
    end

    def <<(public_body)
        # Allow join_rows to handle newlines because of differences between
        # CSV.generate_line in 1.8 / 1.9+
        if RUBY_VERSION.to_f >= 1.9
            rows << CSV.generate_line(collect_public_body_attributes(public_body), :row_sep => '')
        else
            rows << CSV.generate_line(collect_public_body_attributes(public_body))
        end
    end

    # TODO: Just use CSV.generate when Ruby 1.8.7 support is dropped
    def generate
        csv = generate_header_row
        csv << join_rows
        csv << "\n"
    end

    private

    def join_rows
        rows.join("\n")
    end

    def generate_header_row
        # Add a newline because of differences between
        # CSV.generate_line in 1.8 / 1.9+
        row = CSV.generate_line(headers)
        row += "\n" unless RUBY_VERSION.to_f >= 1.9
        row
    end

    def collect_public_body_attributes(public_body)
        fields.map do |field|
            public_body.respond_to?(field) ? public_body.send(field) : ''
        end
    end

end