Sha256: 8ef36b3d1e61a7902a9629aabf8e6cab1233191006bdf8310c7258065f88eb32

Contents?: true

Size: 909 Bytes

Versions: 1

Compression:

Stored size: 909 Bytes

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)
        define_method(:explain) do
          explanation = {
            total: calculate,
          }

          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
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prezzo-0.4.1 lib/prezzo/explainable.rb