Module: ArPerfToolkit::Insert
- Defined in:
- lib/insert.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #create_or_update_x(options = {}) ⇒ Object
-
#create_x(options = {}) ⇒ Object
Creates a new record with values matching those of the instance attributes.
-
#replace_x(options = {}) ⇒ Object
Replace deletes the existing duplicate if it exists then adds this.
-
#save_ignore ⇒ Object
ClassMethods.
- #save_ignore! ⇒ Object
- #save_x(options = {}) ⇒ Object
- #save_x!(options = {}) ⇒ Object
-
#update_x(options = {}) ⇒ Object
Updates the associated record with values matching those of the instance attributes.
Class Method Details
.included(base) ⇒ Object
:nodoc:
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/insert.rb', line 20 def self.included(base) #:nodoc: base.extend(ClassMethods) #alias methods base.class_eval do alias_method_chain_arperf(%w(save update save! create_or_update)) class << self alias_method_chain_arperf(%w(create create!)) end end end |
Instance Method Details
#create_or_update_x(options = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/insert.rb', line 93 def create_or_update_x ={} return false if callback(:before_save) == false [:keywords] = 'IGNORE' if .delete(:ignore).kind_of?(TrueClass) result = if new_record? then create_x() else .delete(:duplicate) update_x() end callback(:after_save) result end |
#create_x(options = {}) ⇒ Object
Creates a new record with values matching those of the instance attributes.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/insert.rb', line 126 def create_x ={} return false if callback(:before_create) == false (true) if self.id.nil? && connection.prefetch_primary_key?(self.class.table_name) self.id = connection.next_sequence_value(self.class.sequence_name) end [:command]||='INSERT' sql = self.class.construct_sql() do |sql, | sql << "INTO #{self.class.table_name} (#{quoted_column_names.join(', ')}) " sql << "VALUES(#{attributes_with_quotes.values.join(', ')})" end self.id = connection.insert(sql, "#{self.class.name} CreateX ", self.class.primary_key, self.id, self.class.sequence_name) @new_record = false callback(:after_create) return self.id != 0 end |
#replace_x(options = {}) ⇒ Object
Replace deletes the existing duplicate if it exists then adds this
151 152 153 |
# File 'lib/insert.rb', line 151 def replace_x ={} create_x(.merge(:command => 'REPLACE')) end |
#save_ignore ⇒ Object
ClassMethods
66 67 68 |
# File 'lib/insert.rb', line 66 def save_ignore save_x :keywords => 'IGNORE' end |
#save_ignore! ⇒ Object
70 71 72 |
# File 'lib/insert.rb', line 70 def save_ignore! save_ignore || raise(ActiveRecord::RecordNotSaved) end |
#save_x(options = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/insert.rb', line 74 def save_x(={}) #invoke save_with_validation if the argument is not a hash return () unless .is_a?(Hash) perform_validation = .delete(:perform_validation) if (perform_validation.is_a?(FalseClass)) || valid? raise ActiveRecord::ReadOnlyRecord if readonly? create_or_update_x else raise ActiveRecord::RecordInvalid.new(self) if perform_validation == :exception false end end |
#save_x!(options = {}) ⇒ Object
88 89 90 91 |
# File 'lib/insert.rb', line 88 def save_x!(={}) [:perform_validation] = :exception save_x() || raise(ActiveRecord::RecordNotSaved) end |
#update_x(options = {}) ⇒ Object
Updates the associated record with values matching those of the instance attributes.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/insert.rb', line 110 def update_x ={} return false if callback(:before_update) == false (false) [:command]||='UPDATE' raise(ArgumentError, "Unknown key: duplicate") if [:duplicate] sql = self.class.construct_sql() do |sql, | sql << "#{self.class.table_name} " sql << "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " + "WHERE (#{self.class.primary_key} = #{quote_value(id)}) " end connection.update(sql, "#{self.class.name} UpdateX") callback(:after_update) return true end |