Class: Flor::Pro::Arith

Inherits:
Flor::Procedure show all
Defined in:
lib/flor/pcore/arith.rb

Constant Summary collapse

DEFAULTS =
{ :+ => 0, :* => 1, :- => 0, :/ => 1 }

Constants inherited from Flor::Procedure

Flor::Procedure::RVARS, Flor::Procedure::TRUE_ATTS

Instance Attribute Summary

Attributes inherited from Node

#message

Instance Method Summary collapse

Methods inherited from Flor::Procedure

[], core?, #debug_msg, #debug_tree, #end, #flank, #heap, inherited, make, names, #prepare_on_receive_last, #trigger_on_error

Methods inherited from Node

#child_id, #cnodes, #cnodes_any?, #cnodes_empty?, #deref, #descendant_of?, #domain, #exid, #fei, #from, #h, #initialize, #lookup_tree, #lookup_value, #message_or_node_payload, #nid, #node_closed?, #node_ended?, #node_open?, #node_payload, #node_payload_ret, #node_status, #node_status_flavour, #on_error_parent, #parent, #payload, #payload_ret, #point, #reheap, #to_procedure_node, #tree

Constructor Details

This class inherits a constructor from Flor::Node

Instance Method Details

#pre_executeObject



45
46
47
48
49
50
51
# File 'lib/flor/pcore/arith.rb', line 45

def pre_execute

  @node['rets'] = []
  @node['atts'] = []

  unatt_unkeyed_children
end

#receive_lastObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/flor/pcore/arith.rb', line 53

def receive_last

  sign = tree.first.to_sym

  rets = @node['rets']
  rets << node_payload_ret \
    if rets.empty? && node_payload_ret.is_a?(Array)
  rets = rets[0] \
    if rets.size == 1 && rets[0].is_a?(Array)

  fail Flor::FlorError.new('modulo % requires at least 2 arguments', self) \
    if sign == :% && rets.size < 2

  if j = att('join')
    max = rets.size - 1
    rets = rets.each_with_index.inject([]) { |a, (e, i)|
      a << e
      a << j if i < max
      a }
    rets[0] = stringify(rets[0]) \
      if rets.any? && j.is_a?(String)
  end

  ret =
    if rets.compact.empty?
      DEFAULTS[sign]
    elsif sign == :+
      rets.reduce { |r, e|
        r + (r.is_a?(String) ? stringify(e) : e) }
    else
      rets = rets.collect(&:to_f) \
        if sign == :/ || rets.find { |r| r.is_a?(Float) }
      rets.reduce(&sign)
    end

  unless ret.is_a?(String)
    round = ret.round
    ret = round if round.to_f.to_s == ret.to_f.to_s
  end
    # follow JSON somehow, in show "1.0" as "1"...

  wrap_reply('ret' => ret)
end