Skip to content

Instantly share code, notes, and snippets.

@Ravenna
Created May 18, 2012 16:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Ravenna/2726296 to your computer and use it in GitHub Desktop.
PricePredicate
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