Module | Sequel::Model::InstanceMethods |
In: |
lib/sequel/model/base.rb
|
Sequel::Model instance methods that implement basic model functionality.
values | [R] | The hash of attribute values. Keys are symbols with the names of the underlying database columns. |
Creates new instance and passes the given values to set. If a block is given, yield the instance to the block unless from_db is true. This method runs the after_initialize hook after it has optionally yielded itself to the block.
Arguments:
# File lib/sequel/model/base.rb, line 523 523: def initialize(values = {}, from_db = false) 524: if from_db 525: @new = false 526: @values = values 527: else 528: @values = {} 529: @new = true 530: set(values) 531: changed_columns.clear 532: yield self if block_given? 533: end 534: after_initialize 535: end
Compares model instances by values.
# File lib/sequel/model/base.rb, line 561 561: def ==(obj) 562: (obj.class == model) && (obj.values == @values) 563: end
Returns value of the column‘s attribute.
# File lib/sequel/model/base.rb, line 538 538: def [](column) 539: @values[column] 540: end
Sets value of the column‘s attribute and marks the column as changed. If the column already has the same value, this is a no-op. Note that changing a columns value and then changing it back will cause the column to appear in changed_columns. Similarly, providing a value that is different from the column‘s current value but is the same after typecasting will also cause changed_columns to include the column.
# File lib/sequel/model/base.rb, line 549 549: def []=(column, value) 550: # If it is new, it doesn't have a value yet, so we should 551: # definitely set the new value. 552: # If the column isn't in @values, we can't assume it is 553: # NULL in the database, so assume it has changed. 554: if new? || !@values.include?(column) || value != @values[column] 555: changed_columns << column unless changed_columns.include?(column) 556: @values[column] = typecast_value(column, value) 557: end 558: end
The current cached associations. A hash with the keys being the association name symbols and the values being the associated object or nil (many_to_one), or the array of associated objects (*_to_many).
# File lib/sequel/model/base.rb, line 581 581: def associations 582: @associations ||= {} 583: end
Like delete but runs hooks before and after delete. If before_destroy returns false, returns false without deleting the object the the database. Otherwise, deletes the item from the database and returns self. Uses a transaction if use_transactions is true.
# File lib/sequel/model/base.rb, line 610 610: def destroy 611: use_transactions ? db.transaction{_destroy} : _destroy 612: end
Returns a string representation of the model instance including the class name and values.
# File lib/sequel/model/base.rb, line 648 648: def inspect 649: "#<#{model.name} @values=#{inspect_values}>" 650: end
Whether this object has been modified since last saved, used by save_changes to determine whether changes should be saved
# File lib/sequel/model/base.rb, line 659 659: def modified? 660: !changed_columns.empty? 661: end
Returns the primary key value identifying the model instance. Raises an error if this model does not have a primary key. If the model has a composite primary key, returns an array of values.
# File lib/sequel/model/base.rb, line 671 671: def pk 672: raise(Error, "No primary key is associated with this model") unless key = primary_key 673: key.is_a?(Array) ? key.map{|k| @values[k]} : @values[key] 674: end
Reloads attributes from database and returns self. Also clears all cached association and changed_columns information. Raises an Error if the record no longer exists in the database.
# File lib/sequel/model/base.rb, line 686 686: def refresh 687: _refresh(this) 688: end
Creates or updates the record, after making sure the record is valid. If the record is not valid, or before_save, before_create (if new?), or before_update (if !new?) return false, returns nil unless raise_on_save_failure is true (if it is true, it raises an error). Otherwise, returns self. You can provide an optional list of columns to update, in which case it only updates those columns.
Takes the following options:
# File lib/sequel/model/base.rb, line 708 708: def save(*columns) 709: opts = columns.last.is_a?(Hash) ? columns.pop : {} 710: return save_failure(:invalid) if opts[:validate] != false and !valid? 711: use_transaction = opts.include?(:transaction) ? opts[:transaction] : use_transactions 712: use_transaction ? db.transaction(opts){_save(columns, opts)} : _save(columns, opts) 713: end
Saves only changed columns if the object has been modified. If the object has not been modified, returns nil. If unable to save, returns false unless raise_on_save_failure is true.
# File lib/sequel/model/base.rb, line 718 718: def save_changes 719: save(:changed=>true) || false if modified? 720: end
Updates the instance with the supplied values with support for virtual attributes, raising an exception if a value is used that doesn‘t have a setter method (or ignoring it if strict_param_setting = false). Does not save the record.
# File lib/sequel/model/base.rb, line 726 726: def set(hash) 727: set_restricted(hash, nil, nil) 728: end
Runs set with the passed hash and runs save_changes (which runs any callback methods).
# File lib/sequel/model/base.rb, line 754 754: def update(hash) 755: update_restricted(hash, nil, nil) 756: end
Validates the object and returns true if no errors are reported.
# File lib/sequel/model/base.rb, line 783 783: def valid? 784: errors.clear 785: if before_validation == false 786: save_failure(:validation) 787: return false 788: end 789: validate 790: after_validation 791: errors.empty? 792: end