Class: Alf::Types::AttrList
- Inherits:
-
Object
- Object
- Alf::Types::AttrList
- 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
-
#<=>(other) ⇒ Integer
(also: #set_compare)
Compares this attribute list with ‘other` on a set basis.
-
#allbut(attrs) ⇒ Object
Shortcut for project(attrs, true).
-
#intersect?(other) ⇒ Boolean
Returns true if self intersects another attribute list.
-
#project(attrs, allbut = false) ⇒ AttrList
Projects this attribute list on a subset of attributes ‘attrs`.
-
#project_tuple(tuple, allbut = false) ⇒ Object
Projects ‘tuple` on known (or unknown) attributes.
-
#sameset?(other) ⇒ Boolean
Returns true if this attribute list captures the same set of attribute names than ‘other`.
-
#split_tuple(tuple, allbut = false) ⇒ Array<Hash>
Splits a tuple in two parts according ‘allbut`.
-
#subset?(other, proper = false) ⇒ Boolean
(also: #subset_of?)
Returns true if self is a (proper) subset of ‘other`.
-
#superset?(other, proper = false) ⇒ Boolean
(also: #supserset_of?)
Returns true if self is a (proper) superset of ‘other`.
-
#to_attr_list ⇒ AttrList
Converts to an attribute list.
-
#to_lispy ⇒ String
Returns a lispy expression.
-
#to_ordering(order = :asc) ⇒ Ordering
Converts this attribute list to an ordering.
-
#to_ruby_literal ⇒ String
(also: #to_s, #inspect)
Returns a ruby literal for this attribute list.
Methods included from Support::OrderedSet
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.
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
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`.
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"}
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`.
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"}]
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`
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`
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_list ⇒ AttrList
Converts to an attribute list.
125 126 127 |
# File 'lib/alf/types/attr_list.rb', line 125 def to_attr_list self end |
#to_lispy ⇒ String
Returns a lispy expression.
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.
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_literal ⇒ String Also known as: to_s, inspect
Returns a ruby literal for this attribute list.
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 |