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

#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.



103
104
105
106
107
108
109
110
111
112
# File 'lib/data_factory/base_dsl.rb', line 103

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



117
118
119
120
121
122
123
124
125
# File 'lib/data_factory/base_dsl.rb', line 117

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



130
131
132
133
134
135
136
137
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
# File 'lib/data_factory/base_dsl.rb', line 130

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,
                              data_length,
                              data_precision,
                              data_scale,
                              column_id,
                              nullable
                       from user_tab_columns
                       where table_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
  @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.



91
92
93
94
95
96
97
# File 'lib/data_factory/base_dsl.rb', line 91

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 'my_table'
  populate_nullable_columns
end


81
82
83
# File 'lib/data_factory/base_dsl.rb', line 81

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.



68
69
70
71
# File 'lib/data_factory/base_dsl.rb', line 68

def set_table_name(tab)
  @table_name = tab.to_s.upcase
  @populate_nullable_columns = false
end