lib/streamer/functors/functor.rb in streamer-0.1.0 vs lib/streamer/functors/functor.rb in streamer-0.1.1
- old
+ new
@@ -11,24 +11,43 @@
def call
Module.const_get(class_name).new(payload, options).call
end
def class_name
- "Streamer::Functors::#{options.fetch(:type).capitalize}"
+ "Streamer::Functors::#{type_name}"
end
+ def type_name
+ options.fetch(:type).to_s.split('_').map(&:capitalize).join
+ end
+
private
- def compare(op_symbol)
- tar = target(options.fetch(:target))
- function = options[:function]
- value = options[:value]
- return functor(function).call.send(op_symbol, tar) if function
- return value.send(op_symbol, tar) unless value.nil?
- fail 'Streamer::Functor#gte no value or fuction given'
+ def compare(op)
+ fail 'Streamer::Functor::Compare no comparison' unless valid_compare
+ compare_function(op) || compare_value(op) || compare_property(op)
end
+ def valid_compare
+ option_value || function || property
+ end
+
+ def compare_function(op_symbol)
+ return unless function
+ functor(function).call.send(op_symbol, target(options.fetch(:target)))
+ end
+
+ def compare_value(op_symbol)
+ return if option_value.nil?
+ option_value.send(op_symbol, target(options.fetch(:target)))
+ end
+
+ def compare_property(op_symbol)
+ return if property.nil?
+ prop(property).send(op_symbol, target(options.fetch(:target)))
+ end
+
def numerify(terms)
terms.map do |t|
val = prop(t) if t.is_a? String
val = t if t.is_a? Numeric
val
@@ -37,12 +56,12 @@
def prop(p)
payload.dig(*p.split('.'))
end
- def functor(function_hash)
- Functor.new(payload, function_hash)
+ def functor(function_hash, pl = payload)
+ Functor.new(pl, function_hash)
end
def target(options)
return options[:value] if options[:value]
return functor(options[:function]).call if options[:function]
@@ -51,9 +70,21 @@
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
+
+ def function
+ options[:function]
+ end
+
+ def option_value
+ options[:value]
+ end
+
+ def property
+ options[:property]
end
end
end
end