Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created October 2, 2010 20:05
Show Gist options
  • Select an option

  • Save practicingruby/607944 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/607944 to your computer and use it in GitHub Desktop.
class Contact
def initialize(&block)
instance_eval &block
end
def name(new_name=nil)
@name = new_name if new_name
@name
end
def email(new_email=nil)
@email = new_email if new_email
@email
end
def phone_number(new_phone_number=nil)
@phone_number = new_phone_number if new_phone_number
end
def to_s
"Name: #{@name}\n" +
"Email: #{@email}\n" +
"Phone Number: #{@phone_number}"
end
private
def secret_private_method_that_breaks_everything
raise "oh noez, the sky is falling"
end
end
contact = Contact.new do
name "Gregory Brown"
email "gregory.t.brown@gmail.com"
phone_number "555.555.5555"
end
puts contact
class PurchaseOrder
def initialize(product_list)
@product_list = product_list
end
attr_reader :product_list
def summary_data
product_list.products.map do |p|
{ :item_number => p.item_number,
:upc => p.upc,
:title => p.super_title,
:platform => p.platform_code,
:vendor_name => p.product_group.name,
:vendor_number => p.extended_vendor_number,
:warehouse_pack_quantity => p.warehouse_pack_quantity,
:total_units => p.total_order_units,
:unit_retail => p.unit_retail.to_f,
:order_at_retail => p.total_order_units.to_f * p.unit_retail,
:allocation => nil, #TODO: Not yet implemented
:original_order => nil, #TODO: Not yet implemented
:amount_cut => nil, #TODO: Not yet implemented
}
end
end
def generate
workbook = WriteExcel.new("#{RAILS_ROOT}/public/po.xls")
summary = workbook.add_worksheet("Summary")
currency_format = workbook.add_format
currency_format.set_num_format("$0.00")
headers = %w[Item\ Nbr UPC Title Platform Vendor\ Name Vendor\ Nbr
WHPK\ QTY Total\ Units Unit\ Retail Order\ At\ Retail
Allocation Original\ Order Amt\ Cut]
headers.each_with_index do |header,col|
summary.write(0,col,header)
end
summary_data.each_with_index do |record, index|
row = index + 1
summary.write(row, 0, record[:item_number])
summary.write(row, 1, record[:upc])
summary.write(row, 2, record[:title])
summary.write(row, 3, record[:platform])
summary.write(row, 4, record[:vendor_name])
summary.write(row, 5, record[:vendor_number])
summary.write(row, 6, record[:warehouse_pack_quantity])
summary.write(row, 7 , record[:total_units])
summary.write(row, 8, record[:unit_retail], currency_format)
summary.write(row, 9 , record[:order_at_retail], currency_format)
summary.write(row, 10, record[:allocation])
summary.write(row, 11, record[:original_order])
summary.write(row, 12, record[:amount_cut])
end
workbook.close
end
end
class Product
InvalidRecord = Class.new(StandardError)
def self.from_string(string)
new(parse(string))
end
def initialize(params)
@product_name = params[:product_name]
@item_number = params[:item_number]
@vendor_name = params[:vendor_name]
@vendor_number = params[:vendor_number]
end
attr_accessor :product_name, :item_number, :vendor_name, :vendor_number
def to_s
"Product: #{product_name} (Item ##{item_number})\n"+
"Manufactured by: #{vendor_name} (Vendor ##{vendor_number})\n"
end
private
def self.parse(text)
product_name, item_number, vendor_name, vendor_number = text.split("\n")
validate_product_name(product_name)
validate_item_number(item_number)
validate_vendor_name(vendor_name)
validate_vendor_number(vendor_number)
{ :product_name => product_name,
:item_number => item_number,
:vendor_name => vendor_name,
:vendor_number => vendor_number }
end
def self.validate_item_number(text)
unless text =~ /\s*\d{6}\s*$/
raise InvalidRecord, "Malformed item number"
end
end
def self.validate_product_name(text)
unless authorized_products.include?(text)
raise InvalidRecord, "Not a valid product name. " +
"Must be one of: #{authorized_products.join(',')}"
end
end
def self.validate_vendor_name(text)
unless authorized_vendors.include?(text)
raise InvalidRecord, "Not a valid product name. " +
"Must be one of: #{authorized_products.join(',')}"
end
end
def self.validate_vendor_number(text)
unless text =~ /\s*\d{12}\s*$/
raise InvalidRecord, "Malformed vendor number"
end
end
# THIS IS A STUB, DO NOT WORRY ABOUT REFACTORING IT
def self.authorized_products
%w[Bananas Apples ZondaHH]
end
# THIS IS A STUB, DO NOT WORRY ABOUT REFACTORING IT
def self.authorized_vendors
%w[MegaSuperCorp EnterpriseCongolomerated MagicUnicornFactory]
end
end
product = Product.from_string("Apples\n123456\nMagicUnicornFactory\n000111222445")
puts product
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment