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/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/date_time.rb,
lib/axiom/attribute/comparable.rb

Overview

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

Direct Known Subclasses

Object

Defined Under Namespace

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

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
  @options  = freeze_object(options)
  @required = @options.fetch(:required, true)
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)


16
17
18
# File 'lib/axiom/attribute.rb', line 16

def name
  @name
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:



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

def self.coerce(object)
  if object.kind_of?(Attribute)
    object
  else
    name, type, options = object
    klass = equal?(Attribute) ? Object : self
    klass = const_get(type.name) if type
    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:



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

def self.infer_type(operand)
  case operand
  when Attribute, Function, Aggregate then operand.type
  when FalseClass                     then Boolean
  else
    type = operand.class
    descendants.detect { |descendant| type <= descendant.primitive }
  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)


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

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.call(self)
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
# File 'lib/axiom/attribute.rb', line 124

def rename(new_name)
  name.equal?(new_name) ? self : self.class.new(new_name, options)
end

#required?Boolean

Test if the attribute is required

Examples:

attribute.required?  # => true or false

Returns:



145
146
147
# File 'lib/axiom/attribute.rb', line 145

def required?
  @required
end

#typeClass<Attribute>

Return the type returned from #call

Returns:



133
134
135
# File 'lib/axiom/attribute.rb', line 133

def type
  self.class
end

#valid_primitive?(value) ⇒ Boolean

Test if a value is the correct primitive type

Examples:

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

Parameters:

  • value (Object)

    the value to test

Returns:



160
161
162
# File 'lib/axiom/attribute.rb', line 160

def valid_primitive?(value)
  value.kind_of?(self.class.primitive)
end

#valid_value?(value) ⇒ Boolean

Test if the value matches the attribute constraints

Examples:

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

Parameters:

  • value (Object)

    the value to test

Returns:



175
176
177
# File 'lib/axiom/attribute.rb', line 175

def valid_value?(value)
  valid_or_optional?(value) { valid_primitive?(value) }
end