Class: Domain::HeadingBased::DomainMethods

Inherits:
Module
  • Object
show all
Defined in:
lib/alf/ext/domain/heading_based.rb

Instance Method Summary collapse

Constructor Details

#initialize(master_class, gt) ⇒ DomainMethods

Returns a new instance of DomainMethods.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/alf/ext/domain/heading_based.rb', line 32

def initialize(master_class, gt)
  define_method(:generating_type){
    gt
  }
  define_method(:dum){
    @dum ||= new(EMPTY_SET)
  }
  define_method(:<=>){|other|
    return 0 if self == other
    return nil unless other.ancestors.include?(master_class)
    return -1 if other == master_class
    to_heading <=> other.to_heading
  }
  define_method(:===){|value|
    super(value) || (value.is_a?(master_class) && self >= value.class)
  }
  define_method(:hash){
    @hash ||= 37*master_class.hash + generating_type.hash
  }
  define_method(:==){|other|
    other.is_a?(Class) &&
    other.superclass==master_class &&
    other.to_heading==to_heading
  }
  define_method(:coerce){|arg|
    return arg if self==arg.class
    master_class.coercions.apply(arg, self)
  }
  define_method(:to_heading){
    @heading ||= Alf::Heading.coerce(generating_type)
  }
  define_method(:to_tuple_type){
    return self if Alf::Tuple==master_class
    return gt   if gt.is_a?(Class) && gt < Alf::Tuple
    @tuple_type ||= Alf::Tuple[to_heading]
  }
  define_method(:recursive?){
    h = to_heading
    h.to_attr_list.any?{|a| h[a] == self}
  }
  define_method(:to_ruby_literal){
    recursive? ?
      "#{master_class.name}[...]" :
      "#{master_class.name}[#{Alf::Support.to_ruby_literal(to_heading.to_hash)}]"
  }
  alias_method :name, :to_ruby_literal
  alias_method :to_s, :to_ruby_literal
  alias_method :inspect, :to_ruby_literal
end