Skip to content

Instantly share code, notes, and snippets.

@6ewis
Created January 18, 2014 20:20
Show Gist options
  • Save 6ewis/616e70766bf162625760 to your computer and use it in GitHub Desktop.
Save 6ewis/616e70766bf162625760 to your computer and use it in GitHub Desktop.
require 'csv'
require 'YAML'
Customer = Struct.new(:cust_id,
:elect_or_gas,
:disconnect_doc,
:move_in_date,
:move_out_date,
:bill_year,
:bill_month,
:span_days,
:meter_read_date,
:meter_read_type,
:consumption,
:exception_code,
:data
)
class Parser
attr_accessor :customers
def initialize filename
self.customers = []
deserialize_users filename
serialize_users
end
def deserialize_users filename
CSV.foreach(filename, col_sep: "|") do |row|
next if row.empty?
#sanitized_values(cust_id, elect_or_gas, disconnect_doc, move_in_date, move_out_date, bill_year, bill_month, span_days, meter_read_date, meter_read_type, consumption, exception_code = row)
customers << Customer.new(*row, row)
end
customers
end
def serialize_users
CSV.open("./serialized_users.txt", "w") do |csv| #is 'a' better?
customers.each do |c|
csv << [c.data] #why an array of hashes is better? and how?
end
end
end
end
class Reporting
attr_accessor :p
def initialize data
self.p = Parser.new data
end
#STATISTICS
def unique_customers
p.customers.uniq {|customer| customer.cust_id }
end
def customers_with_electricity
p.customers.select { |customer| customer.elect_or_gas == "1" && customer.elect_or_gas != "2" }
end
def customers_with_gas
p.customers.select { |customer| customer.elect_or_gas != "1" && customer.elect_or_gas == "2" }
end
#REPORT
def show_all_data
print "\n\nALL CUSTOMERS AND DATA\n\n"
puts p.customers.map { |customer| customer.data.map { |item| item.to_s.center 15 }.join '|' }
end
def show_unique_customers
print "\n\nUNIQUE CUSTOMERS\n\n"
unique_customers.map {|customer| puts customer.data.map { |item| item.to_s.center 15}.join'|' }
puts "\nNumber of unique customers: #{unique_customers.count -1}"
end
def show_customers_with_electricity
print "\n\nCUSTOMERS WITH ELECTRICITY ONLY\n\n"
customers_with_electricity.map {|customer| puts customer.data.map { |item| item.to_s.center 15}.join'|'}
puts "\nNumber of customers with electricity: #{customers_with_electricity.count}"
end
def show_customers_with_gas
print "\n\nCUSTOMER WITH GAS ONLY\n\n"
customers_with_gas.map {|customer| puts customer.data.map { |item| item.to_s.center 15}.join'|'}
puts "\nNumber of customers with gas: #{customers_with_gas.count}"
end
end
r = Reporting.new "./pulse_data.txt"
r.show_all_data
r.show_unique_customers
r.show_customers_with_gas
r.show_customers_with_electricity
@6ewis
Copy link
Author

6ewis commented Jan 18, 2014

14:52 r0bgleeson: X = Struct.new(:foo, :bar); X.new(*["1", "2"]).to_a
14:52 r0bgleeson: (pry):7: warning: already initialized constant X
14:52 r0bgleeson: => ["1", "2"]

nice trick

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