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 Method Summary collapse

Constructor Details

#initialize(remover) ⇒ NoLeftJoinNoise

Returns a new instance of NoLeftJoinNoise.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bmg/operator/autowrap.rb', line 100

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 Method Details

#all_nil?(tuple) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/bmg/operator/autowrap.rb', line 119

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



112
113
114
115
116
117
# File 'lib/bmg/operator/autowrap.rb', line 112

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

#inspectObject Also known as: to_s



124
125
126
# File 'lib/bmg/operator/autowrap.rb', line 124

def inspect
  @remover_to_s.inspect
end