Class: Lore::Model_Factory
- Inherits:
-
Object
- Object
- Lore::Model_Factory
- Defined in:
- lib/lore/model_factory.rb
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_primary_key(key_hash) ⇒ Object
Usage: add_primary_key(:attribute => ‘my_id’, :key_name => ‘my_model_pkey’) or: add_primary_key(:attributes => [‘id_1’, ‘id_2’], :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 |
# File 'lib/lore/model_factory.rb', line 21 def initialize(model_name, connection=nil) name_parts = model_name.split('::') @model_name = name_parts[-1] @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 @constraints = Array.new @schema_name = :public @primary_keys = Array.new @attribute_types = 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_factory.rb', line 11 def aggregates @aggregates end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
11 12 13 |
# File 'lib/lore/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_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_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_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_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_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_factory.rb', line 11 def types @types end |
Class Method Details
.drop_model(model) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/lore/model_factory.rb', line 49 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 => '...')
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/lore/model_factory.rb', line 72 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].to_s if attrib_hash[:length].instance_of? Integer then attribute_part << '(' << attrib_hash[:length].to_s + ')' end attribute_part << ' UNIQUE ' if attrib_hash[:unique] attribute_part << ' NOT NULL ' if attrib_hash[:not_null] @attributes << attribute_part @attribute_types[attrib_name] = attrib_hash[:type] end |
#add_has_a(model, attribute_name) ⇒ Object
97 98 99 |
# File 'lib/lore/model_factory.rb', line 97 def add_has_a(model, attribute_name) @aggregates << [:has_a, model, attribute_name] end |
#add_is_a(model, attribute_name) ⇒ Object
101 102 103 |
# File 'lib/lore/model_factory.rb', line 101 def add_is_a(model, attribute_name) @aggregates << [:is_a, model, attribute] end |
#add_label(label) ⇒ Object
105 106 107 |
# File 'lib/lore/model_factory.rb', line 105 def add_label(label) @labels << label.downcase end |
#add_primary_key(key_hash) ⇒ Object
Usage:
add_primary_key(:attribute => 'my_id', :key_name => 'my_model_pkey')
or:
add_primary_key(:attributes => ['id_1', 'id_2'], :key_name => 'my_model_pkey')
117 118 119 120 121 |
# File 'lib/lore/model_factory.rb', line 117 def add_primary_key(key_hash) key_hash[:attribute] = key_hash[:attribute].to_s key_hash[:key_name] = key_hash[:key_name].to_s @primary_keys << [ key_hash[:attribute], key_hash[:attribute]+'_seq', key_hash[:key_name] ] end |
#build_model_klass ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/lore/model_factory.rb', line 143 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
159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 |
# File 'lib/lore/model_factory.rb', line 159 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 { |attr, type| model << ' has_attribute :' << attr + ', Lore::Type.' << type 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/lore/model_factory.rb', line 126 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" } 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
87 88 89 90 91 |
# File 'lib/lore/model_factory.rb', line 87 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
109 110 111 |
# File 'lib/lore/model_factory.rb', line 109 def set_labels(labels) @labels = labels end |
#set_table_space(table_space) ⇒ Object
93 94 95 |
# File 'lib/lore/model_factory.rb', line 93 def set_table_space(table_space) @table_space = table_space end |
#use_model(model_klass_string, model_klass_file) ⇒ Object
57 58 59 |
# File 'lib/lore/model_factory.rb', line 57 def use_model(model_klass_string, model_klass_file) @base_model_klass = model_klass_string end |