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.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/bmg/operator/autowrap.rb', line 229

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.



246
247
248
# File 'lib/bmg/operator/autowrap.rb', line 246

def remover
  @remover
end

Class Method Details

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



224
225
226
227
# File 'lib/bmg/operator/autowrap.rb', line 224

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

Instance Method Details

#==(other) ⇒ Object



277
278
279
# File 'lib/bmg/operator/autowrap.rb', line 277

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

#all_nil?(tuple) ⇒ Boolean

Returns:

  • (Boolean)


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

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



248
249
250
251
252
253
254
# File 'lib/bmg/operator/autowrap.rb', line 248

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



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

def hash
  remover.hash
end

#id_nil?(tuple) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  tuple[:id].nil?
end

#inspectObject Also known as: to_s



268
269
270
# File 'lib/bmg/operator/autowrap.rb', line 268

def inspect
  @remover_to_s.inspect
end