class ActiveFacts::Metamodel::D

Constants

AlternateNames

Alternate names are case-insensitive, and underscore can be literal, or correspond to a space or to nothing

TypeNames
TypeParameters

Public Class Methods

choose_integer(type_name, length = nil, value_constraint = nil, context = DefaultContext.new) click to toggle source

Integers are available in multiple sizes. Choose the most appropriate one.

# File lib/activefacts/metamodel/datatypes.rb, line 135
def self.choose_integer type_name, length = nil, value_constraint = nil, context = DefaultContext.new
  int_length = length || context.default_length(TYPE_Integer, type_name)
  if int_length
    if value_constraint
      # Pick out the largest maximum and smallest minimum from the ValueConstraint:
      ranges = value_constraint.all_allowed_range_sorted.flat_map{|ar| ar.value_range}
      min = ranges.map(&:minimum_bound).compact.map{|minb| minb.is_inclusive ? minb.value : minb.value-1}.map{|v| v.literal.to_i}.sort[0]
      max = ranges.map(&:maximum_bound).compact.map{|maxb| maxb.is_inclusive ? maxb.value : maxb.value+1}.map{|v| v.literal.to_i}.sort[-1]
    end

    unsigned = type_name =~ /^unsigned/i
    int_min = unsigned ? 0 : -2**(int_length-1)+1
    min = int_min if !min || length && int_min < min
    # SQL does not have unsigned types.
    # Don't force the next largest type just because the app calls for unsigned:
    int_max = unsigned ? 2**(int_length-1) - 1 : 2**(int_length-1)-1
    max = int_max if !max || length && int_max < max
  end
  best = context.choose_integer_range(min, max)
  unless best || length
    # Use the largest available integer size
    best = context.integer_ranges.last
  end

  # Use a context-defined integer size if one suits, otherwise the requested size:
  if best
    best[0]
  else
    nil   # No integer seems suitable
  end
end
intrinsic_type(type_name) click to toggle source
# File lib/activefacts/metamodel/datatypes.rb, line 129
def self.intrinsic_type type_name
  data_type, = type_mapping.detect{|t, names| names.detect{|n| n === type_name}}
  data_type
end

Private Class Methods

type_mapping() click to toggle source
# File lib/activefacts/metamodel/datatypes.rb, line 168
def self.type_mapping
  if DataType.const_defined?("TypeMapping")
    return TypeMapping
  end
  DataType.const_set("TypeMapping",
    AlternateNames.inject({}) do |h, (t, a)|
      h[t] = [TypeNames[t]]
      a.each do |n|
        h[t] << /^#{n.gsub(/_/, '[ _]?')}$/i
      end
      h
    end
  )
  TypeMapping
end