lib/prezzo/explainable.rb in prezzo-0.5.1 vs lib/prezzo/explainable.rb in prezzo-1.0.0.pre.rc
- old
+ new
@@ -1,40 +1,19 @@
module Prezzo
module Explainable
- def self.included(base)
- base.class_eval do
- base.extend(ClassMethods)
- end
- end
-
- module ClassMethods
- def explain_with(*component_names, **options)
- @explained_components ||= {}
- component_names.each do |component_name|
- @explained_components[component_name] = options
- end
- end
-
- attr_reader :explained_components
- end
-
def explain
- explained_components = self.class.explained_components || {}
-
explanation = {
total: calculate,
- components: {},
}
- explained_components.each do |component, options|
- 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) && options.fetch(:recursive, true)
- value = value.calculate if value.respond_to?(:calculate)
- explanation[:components][component] = value
- end
+ components = compile_components
+ explanation[:components] = components unless components.empty?
+
+ context = compile_params
+ explanation[:context] = context unless context.empty?
+
+ transients = compile_transients
+ explanation[:transients] = transients unless transients.empty?
explanation
end
end
end