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.



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/bmg/operator/autowrap.rb', line 192

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.



203
204
205
# File 'lib/bmg/operator/autowrap.rb', line 203

def remover
  @remover
end

Class Method Details

.new(remover) ⇒ Object



187
188
189
190
# File 'lib/bmg/operator/autowrap.rb', line 187

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

Instance Method Details

#==(other) ⇒ Object



227
228
229
# File 'lib/bmg/operator/autowrap.rb', line 227

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

#all_nil?(tuple) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
# File 'lib/bmg/operator/autowrap.rb', line 213

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



205
206
207
208
209
210
211
# File 'lib/bmg/operator/autowrap.rb', line 205

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



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

def hash
  remover.hash
end

#inspectObject Also known as: to_s



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

def inspect
  @remover_to_s.inspect
end