Class: Alf::Relation
- Inherits:
-
Object
- Object
- Alf::Relation
- Includes:
- Algebra::Operand, Lang::ObjectOriented, Enumerable
- Defined in:
- lib/alf-relation/alf/relation.rb
Overview
Defines an in-memory relation data structure.
A relation is a set of tuples; a tuple is a set of attribute (name, value) pairs. The class implements such a data structure with full relational algebra installed as instance methods.
Relation values can be obtained in various ways, for example by invoking a relational operator on an existing relation. Relation literals are simply constructed as follows:
Alf::Relation([
# ... a comma list of ruby hashes ...
])
See main Alf documentation about relational operators.
Constant Summary collapse
Instance Attribute Summary
Attributes included from Support::Bindable
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #check_internal_representation! ⇒ Object
-
#heading ⇒ Object
Returns the relation heading.
-
#initialize(tuples) ⇒ Relation
constructor
A new instance of Relation.
-
#to_attr_list ⇒ Object
Returns the attribute list.
-
#to_cog ⇒ Object
Returns an engine Cog.
-
#to_relvar ⇒ Object
Returns a ReadOnly relvar.
-
#to_ruby_literal ⇒ Object
(also: #inspect)
Returns a literal representation of this relation.
-
#to_s ⇒ Object
Returns a textual representation of this relation.
Methods included from Lang::ObjectOriented
Methods included from Lang::ObjectOriented::RenderingMethods
def_renderer_method, #to_a, #to_array
Methods included from Lang::ObjectOriented::AlgebraMethods
#!~, #&, #*, #+, #-, #=~, #allbut, def_operator_method, #tuple_extract
Methods included from Lang::ObjectOriented::AggregationMethods
Methods included from Algebra::Operand
#attr_list, coerce, #keys, #to_dot, #to_relation
Methods included from Support::Bindable
Constructor Details
Instance Method Details
#[](*args) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/alf-relation/alf/relation.rb', line 61 def [](*args) attrs = args.map{|arg| arg.is_a?(Hash) ? arg.keys : arg }.flatten handler = ->(v){ v.is_a?(Symbol) ? ->{ __send__(v) } : v } extension = args.each_with_object({}) do |arg, ext| case arg when Symbol ext[arg] = handler[arg] when Hash arg.each_pair do |k,v| ext[k] = handler[v] end end end self.extend(extension).project(attrs) end |
#check_internal_representation! ⇒ Object
50 51 52 53 54 55 |
# File 'lib/alf-relation/alf/relation.rb', line 50 def check_internal_representation! error = lambda{|msg| raise TypeError, msg } error["Set expected"] unless reused_instance.is_a?(Set) error["Superclass mismatch"] unless self.class.superclass == Relation self end |
#heading ⇒ Object
Returns the relation heading
78 79 80 |
# File 'lib/alf-relation/alf/relation.rb', line 78 def heading self.class.heading end |
#to_attr_list ⇒ Object
Returns the attribute list.
83 84 85 |
# File 'lib/alf-relation/alf/relation.rb', line 83 def to_attr_list heading.to_attr_list end |
#to_cog ⇒ Object
Returns an engine Cog
93 94 95 |
# File 'lib/alf-relation/alf/relation.rb', line 93 def to_cog Engine::Leaf.new(self) end |
#to_relvar ⇒ Object
Returns a ReadOnly relvar
88 89 90 |
# File 'lib/alf-relation/alf/relation.rb', line 88 def to_relvar Relvar::ReadOnly.new(self) end |
#to_ruby_literal ⇒ Object Also known as: inspect
Returns a literal representation of this relation
103 104 105 |
# File 'lib/alf-relation/alf/relation.rb', line 103 def to_ruby_literal "Alf::Relation([" + tuples.map{|t| Support.to_ruby_literal(t) }.join(', ') + "])" end |
#to_s ⇒ Object
Returns a textual representation of this relation
98 99 100 |
# File 'lib/alf-relation/alf/relation.rb', line 98 def to_s to_text end |