Sha256: 2aad7882223cbd5e8838eb1607ea6dc1abf7e4b03b5be93b1e04d4c1ca54baa9

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

module Alf
  module Types
    #
    # Defines a projection key
    # 
    class AttrList
    
      # Projection attributes
      attr_accessor :attributes
    
      def initialize(attributes)
        @attributes = attributes
      end
    
      def self.coerce(arg)
        case arg
        when AttrList
          arg
        when Ordering
          AttrList.new(arg.attributes)
        when Array
          AttrList.new(arg.collect{|s| Tools.coerce(s, AttrName)})
        else
          raise ArgumentError, "Unable to coerce #{arg} to a projection key"
        end
      end

      def self.from_argv(argv, opts = {})
        coerce(argv)
      end
          
      def to_ordering
        Ordering.new attributes.collect{|arg| [arg, :asc]}
      end
    
      def project(tuple, allbut = false)
        split(tuple, allbut).first
      end
    
      def split(tuple, allbut = false)
        projection, rest = {}, tuple.dup
        attributes.each do |a|
          projection[a] = tuple[a]
          rest.delete(a)
        end
        allbut ? [rest, projection] : [projection, rest]
      end

      def ==(other)
        other.is_a?(AttrList) && (other.attributes == attributes)          
      end
      alias :eql? :==
      
    end # class AttrList
  end # module Types
end # module Alf

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alf-0.10.1 lib/alf/types/attr_list.rb
alf-0.10.0 lib/alf/types/attr_list.rb