Sha256: 2f77b78b5700f0c3a13eb750934256fe9689285735165ec160a9e39ad6047d81
Contents?: true
Size: 1.07 KB
Versions: 1
Compression:
Stored size: 1.07 KB
Contents
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 explanation end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
prezzo-0.5.1 | lib/prezzo/explainable.rb |