Module: DataFactory::BaseAPI
Overview
This module is included into the DataFactory::Base class providing methods to generate data and insert it into the database.
For most use cases, these methods will not need to be called. The DataFactory::BaseFactory module provides factory methods that create objects and insert the data into database
Constant Summary
Constants included from Random
Instance Attribute Summary collapse
-
#binds ⇒ Object
readonly
Returns the value of attribute binds.
-
#column_values ⇒ Object
readonly
Returns the value of attribute column_values.
-
#insert_statement ⇒ Object
readonly
Returns the value of attribute insert_statement.
Instance Method Summary collapse
-
#column_default(key) ⇒ Object
Retrieves the default value set for a column, which can be a proc or any Ruby object in general, but in practice is likely to be a Date, Time, String, Float, Integer.
-
#column_defaults ⇒ Object
:nodoc:.
-
#column_detail(key) ⇒ Object
:nodoc:.
-
#column_details ⇒ Object
:nodoc:.
-
#column_value(key) ⇒ Object
Returns the value assigned to a column, or nil if it is not defined.
-
#commit ⇒ Object
Commit changes to the database.
-
#db_interface ⇒ Object
Returns the database interface object.
-
#generate_column_data(params = Hash.new) ⇒ Object
Generates values for all the columns in the table.
-
#generate_insert ⇒ Object
Generates an insert statement for the current state of the object.
- #initialize ⇒ Object
-
#run_insert ⇒ Object
Runs the insert statement prepared by generate_insert, using the insert statement stored in @insert_statement and the bind variables in @binds.
-
#table_name ⇒ Object
Retrieves the table name this class interfaces with on the database.
-
#where_clause_for(cols) ⇒ Object
Generates a where clause for the DataFactory object.
Methods included from Random
#random_integer, #random_string_of_length, #random_string_upto_length
Instance Attribute Details
#binds ⇒ Object (readonly)
Returns the value of attribute binds.
14 15 16 |
# File 'lib/data_factory/base_api.rb', line 14 def binds @binds end |
#column_values ⇒ Object (readonly)
Returns the value of attribute column_values.
14 15 16 |
# File 'lib/data_factory/base_api.rb', line 14 def column_values @column_values end |
#insert_statement ⇒ Object (readonly)
Returns the value of attribute insert_statement.
14 15 16 |
# File 'lib/data_factory/base_api.rb', line 14 def insert_statement @insert_statement end |
Instance Method Details
#column_default(key) ⇒ Object
Retrieves the default value set for a column, which can be a proc or any Ruby object in general, but in practice is likely to be a Date, Time, String, Float, Integer
43 44 45 |
# File 'lib/data_factory/base_api.rb', line 43 def column_default(key) self.class.column_default(key) end |
#column_defaults ⇒ Object
:nodoc:
32 33 34 |
# File 'lib/data_factory/base_api.rb', line 32 def column_defaults # :nodoc: self.class.column_defaults end |
#column_detail(key) ⇒ Object
:nodoc:
36 37 38 |
# File 'lib/data_factory/base_api.rb', line 36 def column_detail(key) # :nodoc: self.class.column_detail(key) end |
#column_details ⇒ Object
:nodoc:
28 29 30 |
# File 'lib/data_factory/base_api.rb', line 28 def column_details # :nodoc: self.class.column_details end |
#column_value(key) ⇒ Object
Returns the value assigned to a column, or nil if it is not defined
48 49 50 51 52 |
# File 'lib/data_factory/base_api.rb', line 48 def column_value(key) # ensure the requested column is in the table column_detail(key) @column_values[key.to_s.upcase] end |
#commit ⇒ Object
Commit changes to the database
60 61 62 |
# File 'lib/data_factory/base_api.rb', line 60 def commit db_interface.commit end |
#db_interface ⇒ Object
Returns the database interface object
55 56 57 |
# File 'lib/data_factory/base_api.rb', line 55 def db_interface self.class.database_interface end |
#generate_column_data(params = Hash.new) ⇒ Object
Generates values for all the columns in the table.
If no value is passed into this procedure for a column, a default will be used if it is configured. Otherwise a random value will be generated.
Values for a column can be passed into this method using a hash, eg:
obj.generate_column_data(:emp_id => 1, :last_name => 'Smith)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/data_factory/base_api.rb', line 72 def generate_column_data(params=Hash.new) self.class.column_details.keys.each do |k| if column_default(k) if column_default(k).is_a?(Proc) @column_values[k] = column_default(k).call else @column_values[k] = column_default(k) end else @column_values[k] = generate_value(column_detail(k)) end end normalised_params = internalize_column_params(params) validate_columns(normalised_params) @column_values.merge! normalised_params end |
#generate_insert ⇒ Object
Generates an insert statement for the current state of the object. It is intended to be called after generate_column_data has been called, otherwise all the values will be null.
The generated statement returned as a string and also stored in the @insert_statement instance variable.
The insert statement will contain bind variables for all columns. The generated bind variabled are stored in @binds.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/data_factory/base_api.rb', line 99 def generate_insert @binds = Array.new @insert_statement = "insert into #{table_name} (" @insert_statement << column_details.keys.sort.map { |k| column_detail(k).column_name }.join(',') @insert_statement << ') values (' @insert_statement << column_details.keys.sort.map { |k| ":#{k}" }.join(',') column_details.keys.sort.each { |k| if @column_values[k] == nil @binds.push [column_type_to_ruby_type(column_details[k]), nil] else @binds.push @column_values[k] end } @insert_statement << ')' @insert_statement end |
#initialize ⇒ Object
16 17 18 19 20 21 |
# File 'lib/data_factory/base_api.rb', line 16 def initialize unless self.class. self.class. end @column_values = Hash.new end |
#run_insert ⇒ Object
Runs the insert statement prepared by generate_insert, using the insert statement stored in @insert_statement and the bind variables in @binds
If generate_insert has not be executed, this procedure will raise a DataFactory::NoInsertStatement exeception
123 124 125 126 127 128 |
# File 'lib/data_factory/base_api.rb', line 123 def run_insert raise DataFactory::NoInsertStatement unless @insert_statement stmt = db_interface.execute_sql(@insert_statement, *@binds) stmt.close end |
#table_name ⇒ Object
Retrieves the table name this class interfaces with on the database
24 25 26 |
# File 'lib/data_factory/base_api.rb', line 24 def table_name self.class.table_name end |
#where_clause_for(cols) ⇒ Object
Generates a where clause for the DataFactory object. This method looks at the columns on the table, and the values set against them, and generates a string representing a where clause that can be used in an SQL query.
The generated string does not contain the ‘where’ keyword.
135 136 137 138 139 140 141 142 143 |
# File 'lib/data_factory/base_api.rb', line 135 def where_clause_for(cols) cols.map{|c| c.to_s.upcase}.map{|c| if @column_values[c] == nil "#{c} is null" else "#{c} = #{quote_value(c) }" end }.join " and " end |