Module: DataFactory::BaseDSL

Included in:
Base
Defined in:
lib/data_factory/base_dsl.rb

Overview

This module is used to extend the DataFactory::Base class, and so creates a series of class instance methods that can be used in class definitions.

For example:

class Foo < DataFactory::Base

  set_table_name "Foobar"
  set_column_default :last_name, 'Smith'

end

For this reason, calling any of these methods on a class, will affect ALL instances of that class.

Any subclasses expect the class variables @@db_interface to be set, containing a database access class, eg:

DataFactory::Base.set_database_interface(interface_obj)

As this is stored in a class variable the same database interface is shared down the entire inheritance chain. Therefore if many database connections are required, several base classes will need to be created by including the relevant modules, eg:

class OtherBaseClass
  extend BaseDSL
  extend BaseFactory
  include BaseAPI
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#column_defaultsObject (readonly)

Returns the value of attribute column_defaults.



35
36
37
# File 'lib/data_factory/base_dsl.rb', line 35

def column_defaults
  @column_defaults
end

#column_detailsObject (readonly)

Returns the value of attribute column_details.



35
36
37
# File 'lib/data_factory/base_dsl.rb', line 35

def column_details
  @column_details
end

#meta_data_loadedObject (readonly)

Returns the value of attribute meta_data_loaded.



35
36
37
# File 'lib/data_factory/base_dsl.rb', line 35

def 
  @meta_data_loaded
end

#populate_nullable_columnsObject (readonly)

Returns the value of attribute populate_nullable_columns.



35
36
37
# File 'lib/data_factory/base_dsl.rb', line 35

def populate_nullable_columns
  @populate_nullable_columns
end

#schema_nameObject (readonly)

Returns the value of attribute schema_name.



35
36
37
# File 'lib/data_factory/base_dsl.rb', line 35

def schema_name
  @schema_name
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



35
36
37
# File 'lib/data_factory/base_dsl.rb', line 35

def table_name
  @table_name
end

Instance Method Details

#column_default(column_name) ⇒ Object

Returns the value for a column default. If it is a simple value, the value is returned. If the default is a proc, the proc is executed and the resulting values is returned.



111
112
113
114
115
116
117
118
119
120
# File 'lib/data_factory/base_dsl.rb', line 111

def column_default(column_name)
  return nil unless defined? @column_defaults

  val = @column_defaults[column_name.to_s.upcase]
  if val.is_a?(Proc)
    val.call
  else
    val
  end
end

#column_detail(column_name) ⇒ Object

Returns a DataFactory::Column object that defines the properties of the column



125
126
127
128
129
130
131
132
133
# File 'lib/data_factory/base_dsl.rb', line 125

def column_detail(column_name)
   unless 

  unless @column_details.has_key?(column_name.to_s.upcase)
    raise DataFactory::ColumnNotInTable, "Column #{column_name.to_s.upcase} is not in #{table_name}"
  end

  @column_details[column_name.to_s.upcase]
end

#database_interfaceObject

Returns the database interface that was set by set_database_interface



60
61
62
# File 'lib/data_factory/base_dsl.rb', line 60

def database_interface
  class_variable_get("@@db_interface")
end

#load_meta_dataObject

Used to load the table meta-data from the database TODO - abstract into a database layer as this code is Oracle specific



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
# File 'lib/data_factory/base_dsl.rb', line 138

def  # :nodoc:
  raise DataFactory::TableNotSet unless @table_name
  raise DataFactory::DatabaseInterfaceNotSet unless class_variable_defined?("@@db_interface")

  if 
    return
  end

  @column_details  = Hash.new

  unless defined? @column_defaults
    @column_defaults = Hash.new
  end

  table_details_sql = "select column_name,
                              data_type,
                              nvl(char_length, data_length),
                              data_precision,
                              data_scale,
                              column_id,
                              nullable
                       from all_tab_columns
                       where table_name = ?
                       and owner = #{@schema_name.nil? ? 'user' : "'#{@schema_name}'"}
                       order by column_id asc"

  database_interface.execute_sql(table_details_sql, @table_name).each_array do |r|
    c = Column.new
    c.column_name     = r[0].upcase
    c.data_type       = r[1].upcase
    c.data_length     = r[2].to_i
    c.data_precision  = r[3].to_i
    c.data_scale      = r[4].to_i
    c.position        = r[5].to_i
    c.nullable        = r[6] == 'N' ? false : true
    @column_details[r[0].upcase] = c
  end
  # If there are no columns, then the table does not exist!
  if @column_details.keys.length == 0
    raise DataFactory::TableNotExist, "Table #{schema_name.nil? ? '' : schema_name+'.'}#{table_name} does not exist"
  end
  @meta_data_loaded = true
  # This is needed here as some column defaults will have been set
  # before the meta_data was loaded and hence will not have been checked
  validate_column_defaults
end

#set_column_default(column_name, value = nil, &block) ⇒ Object

Sets the default value to be used for a column if nothing else is specified. The block is optional and should be used if dynamic defaults are required. If the default is a constant, a simple value can be specified. The default is stored within a hash which is stored in a class instance variable (@column_defaults) that is shared by all instances of the class, but is not inherited with subclasses.



99
100
101
102
103
104
105
# File 'lib/data_factory/base_dsl.rb', line 99

def set_column_default(column_name, value=nil, &block)
  unless defined? @column_defaults
    @column_defaults = Hash.new
  end
  @column_defaults[column_name.to_s.upcase] = block_given? ? block :
    value.is_a?(Symbol) ? value.to_s : value
end

#set_database_interface(interface) ⇒ Object

Pass a database interface object to be used by all DataFactory sub-classes The interface must implement the following two methods:

def execute_sql(statement, *binds)
end

def each_array(&blk)
end

def commit
end

This method is normally called on the DataFactory::Base class. It will set a class variable (@@db_interface) in the Base class which is shared by all sub-classes of the class.

Ideally use the SimpleOracleJDBC gem to provide the interface



54
55
56
# File 'lib/data_factory/base_dsl.rb', line 54

def set_database_interface(interface)
  class_variable_set("@@db_interface", interface)
end

#set_populate_nullable_columnsObject

By default, no values will be generated for columns that can be null. By calling this method in the class defintion, it will set @populate_nullable_columns to true, which will cause values to be generated for nullable columns in the same way as for not null columns

Examples:

class MyTab < DataFactory::Base
  set_table_name 'schema.my_table'
  populate_nullable_columns
end


89
90
91
# File 'lib/data_factory/base_dsl.rb', line 89

def set_populate_nullable_columns
  @populate_nullable_columns = true
end

#set_table_name(tab) ⇒ Object

Defines the table a subclass of DataFactory interacts with on the database. This method stores the tables in a class instance variable (@table_name) that is shared by all instances of the class, but is not inherited with subclasses. The table name can optionally be prefixed with the schema - ‘schema.table’



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/data_factory/base_dsl.rb', line 69

def set_table_name(tab)
  parts = tab.to_s.upcase.split(/\./)
  if parts.length == 2
    @table_name = parts[1]
    @schema_name = parts[0]
  else
    @table_name = parts[0]
    @schema_name = nil
  end
  @populate_nullable_columns = false
end