class Axiom::Attribute

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

Constants

FalseClass

Represents a Boolean value in a relation tuple

TrueClass

Represents a Boolean value in a relation tuple

Attributes

name[R]

The attribute name

@return [Symbol]

@api private

type[R]

The attribute type

@return [Class<Types::Object>]

@api private

Public Class Methods

coerce(object) click to toggle source

Coerce an object into an Attribute

@param [Attribute, to_ary, to_sym] object

the object to coerce

@return [Attribute]

@api private

# 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) click to toggle source

Infer the Attribute type from the operand

@param [Object] operand

@return [Class<Attribute>]

@api private

# 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) click to toggle source

Extract the attribute name from the object

@param [#name, to_ary, to_a] object

the object to extract a name from

@return [Symbol]

@api private

# 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
new(name, options = EMPTY_HASH) click to toggle source

Initialize an Attribute

@param [#to_sym] name

the attribute name

@param [Hash] options

the options for the attribute

@option options [Boolean] :required (true)

if true, then the value cannot be nil

@return [undefined]

@api private

# 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

Public Instance Methods

call(tuple) click to toggle source

Extract the value corresponding to this attribute from a tuple

@example

value = attribute.call(tuple)

@param [Tuple] tuple

the tuple to extract the value from

@return [Object]

@api public

# File lib/axiom/attribute.rb, line 107
def call(tuple)
  tuple[self]
end
include?(value) click to toggle source

Test if the value matches the attribute constraints

@example

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

@param [Object] value

the value to test

@return [Boolean]

@api public

# File lib/axiom/attribute.rb, line 167
def include?(value)
  valid_or_optional?(value, &type.method(:include?))
end
optional?() click to toggle source

Test if the attribute is optional

@example

attribute.optional?  # => true or false

@return [Boolean]

@api public

# File lib/axiom/attribute.rb, line 152
def optional?
  !required?
end
rename(new_name) click to toggle source

Rename an attribute

@example

new_attribute = attribute.rename(new_name)

@param [Symbol] new_name

the new name to rename the attribute to

@return [Attribute]

@todo Make this have the same API as functions

@api public

# 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?() click to toggle source

Test if the attribute is required

@example

attribute.required?  # => true or false

@return [Boolean]

@api public

# File lib/axiom/attribute.rb, line 140
def required?
  @required
end

Private Instance Methods

coerce(*args) click to toggle source

Coerce the object into an Attribute

@param [Array] args

@return [Attribute]

@api private

# File lib/axiom/attribute.rb, line 194
def coerce(*args)
  self.class.coerce(*args)
end
valid_or_optional?(value) { |value| ... } click to toggle source

Test that the value is either valid or optional

@param [Object] value

the value to test

@yield [] block representing attribute constraint

@return [Boolean]

@api private

# File lib/axiom/attribute.rb, line 183
def valid_or_optional?(value)
  value.nil? ? optional? : yield(value)
end