Module: Chartnado::Evaluator::Operators

Included in:
Hash
Defined in:
lib/chartnado/evaluator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/chartnado/evaluator.rb', line 4

def self.included(base)
  %i{+ * - /}.each do |method|
    define_method method do |*args, &block|
      if Thread.current[:in_chartnado_block]
        OperatorEvaluator.new(self).send(method, *args)
      elsif defined?(super)
        super(*args, &block)
      else
        raise NoMethodError, "#{method} is not defined"
      end
    end
  end

  def coerce(other)
    return super unless Thread.current[:in_chartnado_block]
    if other.is_a?(Numeric)
      [self, other]
    else
      super
    end
  end
end

Instance Method Details

#coerce(other) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/chartnado/evaluator.rb', line 17

def coerce(other)
  return super unless Thread.current[:in_chartnado_block]
  if other.is_a?(Numeric)
    [self, other]
  else
    super
  end
end