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, remover_condition = :all) ⇒ NoLeftJoinNoise

Returns a new instance of NoLeftJoinNoise.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/bmg/operator/autowrap.rb', line 245

def initialize(remover, remover_condition = :all)
  @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
  @remover_condition = case remover_condition
  when :all then ->(tuple){ all_nil?(tuple) }
  when :id  then ->(tuple){ id_nil?(tuple) }
  else
    raise "Invalid remover condition `#{remover_condition}`"
  end
end

Instance Attribute Details

#removerObject (readonly)

Returns the value of attribute remover.



262
263
264
# File 'lib/bmg/operator/autowrap.rb', line 262

def remover
  @remover
end

Class Method Details

.new(remover, remover_condition = :all) ⇒ Object



240
241
242
243
# File 'lib/bmg/operator/autowrap.rb', line 240

def self.new(remover, remover_condition = :all)
  return remover if remover.is_a?(NoLeftJoinNoise)
  super
end

Instance Method Details

#==(other) ⇒ Object



293
294
295
# File 'lib/bmg/operator/autowrap.rb', line 293

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

#all_nil?(tuple) ⇒ Boolean

Returns:

  • (Boolean)


272
273
274
275
276
# File 'lib/bmg/operator/autowrap.rb', line 272

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



264
265
266
267
268
269
270
# File 'lib/bmg/operator/autowrap.rb', line 264

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) && @remover_condition.call(tuple[k])
  end
  tuple
end

#hashObject



289
290
291
# File 'lib/bmg/operator/autowrap.rb', line 289

def hash
  remover.hash
end

#id_nil?(tuple) ⇒ Boolean

Returns:

  • (Boolean)


278
279
280
281
282
# File 'lib/bmg/operator/autowrap.rb', line 278

def id_nil?(tuple)
  return false unless tuple.is_a?(Hash)

  tuple[:id].nil?
end

#inspectObject Also known as: to_s



284
285
286
# File 'lib/bmg/operator/autowrap.rb', line 284

def inspect
  @remover_to_s.inspect
end