Class: Model

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_name, model_class, var = nil) ⇒ Model

Returns a new instance of Model.



15
16
17
18
19
20
21
22
# File 'lib/model.rb', line 15

def initialize model_name, model_class, var=nil
  @model_name= model_name
  @model_class= model_class
  #read the model attributes
  @model_attributes = get_model_attributes model_name,model_class
  @model = var ? var : get_model
  @mapping = Hash[ "binary"=>"base64Binary","boolean"=>"boolean","date"=>"date","datetime"=>"dateTime","decimal"=>"decimal","float"=>"float","integer"=>"integer","string"=>"string","text"=>"string","time"=>"time","timestamp"=>"dateTime",]
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



6
7
8
# File 'lib/model.rb', line 6

def model
  @model
end

#model_attributesObject

Returns the value of attribute model_attributes.



8
9
10
# File 'lib/model.rb', line 8

def model_attributes
  @model_attributes
end

#model_classObject

Returns the value of attribute model_class.



9
10
11
# File 'lib/model.rb', line 9

def model_class
  @model_class
end

#model_nameObject

Returns the value of attribute model_name.



7
8
9
# File 'lib/model.rb', line 7

def model_name
  @model_name
end

Instance Method Details

#get_const(input) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/model.rb', line 29

def get_const input
  #remove function name
  input = input.to_s
  input = input[6..input.index(')')-1]

  const = input.index(",")? input.split(",")[0]:input
  datatype = input.index(",")? input.split(",")[1]: "xsd:String"
  return [const,datatype]
end

#get_datatypesObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/model.rb', line 105

def get_datatypes   
  dtypes = Hash.new
  @model.columns_hash.each_key do |m|
    #make xsd datatye
    dtypes[m.to_s] ="xsd:#{@mapping[@model.columns_hash[m].type.to_s] }"
  end
  #replace mapping
  @model_attributes.each do |k,v|
    if ((v.include? "*") ||(v.include? "." )) && v.to_s[0..5]!="Model("
      field,model = k.to_s.split "->"
      unless model
        #todo: get datatype..thats a little bit tricky at this point
#          v.gsub!("*",".")
#          a,db_field= v.to_s.split "."
#          t = eval("#{model}.columns_hash[db_field.to_s].type.to_s.downcase")
#          dtypes[k.to_s]="xsd:#{@mapping[t]}"
      end 
    else
      dtypes[k.to_s]=dtypes[v.to_s]
    end
  end
  dtypes
end

#get_filter(remove = false) ⇒ Object

*returns the filter of the model *@remove if filter should be removed from model_attributes list */



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/model.rb', line 43

def get_filter remove=false
  filter=nil
  if model_attributes.has_key? :filter
    filter=""
    model_attributes[:filter].to_hash.each do |a,b|
      filter += (filter.blank? ? "" : " and ") +  a.to_s.downcase+b.to_s
    end      
    model_attributes.delete :filter if remove
  end
  filter
end

#get_includeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/model.rb', line 55

def get_include
  ret = Array.new
  #    model_attributes.select {|k,v| k.to_s.include?("->")}.each do |attribute|
  #      ref_model = attribute[0].to_s.split("->")[1]
  #      get_model_model(@model_class).each do |r_mod|
  #        ret.push(r_mod.to_s.capitalize.to_sym)
  #      end
  #    end

  model_attributes.select {|k,v| v.to_s.include?("*")}.each do |attribute|
    m_class,field=attribute[1].to_s.split("*")
    #get model
    ret.push(m_class.downcase.to_sym) unless ret.index(m_class.downcase.to_sym) || m_class[0..5]=="MODEL("
  end

  model_attributes.select {|k,v| v.to_s.include?(".")}.each do |attribute|
    ref_model = attribute[1].split(".")[0].to_s.downcase
    #get model
    ret.push(ref_model.to_sym) unless ret.index(ref_model.to_sym)
  end
  ret
end

#get_keyObject



176
177
178
179
180
181
182
183
# File 'lib/model.rb', line 176

def get_key
  id_keys = @model_attributes.to_hash.keys
  #hotfix..bad performance
  id_keys.map!{|k| k.to_s }
  id_key= id_keys.select{|k| k =~/^(ID|id|iD|Id)$/ }
  return :id if id_key.empty?
  return @model_attributes[id_key[0].to_sym]      
end

#get_modelObject



25
26
27
# File 'lib/model.rb', line 25

def get_model
  eval(@model_name.to_s)
end

#get_model_attributes(key, model_class) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/model.rb', line 78

def get_model_attributes key, model_class
  model_groups =  eval("configatron.query").to_hash
  model_groups.each do |model_group_name,model_group|
    if model_group_name.to_s==model_class.to_s
      hello="hello"
      model_group.each do |model_name_query,attributes|
        return attributes if model_name_query.to_s.downcase == key.to_s.downcase
      end
    end
  end
  nil
end

#get_model_model(model_class) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/model.rb', line 91

def get_model_model model_class
  model_groups =  eval("configatron.query").to_hash
  model_groups.each do |model_group_name,model_group|
    if model_group_name==model_class
      return model_group.keys
    end
  end
  nil
end

#get_row_by_id(id) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/model.rb', line 153

def get_row_by_id id
  #make filter
  t = @model
  filter = get_filter(true)
  include = get_include
  find_command = "find_all_by_#{get_key.to_s.downcase}"

  if include.empty?
    if filter
      t = eval("t.#{find_command}(#{id}, :conditions =>filter)")
    else
      t = eval("t.#{find_command}(#{id})")
    end
  else
    if filter
      t = eval("t.#{find_command}(#{id},:include => include, :conditions => filter)")
    else
      t = eval("t.#{find_command}(\"#{id}\",:include => include)")
    end
  end
  t
end

#get_rowsObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/model.rb', line 129

def get_rows
  #make filter

  t = @model
  return t if t.class == Array
  filter = get_filter(true)
  include = get_include
    
  if include.empty?
    if filter
      t = t.find(:all, :conditions =>filter)
    else
      t = t.find(:all)
    end  
  else
    if filter
      t = t.find(:all,:include => include, :conditions => filter)
    else
      t = t.find(:all,:include => include)
    end    
  end
  t
end

#read_attributesObject



101
102
103
# File 'lib/model.rb', line 101

def read_attributes
  get_model_attributes @model_name
end