Class: Locomotive::RelationalAlgebra::Schema

Inherits:
Object
  • Object
show all
Includes:
XML
Defined in:
lib/locomotive/relational_algebra/schema.rb

Overview

The Schema contains the attributes and its associated types. Each operator of the relational algebra contains a schema.

Instance Method Summary collapse

Methods included from XML

included, #quote

Constructor Details

#initialize(hash) ⇒ Schema

Returns a new instance of Schema.



33
34
35
# File 'lib/locomotive/relational_algebra/schema.rb', line 33

def initialize(hash)
  self.schema = hash
end

Instance Method Details

#+(schm) ⇒ Object

merges two schemas, given that there are no duplicate keys



48
49
50
51
52
53
54
55
56
# File 'lib/locomotive/relational_algebra/schema.rb', line 48

def +(schm)
  if duplicates?(schm)
    raise Duplicates,
          "Found duplicates in #{self.attributes} " \
          "and #{schm.attributes}."
  end
  # create a new schema
  Schema.new(schema.merge(schm.schema))
end

#[]=(attr, types) ⇒ Object

Raises:



59
60
61
62
63
64
# File 'lib/locomotive/relational_algebra/schema.rb', line 59

def []=(attr,types)
  raise Duplicates, 
         "#{attr.inspect} results in duplicates " \
         "in schema #{self.attributes}" unless self[attr].nil?
  schema[attr] = types
end

#attributesObject



37
38
39
# File 'lib/locomotive/relational_algebra/schema.rb', line 37

def attributes
  schema.keys
end

#attributes?(attributes) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/locomotive/relational_algebra/schema.rb', line 41

def attributes?(attributes)
  attributes.all? { |attr| self.attributes.member? attr }
end

#cloneObject



80
81
82
# File 'lib/locomotive/relational_algebra/schema.rb', line 80

def clone
  Schema.new( self.schema.clone )
end

#inspectObject



76
77
78
# File 'lib/locomotive/relational_algebra/schema.rb', line 76

def inspect
  "<Schema [#{schema.map { |s| s.inspect }.join(", ")}]>"
end

#to_xmlObject



67
68
69
70
71
72
73
74
# File 'lib/locomotive/relational_algebra/schema.rb', line 67

def to_xml
  _schema_ do
    self.schema.collect do |attr,types|
      col :name => attr.to_xml,
          :type => types.collect { |ty| ty.to_xml }.join(",")
    end.join
  end
end