Class: Lore::Model_Factory
Instance Attribute Summary collapse
-
#aggregates ⇒ Object
readonly
Returns the value of attribute aggregates.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#labels ⇒ Object
readonly
Returns the value of attribute labels.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#output_file ⇒ Object
Returns the value of attribute output_file.
-
#output_folder ⇒ Object
Returns the value of attribute output_folder.
-
#primary_keys ⇒ Object
readonly
Returns the value of attribute primary_keys.
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_attribute(attrib_name, attrib_hash = {}) ⇒ Object
Usage: .
- #add_has_a(model, attribute_name) ⇒ Object
- #add_is_a(model, attribute_name) ⇒ Object
- #add_label(label) ⇒ Object
- #add_mandatory(attribute_name) ⇒ Object
-
#add_primary_key(key_hash) ⇒ Object
Usage: add_primary_key(:attribute => ‘my_id’, :key_name => ‘my_model_pkey’).
- #build_model_klass ⇒ Object
- #build_model_klass_string ⇒ Object
-
#build_table ⇒ Object
Finally installs model table.
-
#initialize(model_name, connection = nil) ⇒ Model_Factory
constructor
Usage: builder = Model_Builder.new(‘new_model’) builder.add_attribute(‘model_id’, :type => Type.integer, :not_null => true) builder.add_attribute(‘name’, :type => Type.integer, :not_null => true, :check => “name <> ””, :unique => true) builder.add_attribute(‘created’, :type => Type.timestamp, :not_null => true, :default => ‘now()’) builder.add_primary_key(:key_name => ‘model_pkey’, :attribute => ‘model_id’) builder.set_table_space(‘diskvol1’) builder.build().
- #set_attributes(attrib_hash) ⇒ Object
- #set_labels(labels) ⇒ Object
- #set_table_space(table_space) ⇒ Object
- #use_model(model_klass_string, model_klass_file) ⇒ Object
Constructor Details
#initialize(model_name, connection = nil) ⇒ Model_Factory
Usage:
builder = Model_Builder.new('new_model')
builder.add_attribute('model_id', :type => Type.integer, :not_null => true)
builder.add_attribute('name', :type => Type.integer, :not_null => true, :check => "name <> ''", :unique => true)
builder.add_attribute('created', :type => Type., :not_null => true, :default => 'now()')
builder.add_primary_key(:key_name => 'model_pkey', :attribute => 'model_id')
builder.set_table_space('diskvol1')
builder.build()
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lore/model/model_factory.rb', line 21 def initialize(model_name, connection=nil) name_parts = model_name.to_s.split('::') @model_name = name_parts[-1] @model_name = @model_name.split('_').map { |p| p.upcase }.join('_') @namespaces = name_parts[0..-2] @table_name = @model_name.downcase @output_folder = './' @output_file = @table_name + '.rb' @connection = connection @connection = Lore::Connection unless @connection @table_space = '' @fields = Array.new @types = Array.new @labels = Array.new @aggregates = Array.new @attributes = Array.new @expects = Array.new @constraints = Array.new @schema_name = :public @primary_keys = Array.new @attribute_types = Hash.new @attribute_labels = Hash.new @base_model_klass = 'Lore::Model' @base_model_klass_file = 'lore/model' end |
Instance Attribute Details
#aggregates ⇒ Object (readonly)
Returns the value of attribute aggregates.
11 12 13 |
# File 'lib/lore/model/model_factory.rb', line 11 def aggregates @aggregates end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
11 12 13 |
# File 'lib/lore/model/model_factory.rb', line 11 def fields @fields end |
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
11 12 13 |
# File 'lib/lore/model/model_factory.rb', line 11 def labels @labels end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
11 12 13 |
# File 'lib/lore/model/model_factory.rb', line 11 def model_name @model_name end |
#output_file ⇒ Object
Returns the value of attribute output_file.
10 11 12 |
# File 'lib/lore/model/model_factory.rb', line 10 def output_file @output_file end |
#output_folder ⇒ Object
Returns the value of attribute output_folder.
10 11 12 |
# File 'lib/lore/model/model_factory.rb', line 10 def output_folder @output_folder end |
#primary_keys ⇒ Object (readonly)
Returns the value of attribute primary_keys.
11 12 13 |
# File 'lib/lore/model/model_factory.rb', line 11 def primary_keys @primary_keys end |
#types ⇒ Object (readonly)
Returns the value of attribute types.
11 12 13 |
# File 'lib/lore/model/model_factory.rb', line 11 def types @types end |
Class Method Details
.drop_model(model) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/lore/model/model_factory.rb', line 52 def self.drop_model(model) query = 'DROP TABLE ' << model.table_name + ';' model.get_sequences()[model.table_name].each { |key, seq| query << 'DROP SEQUENCE ' << seq.to_s + ";\n" } Lore::Connection.perform(query) end |
Instance Method Details
#add_attribute(attrib_name, attrib_hash = {}) ⇒ Object
Usage:
add_attribute('primary_id',
:type => integer,
:not_null => true,
:default => '0',
:length => 20,
:check => '...')
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/lore/model/model_factory.rb', line 75 def add_attribute(attrib_name, attrib_hash={}) @fields << { :name => attrib_name, :type => attrib_hash[:type] } attribute_part = '' attribute_part << "#{attrib_name} \t #{attrib_hash[:type]}" if attrib_hash[:length].instance_of? Integer then attribute_part << '(' << attrib_hash[:length].to_s + ')' end if attrib_hash[:mandatory] || attrib_hash[:null].instance_of?(FalseClass) || attrib_hash[:not_null] then add_mandatory(attrib_name) attribute_part << ' NOT NULL ' end attribute_part << ' UNIQUE ' if attrib_hash[:unique] @attributes << attribute_part @attribute_types[attrib_name] = attrib_hash[:type] @attribute_labels[attrib_name] = attrib_hash[:label] end |
#add_has_a(model, attribute_name) ⇒ Object
106 107 108 |
# File 'lib/lore/model/model_factory.rb', line 106 def add_has_a(model, attribute_name) @aggregates << [:has_a, model, attribute_name] end |
#add_is_a(model, attribute_name) ⇒ Object
115 116 117 |
# File 'lib/lore/model/model_factory.rb', line 115 def add_is_a(model, attribute_name) @aggregates << [:is_a, model, attribute] end |
#add_label(label) ⇒ Object
119 120 121 |
# File 'lib/lore/model/model_factory.rb', line 119 def add_label(label) @labels << label.downcase end |
#add_mandatory(attribute_name) ⇒ Object
110 111 112 113 |
# File 'lib/lore/model/model_factory.rb', line 110 def add_mandatory(attribute_name) @expects << attribute_name.to_s @expects.uniq! end |
#add_primary_key(key_hash) ⇒ Object
Usage:
add_primary_key(:attribute => 'my_id', :key_name => 'my_model_pkey')
130 131 132 133 134 135 |
# File 'lib/lore/model/model_factory.rb', line 130 def add_primary_key(key_hash) key_hash[:attribute] = key_hash[:attribute].to_s key_hash[:key_name] = key_hash[:key_name].to_s sequence = key_hash[:sequence] @primary_keys << [ key_hash[:attribute], sequence, key_hash[:key_name] ] end |
#build_model_klass ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/lore/model/model_factory.rb', line 157 def build_model_klass build_table model_klass_string = build_model_klass_string if @output_file then out = @output_folder + @output_file prepend_header = !File.exists?(out) File.open(out, 'a+') { |f| f << "\nrequire('" << @base_model_klass_file + "') \n\n" if prepend_header prepend_header = false f << model_klass_string f << "\n\n" } end global_eval(model_klass_string) end |
#build_model_klass_string ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/lore/model/model_factory.rb', line 173 def build_model_klass_string requires = '' @aggregates.each { |type, model_name, attribute| requires << "require('aurita/main/model/" << model_name.downcase + "')" requires << "\n" } requires << "\n" model = requires model << ' class ' << @model_name.to_s + ' < ' << @base_model_klass + " \n" model << ' table :' << @table_name.to_s model << ', :' << @schema_name.to_s model << "\n" @primary_keys.each { |key| model << ' primary_key :' << key[0] model << ', :' << key[1] if key[1] model << "\n" } @attribute_types.each_pair { |attrib, type| model << ' # has_attribute :' << attrib + ', Lore::Type.' << type model << "\n" } model << "\n" model << " def self.attribute_labels\n" model << " { \n" @attribute_labels.each_pair { |attrib, label| model << " '#{attrib}' => '#{label}',\n" } model << " :none => ''\n" model << " } \n" model << " end\n" @expects.each { |attrib| model << ' expects :' << attrib model << "\n" } model << ' use_label :' << @labels.join(', :') if @labels.first model << "\n" @aggregates.each { |type, model_name, attribute_name| model << ' has_a ' << model_name + ', :' << attribute_name.downcase if type==:has_a model << ' is_a ' << model_name + ', :' << attribute_name.downcase if type==:is_a model << "\n" } model << "\n" << ' end' @namespaces.reverse.each { |ns| model = 'module ' << ns + "\n" << model << "\n" << 'end' } return model end |
#build_table ⇒ Object
Finally installs model table
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/lore/model/model_factory.rb', line 140 def build_table query = '' pkey_constraint = "CONSTRAINT #{@table_name}_pkey PRIMARY KEY (#{@primary_keys.collect { |pk| pk[0] }.join(',')})\n" @primary_keys.each { |pk| query << "CREATE SEQUENCE #{pk[1]}; \n" if pk[1] } query << 'CREATE TABLE ' << @table_name + " ( \n" query << @attributes.join(", \n") query << ', ' << "\n" query << pkey_constraint query << "\n" << ') ' << @table_space + ';' @connection.perform(query) end |
#set_attributes(attrib_hash) ⇒ Object
96 97 98 99 100 |
# File 'lib/lore/model/model_factory.rb', line 96 def set_attributes(attrib_hash) attrib_hash.each_pair { |attrib_name, attrib_props| add_attribute(attrib_name.to_s, attrib_props) } end |
#set_labels(labels) ⇒ Object
123 124 125 |
# File 'lib/lore/model/model_factory.rb', line 123 def set_labels(labels) @labels = labels end |
#set_table_space(table_space) ⇒ Object
102 103 104 |
# File 'lib/lore/model/model_factory.rb', line 102 def set_table_space(table_space) @table_space = table_space end |
#use_model(model_klass_string, model_klass_file) ⇒ Object
60 61 62 |
# File 'lib/lore/model/model_factory.rb', line 60 def use_model(model_klass_string, model_klass_file) @base_model_klass = model_klass_string end |