Sha256: 29e6178647055adccd17bbc0e2ced9e959a711abb35e1a110cc953937eea2a1d

Contents?: true

Size: 978 Bytes

Versions: 3

Compression:

Stored size: 978 Bytes

Contents

# frozen_string_literal: true

module Danica
  module VariablesHolder
    class Calculator
      attr_accessor :object, :args

      def initialize(object, *args)
        @object = object
        @args = args
      end

      def calculate
        raise Exception::NotDefined unless all_valued?

        clazz.new(vars_map).to_f
      end

      private

      def vars_map
        @vars_map ||= variables_value_hash.tap do |map|
          vars = args.dup
          map.merge! vars.extract_options!
          vars_map = vars.as_hash(map.keys).select { |_, v| valued?(v) }
          map.merge! vars_map
        end
      end

      def all_valued?
        vars_map.values.all? { |v| valued?(v) }
      end

      def valued?(value)
        value.is_a?(Integer) || value.try(:valued?)
      end

      def variables_value_hash
        @variables_value_hash ||= object.variables_value_hash.dup
      end

      def clazz
        @clazz ||= object.class
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
danica-2.7.7 lib/danica/variables_holder/calculator.rb
danica-2.7.6 lib/danica/variables_holder/calculator.rb
danica-2.7.5 lib/danica/variables_holder/calculator.rb