Class: Axiom::Attribute

Inherits:
Object
  • Object
show all
Extended by:
Aliasable, DescendantsTracker
Includes:
AbstractType, Visitable, Comparable
Defined in:
lib/axiom/attribute.rb,
lib/axiom/attribute/date.rb,
lib/axiom/attribute/time.rb,
lib/axiom/attribute/class.rb,
lib/axiom/attribute/float.rb,
lib/axiom/attribute/tuple.rb,
lib/axiom/attribute/object.rb,
lib/axiom/attribute/string.rb,
lib/axiom/attribute/boolean.rb,
lib/axiom/attribute/decimal.rb,
lib/axiom/attribute/integer.rb,
lib/axiom/attribute/numeric.rb,
lib/axiom/attribute/relation.rb,
lib/axiom/attribute/date_time.rb,
lib/axiom/attribute/comparable.rb,
lib/axiom/attribute/value_comparable.rb,
lib/axiom/attribute/length_comparable.rb

Overview

Abstract base class representing a type of data in a relation tuple

Direct Known Subclasses

Object

Defined Under Namespace

Modules: Comparable, LengthComparable, ValueComparable Classes: Boolean, Class, Date, DateTime, Decimal, Float, Integer, Numeric, Object, Relation, String, Time, Tuple

Constant Summary collapse

TrueClass =

Add aliases for Boolean

Boolean
FalseClass =
Boolean

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Aliasable

inheritable_alias

Methods included from Visitable

#accept

Constructor Details

#initialize(name, options = EMPTY_HASH) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize an Attribute

Parameters:

  • name (#to_sym)

    the attribute name

  • options (Hash) (defaults to: EMPTY_HASH)

    the options for the attribute

Options Hash (options):

  • :required (Boolean) — default: true

    if true, then the value cannot be nil



90
91
92
93
94
# File 'lib/axiom/attribute.rb', line 90

def initialize(name, options = EMPTY_HASH)
  @name     = name.to_sym
  @required = options.fetch(:required, true)
  @type     = self.class.type
end

Instance Attribute Details

#nameSymbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The attribute name

Returns:

  • (Symbol)


18
19
20
# File 'lib/axiom/attribute.rb', line 18

def name
  @name
end

#typeClass<Types::Object> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The attribute type

Returns:



25
26
27
# File 'lib/axiom/attribute.rb', line 25

def type
  @type
end

Class Method Details

.coerce(object) ⇒ Attribute

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerce an object into an Attribute

Parameters:

  • object (Attribute, #to_ary, #to_sym)

    the object to coerce

Returns:



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/axiom/attribute.rb', line 35

def self.coerce(object)
  if object.kind_of?(Attribute)
    object
  elsif equal?(Attribute)
    Object.coerce(object)
  else
    name, type, options = object
    klass = type ? const_get(type.name) : self
    klass.new(name, options || EMPTY_HASH)
  end
end

.infer_type(operand) ⇒ Class<Attribute>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Infer the Attribute type from the operand

Parameters:

Returns:



70
71
72
73
74
75
76
# File 'lib/axiom/attribute.rb', line 70

def self.infer_type(operand)
  if operand.respond_to?(:type)
    operand.type
  else
    Types::Type.descendants.detect { |type| type.include?(operand) }
  end
end

.name_from(object) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract the attribute name from the object

Parameters:

  • object (#name, #to_ary, #to_a)

    the object to extract a name from

Returns:

  • (Symbol)


55
56
57
58
59
60
61
# File 'lib/axiom/attribute.rb', line 55

def self.name_from(object)
  if object.respond_to?(:name)
    object.name
  else
    Array(object).first.to_sym
  end
end

Instance Method Details

#call(tuple) ⇒ Object

Extract the value corresponding to this attribute from a tuple

Examples:

value = attribute.call(tuple)

Parameters:

  • tuple (Tuple)

    the tuple to extract the value from

Returns:



107
108
109
# File 'lib/axiom/attribute.rb', line 107

def call(tuple)
  tuple[self]
end

#include?(value) ⇒ Boolean

Test if the value matches the attribute constraints

Examples:

attribute.include?(value)  # => true or false

Parameters:

  • value (Object)

    the value to test

Returns:



167
168
169
# File 'lib/axiom/attribute.rb', line 167

def include?(value)
  valid_or_optional?(value, &type.method(:include?))
end

#optional?Boolean

Test if the attribute is optional

Examples:

attribute.optional?  # => true or false

Returns:



152
153
154
# File 'lib/axiom/attribute.rb', line 152

def optional?
  !required?
end

#rename(new_name) ⇒ Attribute

TODO:

Make this have the same API as functions

Rename an attribute

Examples:

new_attribute = attribute.rename(new_name)

Parameters:

  • new_name (Symbol)

    the new name to rename the attribute to

Returns:



124
125
126
127
128
129
130
# File 'lib/axiom/attribute.rb', line 124

def rename(new_name)
  if name.equal?(new_name)
    self
  else
    self.class.new(new_name, required: required?)
  end
end

#required?Boolean

Test if the attribute is required

Examples:

attribute.required?  # => true or false

Returns:



140
141
142
# File 'lib/axiom/attribute.rb', line 140

def required?
  @required
end