Skip to content

Instantly share code, notes, and snippets.

@brikis98
Created February 1, 2012 05:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brikis98/1715263 to your computer and use it in GitHub Desktop.
Save brikis98/1715263 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks: Ruby, Day 3
module ActsAsCsv
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def acts_as_csv
include InstanceMethods
include Enumerable
end
end
module InstanceMethods
attr_accessor :headers, :csv_contents
def initialize
read
end
def read
@csv_contents = []
filename = self.class.to_s.downcase + '.csv'
file = File.new(filename)
@headers = parse_row file.gets
file.each do |row|
@csv_contents << CsvRow.new(@headers, parse_row(row))
end
end
def parse_row(row)
row.chomp.split(', ')
end
def each
@csv_contents.each { |row| yield row }
end
class CsvRow
def initialize(headers, row)
@headers = headers
@row = row
end
def respond_to?(sym)
@headers.index(name.to_s) || super(sym)
end
def method_missing name, *args, &block
index = @headers.index(name.to_s)
if index
@row[index]
else
super
end
end
end
end
end
class RubyCsv
include ActsAsCsv
acts_as_csv
end
csv = RubyCsv.new
puts csv.headers.inspect
puts csv.csv_contents.inspect
csv.each { |row| puts "#{row.name}, #{row.age}" }
$ ruby csv_new.rb
["name", "location", "age"]
[#<ActsAsCsv::InstanceMethods::CsvRow:0x25814 @row=["Jim", "Menlo Park", "27"], @headers=["name", "location", "age"]>, #<ActsAsCsv::InstanceMethods::CsvRow:0x25738 @row=["Bob", "Palo Alto", "37"], @headers=["name", "location", "age"]>, #<ActsAsCsv::InstanceMethods::CsvRow:0x2565c @row=["Steve", "NYC", "28"], @headers=["name", "location", "age"]>]
Jim, 27
Bob, 37
Steve, 28
module ActsAsCsv
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def acts_as_csv
include InstanceMethods
end
end
module InstanceMethods
attr_accessor :headers, :csv_contents
def initialize
read
end
def read
@csv_contents = []
filename = self.class.to_s.downcase + '.csv'
file = File.new(filename)
@headers = file.gets.chomp.split(', ')
file.each do |row|
@csv_contents << row.chomp.split(', ')
end
end
end
end
class RubyCsv
include ActsAsCsv
acts_as_csv
end
csv = RubyCsv.new
puts csv.headers.inspect
puts csv.csv_contents.inspect
$ ruby csv_original.rb
["name", "location", "age"]
[["Jim", "Menlo Park", "27"], ["Bob", "Palo Alto", "37"], ["Steve", "NYC", "28"]]
class LinkedInProfile
SIMPLE_PROFILE_FIELDS = %w[id summary headline honors interests specialties industry first_name last_name public_profile_url picture_url associations]
SIMPLE_PROFILE_FIELDS.each do |field|
define_method(field.to_sym) do
@json[field]
end
end
def initialize(json)
@json = json
end
end
name location age
Jim Menlo Park 27
Bob Palo Alto 37
Steve NYC 28
@brikis98
Copy link
Author

brikis98 commented Feb 1, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment