Class: Qrb::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/qrb/support/attribute.rb

Overview

Helper class for tuple and relation attributes.

An attribute is simply a ‘(name: AttrName, type: Type)` pair, where the type is a Q type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type) ⇒ Attribute

Returns a new instance of Attribute.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/qrb/support/attribute.rb', line 10

def initialize(name, type)
  unless name.is_a?(Symbol)
    raise ArgumentError, "Symbol expected for attribute name, got `#{name}`"
  end

  unless type.is_a?(Type)
    raise ArgumentError, "Type expected for attribute domain, got `#{type}`"
  end

  @name, @type = name, type
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/qrb/support/attribute.rb', line 21

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



21
22
23
# File 'lib/qrb/support/attribute.rb', line 21

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



42
43
44
45
# File 'lib/qrb/support/attribute.rb', line 42

def ==(other)
  return nil unless other.is_a?(Attribute)
  name==other.name and type==other.type
end

#fetch_on(arg, &bl) ⇒ Object

Fetch the attribute on ‘arg`, which is expected to be a Hash object.

This method allows working with ruby hashes having either Symbols or Strings as keys. It ensures that no Symbol is created by the rest of the code, since this would provide a DoS attack vector under MRI.



29
30
31
32
33
34
35
36
# File 'lib/qrb/support/attribute.rb', line 29

def fetch_on(arg, &bl)
  unless arg.respond_to?(:fetch)
    raise ArgumentError, "Object responding to `fetch` expected"
  end
  arg.fetch(name) do
    arg.fetch(name.to_s, &bl)
  end
end

#hashObject



48
49
50
# File 'lib/qrb/support/attribute.rb', line 48

def hash
  name.hash ^ type.hash
end

#to_nameObject



38
39
40
# File 'lib/qrb/support/attribute.rb', line 38

def to_name
  "#{name}: #{type}"
end