annotate 1-Ruby/acts_as_csv_module.rb @ 29:9c7af76fdbd0

Print isn't PrinLn, so add some \n characters to the output
author IBBoard <dev@ibboard.co.uk>
date Wed, 06 Sep 2017 18:36:02 +0100
parents 2ea3b5e9b41b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 #---
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 # Excerpted from "Seven Languages in Seven Weeks",
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 # published by The Pragmatic Bookshelf.
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 # Copyrights apply to this code. It may not be used to create training material,
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 # courses, books, articles, and the like. Contact us if you are in doubt.
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 # We make no guarantees that this code is fit for any purpose.
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 # Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 #---
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 module ActsAsCsv
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 def self.included(base)
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 base.extend ClassMethods
26
d50ff917c163 Change function calls so methods get patched in anyway
IBBoard <dev@ibboard.co.uk>
parents: 24
diff changeset
12 # The book doesn't do this, but it is possible to add the methods
d50ff917c163 Change function calls so methods get patched in anyway
IBBoard <dev@ibboard.co.uk>
parents: 24
diff changeset
13 # without having the inheriting class call a second function to do it
d50ff917c163 Change function calls so methods get patched in anyway
IBBoard <dev@ibboard.co.uk>
parents: 24
diff changeset
14 base.acts_as_csv
24
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 end
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 module ClassMethods
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 def acts_as_csv
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 include InstanceMethods
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 end
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 end
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22
27
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
23 module InstanceMethods
24
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 def read
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 @csv_contents = []
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 filename = self.class.to_s.downcase + '.txt'
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 file = File.new(filename)
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 @headers = file.gets.chomp.split(', ')
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 file.each do |row|
27
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
31 csv_row = CsvRow.new(headers, row.chomp.split(', '))
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
32 @csv_contents << csv_row
24
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 end
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 end
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 attr_accessor :headers, :csv_contents
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 def initialize
27
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
38 read
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
39 end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
40
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
41 def each(&block)
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
42 @csv_contents.each { |row| block.call row }
24
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 end
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 end
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 end
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 class RubyCsv # no inheritance! You can mix it in
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 include ActsAsCsv
26
d50ff917c163 Change function calls so methods get patched in anyway
IBBoard <dev@ibboard.co.uk>
parents: 24
diff changeset
49 # Also, nothing to do beyond the "include" - it automatically defines the methods!
24
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 end
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51
27
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
52 class CsvRow
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
53 attr_reader :values
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
54
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
55 def initialize(headers, values)
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
56 @values = Hash[headers.zip(values)]
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
57 end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
58
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
59 def method_missing(methId, *args)
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
60 str = methId.id2name
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
61 if @values.key?(str)
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
62 return @values[str]
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
63 else
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
64 super.method_missing(methId)
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
65 end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
66 end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
67 end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
68
24
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 m = RubyCsv.new
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 puts m.headers.inspect
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 puts m.csv_contents.inspect
27
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
72 # Show the "each" method
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
73 m.each { |row| puts row.inspect }
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
74 # Use method_missing to implement heading-as-a-method
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
75 puts m.csv_contents[0].country
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
76 # And show that we still get normal failure behaviour
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
77 puts m.csv_contents[0].Country
24
cd874e58dbc5 Add code from book
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78