-
-
Save 6ewis/f0c0069f277fcf6b0e9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
require 'YAML' | |
class Customers | |
attr_reader :custom_methods | |
def initialize | |
@custom_methods = {} | |
end | |
def method_missing name, *args | |
custom_method = name.to_s.underscore | |
if custom_method.match(/=$/) | |
@custom_methods[custom_method.chop] = args[0] | |
else | |
@custom_methods[custom_method] | |
end | |
end | |
end | |
class Parse | |
attr_accessor :customers | |
def initialize filename | |
self.customers = [] | |
deserialized_users filename | |
end | |
def deserialized_users filename | |
CSV.foreach(filename, col_sep: "|") do |row| | |
next if row.empty? | |
customer = Customers.new | |
row.each_with_index do |var, index| | |
customer.send("#{var}=", row[index]) | |
customers << customer | |
end | |
end | |
customers | |
end | |
def serialized_users | |
File.open("./serialized_users.yaml", "w") do |yaml_file| | |
customers.each do |c| | |
yaml_file.puts YAML::dump([c.custom_methods.each {|k,v| }, c.elect_or_gas, c.disconnect_doc, c.move_in_date, c.move_out_date, c.bill_year, c.bill_month, c.span_days, c.meter_read_date, c.meter_read_type, c.consumption, c.exception_code]) | |
end | |
end | |
end | |
end | |
class String | |
def underscore | |
self.gsub(/::/, '/'). | |
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). | |
gsub(/([a-z\d])([A-Z])/,'\1_\2'). | |
tr("-", "_"). | |
downcase | |
end | |
end | |
p = Parse.new "./pulse_data.txt" | |
#p.serialized_users | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment