Class: Wolflow::Operators::Operation

Inherits:
Base
  • Object
show all
Defined in:
lib/wolflow/operators/operation.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

inherited

Constructor Details

#initialize(op:, members:, **args) ⇒ Operation

Returns a new instance of Operation.



6
7
8
9
10
# File 'lib/wolflow/operators/operation.rb', line 6

def initialize(op:, members:, **args)
  @op = op
  @members = Array(members)
  super(**args)
end

Class Method Details

.from_hash(hash) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/wolflow/operators/operation.rb', line 31

def self.from_hash(hash)
  case hash
  in { op: String, members: [*, Hash, *] => members }
    members.map! { |m| Operators.from_hash(m) }
    super(hash.merge(members: members))
  else
    super
  end
end

Instance Method Details

#call(task) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/wolflow/operators/operation.rb', line 12

def call(task)
  reducer, *members = @members

  return unless reducer

  reducer = reducer.call(task)

  members.reduce(reducer) do |acc, member|
    acc.__send__(@op, member.call(task))
  end
end

#to_hashObject



24
25
26
27
28
29
# File 'lib/wolflow/operators/operation.rb', line 24

def to_hash
  hs = super
  hs[:op] = @op
  hs[:members] = @members.map(&:to_hash)
  hs
end