Class: Cheri::Builder::TypeConnecter

Inherits:
Object
  • Object
show all
Defined in:
lib/cheri/builder/connecter.rb

Defined Under Namespace

Classes: SCtr, Sym, TCtr, Type

Instance Method Summary collapse

Constructor Details

#initialize(*parents, &block) ⇒ TypeConnecter

Returns a new instance of TypeConnecter.



30
31
32
33
34
35
36
# File 'lib/cheri/builder/connecter.rb', line 30

def initialize(*parents,&block)
  @t = {}
  @s = {}
  @c = {}
  parents.each do |parent| merge!(parent); end
  instance_eval(&block) if block
end

Instance Method Details

#add_type(mod) ⇒ Object

used by simple builder-builder



155
156
157
# File 'lib/cheri/builder/connecter.rb', line 155

def add_type(mod) #:nodoc:
  @t[mod] ||= Type.new(mod)
end

#merge!(other) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cheri/builder/connecter.rb', line 38

def merge!(other)
  raise Cheri.type_error(other,TypeConnecter) unless TypeConnecter === other
  t = @t
  s = @s
  other.get_types.each_pair do |mod,type|
    if (tmod = t[mod])
      tmod.merge(type)
    else
      t[mod] = type.copy
    end
  end
  other.get_syms.each_pair do |sym,symc|
    if (ssym = s[sym])
      ssym.merge(symc)
    else
      s[sym] = symc.copy
    end
  end
  self
end

#prepare(ctx, builder, obj, sym, props) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cheri/builder/connecter.rb', line 59

def prepare(ctx,builder,obj,sym,props)
  # try parent symbol matches first
  if (ssym = @s[builder.sym])
    # child symbol match?
    if (sctr = ssym.sctrs[sym])
      return sctr.prepare(ctx,builder,obj,sym,props) 
    end
    # child type match?
    unless ssym.tctrs.empty?
      anc = (class<<obj;self;end).ancestors
      ix = anc.length
      match = nil
      # match on nearest ancestor
      ssym.tctrs.each do |tctr|
        if (cix = anc.index(tctr.mod)) && cix < ix
          ix = cix
          match = tctr
        end
      end
      return match.prepare(ctx,builder,obj,sym,props) if match
    end
  end
  # then try parent type matches
  t = @t
  bld_anc = (class<<builder.object;self;end).ancestors
  anc ||= (class<<obj;self;end).ancestors
  key = [sym,bld_anc,anc]
  if (ctr = @c[key])
    return ctr.prepare(ctx,builder,obj,sym,props)
  end
  bld_anc.each do |a|
    if (type = t[a])
      # child symbol match?
      if (sctr = type.sctrs[sym])
        @c[key] = sctr
        return sctr.prepare(ctx,builder,obj,sym,props) 
      end
      # child type match?
      #anc ||= (class<<obj;self;end).ancestors
      ix ||= anc.length
      match = nil
      # match on nearest ancestor
      type.tctrs.each do |tctr|
        if (cix = anc.index(tctr.mod)) && cix < ix
          ix = cix
          match = tctr
        end
      end
      if match
        @c[key] = match
        return match.prepare(ctx,builder,obj,sym,props)
      end
    end
  end
  false
end

#type(mod, &k) ⇒ Object Also known as: types, symbol, symbols



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cheri/builder/connecter.rb', line 116

def type(mod,&k)
  if Module === mod
    tp = @t[mod] ||= Type.new(mod)
    tp.instance_eval(&k) if k
  elsif Symbol === mod
    sm = @s[mod] ||= Sym.new(mod)
    sm.instance_eval(&k) if k
  elsif Array === mod
    mod.each do |m|
      if Symbol === m
        sm = @s[m] ||= Sym.new(m)
        sm.instance_eval(&k) if k
      elsif Module === m
        tp = @t[m] ||= Type.new(m)
        tp.instance_eval(&k) if k
      else
        raise Cheri.type_error(m,Symbol,Class,Module)
      end
    end    
  else
    raise Cheri.type_error(mod,Class,Module,Symbol,Array)
  end
  nil
end