class Streamer::Stream

Stream is the object that is responsible for mutating the data passed to it

Attributes

finder[RW]
payload[R]

Public Class Methods

new(hash) click to toggle source
# File lib/streamer/stream.rb, line 8
def initialize(hash)
  @payload = hash
end

Public Instance Methods

assign(property:, value: nil, function: nil) click to toggle source
# File lib/streamer/stream.rb, line 16
def assign(property:, value: nil, function: nil)
  assign_property(
    structure: payload,
    properties: property.to_s.split('.'),
    value: value,
    function: function
  )
  self
end
assign_each(list:, property:, value: nil, function: nil) click to toggle source
# File lib/streamer/stream.rb, line 38
def assign_each(list:, property:, value: nil, function: nil)
  payload.dig(*list.split('.')).each do |item|
    item[property] = value unless value.nil?
    item[property] = functor(replace_terms(item, function)).call if function
  end
  self
end
assign_property(structure:, properties:, value: nil, function: nil) click to toggle source
# File lib/streamer/stream.rb, line 26
def assign_property(structure:, properties:, value: nil, function: nil)
  properties.each_with_index do |prop, index|
    if index == properties.size - 1
      structure[prop] = value unless value.nil?
      structure[prop] = functor(function).call if function
    else
      structure[prop] = {} unless structure[prop]
      structure = structure[prop]
    end
  end
end
filter(function:) click to toggle source
# File lib/streamer/stream.rb, line 12
def filter(function:)
  assign(property: 'filter_value', value: functor(function).call)
end
functor(options = {}, pl = payload) click to toggle source
# File lib/streamer/stream.rb, line 59
def functor(options = {}, pl = payload)
  Streamer::Functors::Functor.new(pl, options)
end
replace_terms(item, function_hash) click to toggle source
# File lib/streamer/stream.rb, line 46
def replace_terms(item, function_hash)
  newfunc = Marshal.load(Marshal.dump(function_hash))
  newfunc[:terms] = newfunc[:terms].map do |t|
    if t.is_a?(String) && t.start_with?('#')
      t[0] = '' # remove the '#'
      item.dig(*t.split('.'))
    else
      t
    end
  end
  newfunc
end