lib/prezzo/explainable.rb in prezzo-0.4.0 vs lib/prezzo/explainable.rb in prezzo-0.4.1

- old
+ new

@@ -5,17 +5,23 @@ base.extend(ClassMethods) end end module ClassMethods - def explain_with(*methods) + def explain_with(*component_names) define_method(:explain) do explanation = { total: calculate, } - components = methods.each_with_object({}) do |method, acc| - acc[method] = send(method) + components = component_names.each_with_object({}) do |component, acc| + value = send(component) + if self.class.respond_to?(:components) && self.class.components.include?(component) + value = cached_components[component] + end + value = value.explain if value.respond_to?(:explain) + value = value.calculate if value.respond_to?(:calculate) + acc[component] = value end explanation[:components] = components unless components.empty? explanation