Created
May 18, 2012 16:41
-
-
Save Ravenna/2726296 to your computer and use it in GitHub Desktop.
PricePredicate
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
class PricePredicate | |
def initialize(&block) | |
@predicate = block | |
end | |
def evaluate(subject) #would I pass the lead object in here? | |
@predicate.call(subject) #and here | |
end | |
end | |
prices = { | |
"2008" => [ | |
PricePredicate.new { |subject| if subject >= 30 then 10 end }, | |
PricePredicate.new { |subject| if subject >= 20 && subject <= 29 then 9 end }, | |
PricePredicate.new { |subject| if subject >= 15 && subject <= 19 then 8 end }, | |
PricePredicate.new { |subject| if subject >= 10 && subject <= 14 then 7 end }, | |
PricePredicate.new { |subject| if subject < 10 then 6 end }, | |
], | |
"2007" => [ | |
... | |
], | |
... | |
} | |
year = "2008" | |
value = 9 | |
price = nil | |
catch (:done) do | |
prices[year].each do |predicate| | |
price = predicate.evaluate(value) | |
throw :done unless price.nil? | |
end | |
end | |
puts price |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment