Class: Models

Inherits:
Object
  • Object
show all
Defined in:
lib/models.rb

Instance Method Summary collapse

Instance Method Details

#childline(c) ⇒ Object



105
106
107
# File 'lib/models.rb', line 105

def childline(c)
  "has_one :#{pname(c)}#{c.name}"
end

#classline(e) ⇒ Object



102
103
104
# File 'lib/models.rb', line 102

def classline(e)
  "class #{pname(e)}#{e.name} < ActiveRecord::Base"
end

#createClass(parent, list) ⇒ Object



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
# File 'lib/models.rb', line 62

def createClass(parent,list)
  list.each do |e|
    e.parent = parent if parent
    if (e.coverage)
      @entitycoverage = e.name
    end
    #build up the node name clauses
    @nodeName << "#{classline(e)}\n"
    nodeclauses(e.name)

    @entityModel << "#{classline(e)}\n"
    #puts "classline for #{e.name} is #{classline(e)}"
    #iterate around the children and build the entity model
    e.children.each do |c|
      c.parent = e
      @entityModel << "#{childline(c)}\n"
    end
    @entityModel << "end\n"
    @nodeName << "end\n"

    #now take the children in turn
    createClass(e,e.children) if e.children.length > 0
    if (e.coverage)
      @result[e.name.to_sym] = []
      @result[e.name.to_sym][0] = @entityModel
      @result[e.name.to_sym][1] = @nodeName
      @result[e.name.to_sym][3] = e.type
      @entityModel = ""
      @nodeName = ""
    end
  end
end

#createDictionaryEntries(list) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/models.rb', line 124

def createDictionaryEntries(list)
  list.each do |e|
    fieldPrefix = "'#{pname(e)}#{e.name}".downcase
    e.fields.each do |f|
      key = "#{pname(e)}#{e.name}#{f[:name]}"
      if !@distinctLabels.has_key?(key.to_sym)
        @dictionary << "\t\t\t#{fieldPrefix}_#{f[:name].downcase}' => '#{f[:name]}',\n"
        @distinctLabels[key.to_sym] = 1
      end
    end
    createDictionaryEntries(e.children) if e.children.length > 0
  end
end

#createHash(parent, list) ⇒ Object

– build entity model and propertyhashes files to plug into IAB library



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/models.rb', line 30

def createHash(parent,list)
  list.each do |e|
    e.parent = parent if parent
    if (e.coverage)
      @entitycoverage = e.name
    end
    @propertyHash << "#{hashline(e)}\n"
      e.fields.each do |f|
        @propertyHash << "#{fieldline(f)}\n"
        @fieldDECs << "#{fieldDEC(e,f)}\n"
      end
    createHash(e,e.children) if e.children.length > 0
    if (e.coverage)
      @result[e.name.to_sym][2] = @propertyHash + @fieldDECs
      @result[e.name.to_sym][3] = e.type
      @propertyHash = ""
      @fieldDECs = ""
    end
  end
end

#fieldDEC(e, f) ⇒ Object



58
59
60
# File 'lib/models.rb', line 58

def fieldDEC(e,f)
  "#{pname(e)}#{e.name}#{f[:name]}: !map:HashWithIndifferentAccess\n  xpath: \"#{pname2(e,'/')}#{e.name}/#{f}\""
end

#fieldline(f) ⇒ Object



54
55
56
57
# File 'lib/models.rb', line 54

def fieldline(f)
  mask = f[:mask] ? "\n    mask: \"#{f[:mask]}\"" : ""
  "  #{f[:name]}: \"\"\n  #{f[:name]}MD: !map:HashWithIndifferentAccess\n    type: \"#{f[:stype]}\"#{mask}"
end

#genDict(result) ⇒ Object



22
23
24
25
26
27
# File 'lib/models.rb', line 22

def genDict(result)
  @distinctLabels = Hash.new
  @dictionary = ""
  #createDictionaryEntries(result)
  @dictionary
end

#generateFiles(result) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/models.rb', line 5

def generateFiles(result)
  list = (result.collect {|x| x if x.coverage}).compact
  @result = Hash.new
  #collect statement reduces the list to a list of coverages only
  @entitycoverage = ""
  @entityModel = ""
  @nodeName = ""
  createClass(nil,list)

  @entitycoverage = ""
  @propertyHash = ""
  @fieldDECs=""
  createHash(nil,list)

  @result
end

#hashline(e) ⇒ Object



51
52
53
# File 'lib/models.rb', line 51

def hashline(e)
  "#{pname(e)}#{e.name}: !map:HashWithIndifferentAccess\n  xpath: \"#{pname2(e,'/')}#{e.name}\""
end

#nodeclauses(n) ⇒ Object



95
96
97
98
99
100
# File 'lib/models.rb', line 95

def nodeclauses(n)
  @nodeName << "\t@@nodeName = \"#{n}\"\n"
  @nodeName << "\tdef self.nodeName\n"
  @nodeName << "\t@@nodeName\n"
  @nodeName << "\tend\n"
end

#pname(e) ⇒ Object



109
110
111
# File 'lib/models.rb', line 109

def pname(e)
  pname2(e,"")
end

#pname2(e, delimiter) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/models.rb', line 113

def pname2(e,delimiter)
  pname = ""
  parent = e.parent
  while parent
    pname = parent.name + delimiter + pname
    parent = parent.parent
  end
  pname =  pname if !e.coverage
  pname
end