Class: Alf::Types::AttrList

Inherits:
Object
  • Object
show all
Includes:
Support::OrderedSet
Defined in:
lib/alf/types/attr_list.rb

Overview

An attribute list is an ordered collection of attribute names (see AttrName).

Example:

list = AttrList[:name, :city]

Constant Summary collapse

EMPTY =
AttrList.new([])

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::OrderedSet

#&, #-, #elements, #map, #|

Class Method Details

.[](*args) ⇒ Object



20
# File 'lib/alf/types/attr_list.rb', line 20

def self.[](*args); coerce(args); end

Instance Method Details

#<=>(other) ⇒ Integer Also known as: set_compare

Compares this attribute list with ‘other` on a set basis.

other, 1 if a superset, nil otherwise.

Parameters:

  • other (AttrList)

    another attribute list

Returns:

  • (Integer)

    0 if same set of attribute names, -1 if self is a subset of



85
86
87
88
89
90
91
92
93
# File 'lib/alf/types/attr_list.rb', line 85

def <=>(other)
  return nil unless other.respond_to?(:to_set)
  s1, s2 = to_set, other.to_set
  if s1==s2              then 0
  elsif s1.subset?(s2)   then -1
  elsif s1.superset?(s2) then 1
  else nil
  end
end

#allbut(attrs) ⇒ Object

Shortcut for project(attrs, true)



76
77
78
# File 'lib/alf/types/attr_list.rb', line 76

def allbut(attrs)
  project(attrs, true)
end

#intersect?(other) ⇒ Boolean

Returns true if self intersects another attribute list

Returns:



118
119
120
# File 'lib/alf/types/attr_list.rb', line 118

def intersect?(other)
  !((self & other).empty?) || (empty? && other.empty?)
end

#project(attrs, allbut = false) ⇒ AttrList

Projects this attribute list on a subset of attributes ‘attrs`.

Parameters:

  • attrs (AttrList)

    a subset of these attributes

  • allbut (Boolean) (defaults to: false)

    projection?

Returns:

  • (AttrList)

    the projection as an attribute list



71
72
73
# File 'lib/alf/types/attr_list.rb', line 71

def project(attrs, allbut = false)
  allbut ? self - attrs : self & attrs
end

#project_tuple(tuple, allbut = false) ⇒ Object

Projects ‘tuple` on known (or unknown) attributes.

Example:

list = AttrList.new([:name])
tuple = {:name => "Jones", :city => "London"}

list.project_tuple(tuple)
# => {:name => "Jones"}

list.project_tuple(tuple, true)
# => {:city => "London"}

Parameters:

  • tuple (Hash)

    a tuple to project

  • allbut (Boolean) (defaults to: false)

    projection?

Returns:

  • Hash the projected tuple



62
63
64
# File 'lib/alf/types/attr_list.rb', line 62

def project_tuple(tuple, allbut = false)
  split_tuple(tuple, allbut).first
end

#sameset?(other) ⇒ Boolean

Returns true if this attribute list captures the same set of attribute names than ‘other`.

Returns:



98
99
100
101
# File 'lib/alf/types/attr_list.rb', line 98

def sameset?(other)
  c = set_compare(other)
  c and (c==0)
end

#split_tuple(tuple, allbut = false) ⇒ Array<Hash>

Splits a tuple in two parts according ‘allbut`.

Example:

list = AttrList.new([:name])
tuple = {:name => "Jones", :city => "London"}

list.split_tuple(tuple)
# => [{:name => "Jones"}, {:city => "London"}]

list.split_tuple(tuple, true)
# => [{:city => "London"}, {:name => "Jones"}]

Parameters:

  • tuple (Hash)

    a tuple to split

  • allbut (Boolean) (defaults to: false)

    unknown attributes as first result?

Returns:

  • (Array<Hash>)

    an array containing two tuples, according to known attributes and ‘allbut`



38
39
40
41
42
43
44
45
# File 'lib/alf/types/attr_list.rb', line 38

def split_tuple(tuple, allbut = false)
  projection, rest = {}, tuple.to_hash.dup
  each do |a|
    projection[a] = tuple[a] if tuple.has_key?(a)
    rest.delete(a)
  end
  allbut ? [rest, projection] : [projection, rest]
end

#subset?(other, proper = false) ⇒ Boolean Also known as: subset_of?

Returns true if self is a (proper) subset of ‘other`

Returns:



111
112
113
114
# File 'lib/alf/types/attr_list.rb', line 111

def subset?(other, proper = false)
  c = set_compare(other)
  c and (c <= (proper ? -1 : 0))
end

#superset?(other, proper = false) ⇒ Boolean Also known as: supserset_of?

Returns true if self is a (proper) superset of ‘other`

Returns:



104
105
106
107
# File 'lib/alf/types/attr_list.rb', line 104

def superset?(other, proper = false)
  c = set_compare(other)
  c and (c >= (proper ? 1 : 0))
end

#to_attr_listAttrList

Converts to an attribute list.

Returns:



125
126
127
# File 'lib/alf/types/attr_list.rb', line 125

def to_attr_list
  self
end

#to_lispyString

Returns a lispy expression.

Returns:

  • (String)

    a lispy expression for this attribute list



140
141
142
# File 'lib/alf/types/attr_list.rb', line 140

def to_lispy
  Support.to_ruby_literal(to_a)
end

#to_ordering(order = :asc) ⇒ Ordering

Converts this attribute list to an ordering.

Parameters:

  • :asc (Symbol)

    or :desc for ordering direction

Returns:



133
134
135
# File 'lib/alf/types/attr_list.rb', line 133

def to_ordering(order = :asc)
  Ordering.new elements.map{|arg| [arg, order]}
end

#to_ruby_literalString Also known as: to_s, inspect

Returns a ruby literal for this attribute list.

Returns:

  • (String)

    a literal s.t. ‘eval(self.to_ruby_literal) == self`



147
148
149
# File 'lib/alf/types/attr_list.rb', line 147

def to_ruby_literal
  "Alf::AttrList#{Support.to_ruby_literal(to_a)}"
end