class Streamer::Functors::Functor
Functor
is responsible for creating the functions that the stream uses.
Attributes
options[R]
payload[R]
Public Class Methods
new(payload, options)
click to toggle source
# File lib/streamer/functors/functor.rb, line 6 def initialize(payload, options) @payload = payload @options = options end
Public Instance Methods
call()
click to toggle source
# File lib/streamer/functors/functor.rb, line 11 def call Module.const_get(class_name).new(payload, options).call end
class_name()
click to toggle source
# File lib/streamer/functors/functor.rb, line 15 def class_name "Streamer::Functors::#{type_name}" end
type_name()
click to toggle source
# File lib/streamer/functors/functor.rb, line 19 def type_name options.fetch(:type).to_s.split('_').map(&:capitalize).join end
Private Instance Methods
compare(op)
click to toggle source
# File lib/streamer/functors/functor.rb, line 25 def compare(op) fail 'Streamer::Functor::Compare no comparison' unless valid_compare compare_function(op) || compare_value(op) || compare_property(op) end
compare_function(op_symbol)
click to toggle source
# File lib/streamer/functors/functor.rb, line 34 def compare_function(op_symbol) return unless function functor(function).call.send(op_symbol, target(options.fetch(:target))) end
compare_property(op_symbol)
click to toggle source
# File lib/streamer/functors/functor.rb, line 44 def compare_property(op_symbol) return if property.nil? prop(property).send(op_symbol, target(options.fetch(:target))) end
compare_value(op_symbol)
click to toggle source
# File lib/streamer/functors/functor.rb, line 39 def compare_value(op_symbol) return if option_value.nil? option_value.send(op_symbol, target(options.fetch(:target))) end
function()
click to toggle source
# File lib/streamer/functors/functor.rb, line 77 def function options[:function] end
functor(function_hash, pl = payload)
click to toggle source
# File lib/streamer/functors/functor.rb, line 61 def functor(function_hash, pl = payload) Functor.new(pl, function_hash) end
list()
click to toggle source
# File lib/streamer/functors/functor.rb, line 93 def list return list_from_value unless list_definition[:value].nil? return list_from_function unless list_definition[:function].nil? fail 'Streamer::Functors - no list given' end
list_definition()
click to toggle source
# File lib/streamer/functors/functor.rb, line 89 def list_definition options.fetch(:list) end
list_from_function()
click to toggle source
# File lib/streamer/functors/functor.rb, line 103 def list_from_function functor(list_definition[:function]).call end
list_from_value()
click to toggle source
# File lib/streamer/functors/functor.rb, line 99 def list_from_value payload[list_definition[:value]] end
numerify(terms)
click to toggle source
# File lib/streamer/functors/functor.rb, line 49 def numerify(terms) terms.map do |t| val = prop(t) if t.is_a? String val = t if t.is_a? Numeric val end end
option_value()
click to toggle source
# File lib/streamer/functors/functor.rb, line 81 def option_value options[:value] end
prop(p)
click to toggle source
# File lib/streamer/functors/functor.rb, line 57 def prop(p) payload.dig(*p.split('.')) end
property()
click to toggle source
# File lib/streamer/functors/functor.rb, line 85 def property options[:property] end
target(options)
click to toggle source
# File lib/streamer/functors/functor.rb, line 65 def target(options) return options[:value] if options[:value] return functor(options[:function]).call if options[:function] end
valid_compare()
click to toggle source
# File lib/streamer/functors/functor.rb, line 30 def valid_compare option_value || function || property end
value(pk, data = payload)
click to toggle source
# File lib/streamer/functors/functor.rb, line 70 def value(pk, data = payload) return data if pk.size == 0 data = data[pk.shift] return data.map { |x| value(pk, x) } if data.is_a? Array value(pk, data) end