lib/galaxy_converter/note.rb in galaxy_converter-3.0.1 vs lib/galaxy_converter/note.rb in galaxy_converter-3.1.1

- old
+ new

@@ -3,11 +3,14 @@ PREFIXES = ["how much is", "how many credits is"] QUESTION = "?" NO_IDEA = "I have no idea what you are talking about" def self.from(notes) - Array(notes).map { |body| new(body) } + Array(notes).map do |body| + klass = body.index(/credits/i) ? Credit : Note + klass.new(body) + end end attr_reader :body, :units, :good def initialize(body) @@ -17,32 +20,37 @@ def question? @body.end_with?(QUESTION) end - def commercial? - !!@body.index("credits") - end - def answer(total = 0) return NO_IDEA if total.zero? [].tap do |s| s << units s << good.to_s.capitalize s << "is" s << "%g" % total - s << "Credits" if commercial? end.reject(&:empty?).join(" ") end private def detect - tokens = stripped.split - return [tokens.join(" "), nil] unless commercial? - good = tokens.pop - [tokens.join(" "), good] + return stripped, nil end private def stripped @body.sub(/#{PREFIXES.join("|")}/, "").sub(QUESTION, "").strip + end + end + + class Credit < Note + def answer(total = 0) + s = super + s << " Credits" + end + + private def detect + tokens = stripped.split + good = tokens.pop + [tokens.join(" "), good] end end end