Class: Bmg::Operator::Autowrap::NoLeftJoinNoise

Inherits:
Object
  • Object
show all
Defined in:
lib/bmg/operator/autowrap.rb

Overview

Removes the noise generated by left joins that were not join.

i.e. x is removed in { x: { id: nil, name: nil, … } }

Supported heuristics are:

  • nil: { x: { id: nil, name: nil, … } } => { x: nil }

  • delete: { x: { id: nil, name: nil, … } } => { }

  • none: { x: { id: nil, name: nil, … } } => { x: { id: nil, name: nil, … } }

  • a Hash, specifying a specific heuristic by tuple attribute

  • a Proc, ‘->(tuple,key){ … }` that affects the tuple manually

Constant Summary collapse

REMOVERS =
{
  nil:    ->(t,k){ t[k] = nil  },
  delete: ->(t,k){ t.delete(k) },
  none:   ->(t,k){ t           }
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remover) ⇒ NoLeftJoinNoise

Returns a new instance of NoLeftJoinNoise.



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/bmg/operator/autowrap.rb', line 222

def initialize(remover)
  @remover_to_s = remover
  @remover = case remover
  when NilClass then REMOVERS[:none]
  when Proc     then remover
  when Symbol   then REMOVERS[remover]
  when Hash     then ->(t,k){ REMOVERS[remover[k] || :none].call(t,k) }
  else
    raise "Invalid remover `#{remover}`"
  end
end

Instance Attribute Details

#removerObject (readonly)

Returns the value of attribute remover.



233
234
235
# File 'lib/bmg/operator/autowrap.rb', line 233

def remover
  @remover
end

Class Method Details

.new(remover) ⇒ Object



217
218
219
220
# File 'lib/bmg/operator/autowrap.rb', line 217

def self.new(remover)
  return remover if remover.is_a?(NoLeftJoinNoise)
  super
end

Instance Method Details

#==(other) ⇒ Object



257
258
259
# File 'lib/bmg/operator/autowrap.rb', line 257

def ==(other)
  other.is_a?(NoLeftJoinNoise) && remover.eql?(other.remover)
end

#all_nil?(tuple) ⇒ Boolean

Returns:

  • (Boolean)


243
244
245
246
# File 'lib/bmg/operator/autowrap.rb', line 243

def all_nil?(tuple)
  return false unless tuple.is_a?(Hash)
  tuple.all?{|(k,v)| v.nil? || all_nil?(tuple[k]) }
end

#call(tuple) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/bmg/operator/autowrap.rb', line 235

def call(tuple)
  tuple.each_key do |k|
    call(tuple[k]) if tuple[k].is_a?(Hash)
    @remover.call(tuple, k) if tuple[k].is_a?(Hash) && all_nil?(tuple[k])
  end
  tuple
end

#hashObject



253
254
255
# File 'lib/bmg/operator/autowrap.rb', line 253

def hash
  remover.hash
end

#inspectObject Also known as: to_s



248
249
250
# File 'lib/bmg/operator/autowrap.rb', line 248

def inspect
  @remover_to_s.inspect
end