Skip to content

Commit

Permalink
Small fixes. Add (stolen) readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcheiligers committed Jun 11, 2012
1 parent fff24cb commit 56f31b1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
40 changes: 40 additions & 0 deletions ruby/marcheiligers/README.md
@@ -0,0 +1,40 @@
This README stolen from apauly's version and tweaked.

How to run this Ruby version
============================

* Change to this directory in a terminal

* Run prices.rb

> $ ruby prices.rb
* Look at the contents of pricefile.txt


About the Code
==============

The example lends itself to a simple Product/Fruit object hierarchy,
which is totally not what I've used here. Unlike _apauly_, instead I've
decided that products are products, but there are Rules which change
how a Product's markup and shelf life is calculated.

The extra supplier logic is something which does not fit in with the
_apualy_'s hierarchy, doesn't exist in this example. Instead all Products
are tested against all Rules and those rules that match a product are
applied. There are the basic fruit rules which set markup % and shelf
life in days, and then the special Supplier rules which actually
override the way the calculations are done. All the rules use some mild
meta-programming, which I like.

Some interesting features:
* Rules are never actually instantiated.
* It's really easy to add additional Rules, both for new types of Fruit
and for new Supplier "issues". But, there's no support for Supplier rules
that apply to the same product. I haven't thought about how one might
implement that using this solution.
* The actual LabelPrinter class just pulls functionality from the 3
Modules. It's really easy to split the responsibilities, add rules, even
have multiple different label printers, perhaps for different shops.
* In a very non-Rubyist approach, I've done no testing what-so-ever :(
11 changes: 4 additions & 7 deletions ruby/marcheiligers/prices.rb
Expand Up @@ -19,9 +19,6 @@ def initialize(supplier_id, product_code, description, delivery_date, cost_price
@delivery_date = Date.parse delivery_date @delivery_date = Date.parse delivery_date
@cost_price = cost_price.to_i @cost_price = cost_price.to_i
@unit_count = unit_count.to_i @unit_count = unit_count.to_i

@markup = 0.5
@shelf_life = 7
end end


def selling_price def selling_price
Expand Down Expand Up @@ -104,9 +101,9 @@ def apply!(product)
end end


class FruitRule < Rule class FruitRule < Rule
product_codes 1100, 1199 product_codes 1000, 1999
markup 0.4 markup 0.5
shelf_life 14 shelf_life 7
end end


class AppleRule < Rule class AppleRule < Rule
Expand All @@ -126,7 +123,7 @@ class BerryRule < Rule
markup 0.55 markup 0.55
end end


class SupplierRule class SupplierRule < Rule
def self.for_suppliers(*ids) def self.for_suppliers(*ids)
suppliers.concat ids suppliers.concat ids
self self
Expand Down

0 comments on commit 56f31b1

Please sign in to comment.