Mercurial > repos > other > SevenLanguagesInSevenWeeks
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 |
rev | line source |
---|---|
24 | 1 #--- |
2 # Excerpted from "Seven Languages in Seven Weeks", | |
3 # published by The Pragmatic Bookshelf. | |
4 # Copyrights apply to this code. It may not be used to create training material, | |
5 # courses, books, articles, and the like. Contact us if you are in doubt. | |
6 # We make no guarantees that this code is fit for any purpose. | |
7 # Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information. | |
8 #--- | |
9 module ActsAsCsv | |
10 def self.included(base) | |
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 | 15 end |
16 | |
17 module ClassMethods | |
18 def acts_as_csv | |
19 include InstanceMethods | |
20 end | |
21 end | |
22 | |
27
2ea3b5e9b41b
Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
26
diff
changeset
|
23 module InstanceMethods |
24 | 24 def read |
25 @csv_contents = [] | |
26 filename = self.class.to_s.downcase + '.txt' | |
27 file = File.new(filename) | |
28 @headers = file.gets.chomp.split(', ') | |
29 | |
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 | 33 end |
34 end | |
35 | |
36 attr_accessor :headers, :csv_contents | |
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 | 43 end |
44 end | |
45 end | |
46 | |
47 class RubyCsv # no inheritance! You can mix it in | |
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 | 50 end |
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 | 69 m = RubyCsv.new |
70 puts m.headers.inspect | |
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 | 78 |