Sha256: c290a579b8d13a95f61bfa8ef9aeb9eaeb31582ae2338c02aa2413f88071e04e

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(:resursive, 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.0 lib/prezzo/explainable.rb