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           }
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remover) ⇒ NoLeftJoinNoise



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/bmg/operator/autowrap.rb', line 129

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

Class Method Details

.new(remover) ⇒ Object



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

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

Instance Method Details

#all_nil?(tuple) ⇒ Boolean



148
149
150
151
# File 'lib/bmg/operator/autowrap.rb', line 148

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



141
142
143
144
145
146
# File 'lib/bmg/operator/autowrap.rb', line 141

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



153
154
155
# File 'lib/bmg/operator/autowrap.rb', line 153

def inspect
  @remover_to_s.inspect
end