Sha256: 88f1c59faac2b5690de4c417ae638455bee2a1619c5a29da3887672470f380b9

Contents?: true

Size: 927 Bytes

Versions: 5

Compression:

Stored size: 927 Bytes

Contents

module Piglet
  module Relation
    class Split # :nodoc:
      include Relation
    
      def initialize(relation, expressions)
        @sources, @expressions = [relation], expressions
        @shard_map = create_shards
      end
    
      def shards
        @shard_map.values_at(*@expressions)
      end
    
      def to_s
        split_strings = @expressions.map do |expression|
          "#{@shard_map[expression].alias} IF #{expression}"
        end
        
        "SPLIT #{@sources.first.alias} INTO #{split_strings.join(', ')}"
      end
    
    private
  
      def create_shards
        @expressions.inject({}) do |map, expr|
          map[expr] = RelationShard.new(self)
          map
        end
      end
    end
  
    class RelationShard # :nodoc:
      include Relation
    
      def initialize(split)
        @sources = [split]
      end
    
      def to_s
        self.alias
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
piglet-0.2.4 lib/piglet/relation/split.rb
piglet-0.2.3 lib/piglet/relation/split.rb
piglet-0.2.2 lib/piglet/relation/split.rb
piglet-0.2.0 lib/piglet/relation/split.rb
piglet-0.1.2 lib/piglet/relation/split.rb