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 525 525: def initialize(values = {}, from_db = false) 526: if from_db 527: @new = false 528: set_values(values) 529: else 530: @values = {} 531: @new = true 532: @modified = true 533: set(values) 534: changed_columns.clear 535: yield self if block_given? 536: end 537: after_initialize 538: end
Compares model instances by values.
# File lib/sequel/model/base.rb, line 562 562: def ==(obj) 563: (obj.class == model) && (obj.values == @values) 564: end
Returns value of the column‘s attribute.
# File lib/sequel/model/base.rb, line 541 541: def [](column) 542: @values[column] 543: end
Sets the value for the given column. If typecasting is enabled for this object, typecast the value based on the column‘s type. If this a a new record or the typecasted value isn‘t the same as the current value for the column, mark the column as changed.
# 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: v = typecast_value(column, value) 555: if new? || !@values.include?(column) || v != @values[column] 556: changed_columns << column unless changed_columns.include?(column) 557: @values[column] = v 558: end 559: 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 582 582: def associations 583: @associations ||= {} 584: 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 611 611: def destroy 612: use_transactions ? db.transaction{_destroy} : _destroy 613: end
Returns a string representation of the model instance including the class name and values.
# File lib/sequel/model/base.rb, line 649 649: def inspect 650: "#<#{model.name} @values=#{inspect_values}>" 651: end
Remove elements of the model object that make marshalling fail. Returns self.
# File lib/sequel/model/base.rb, line 659 659: def marshallable! 660: @this = nil 661: self 662: end
Explicitly mark the object as modified, so save_changes/update will run callbacks even if no columns have changed.
# File lib/sequel/model/base.rb, line 666 666: def modified! 667: @modified = true 668: end
Whether this object has been modified since last saved, used by save_changes to determine whether changes should be saved. New values are always considered modified.
# File lib/sequel/model/base.rb, line 673 673: def modified? 674: @modified || !changed_columns.empty? 675: 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 685 685: def pk 686: raise(Error, "No primary key is associated with this model") unless key = primary_key 687: key.is_a?(Array) ? key.map{|k| @values[k]} : @values[key] 688: 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 700 700: def refresh 701: _refresh(this) 702: 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 722 722: def save(*columns) 723: opts = columns.last.is_a?(Hash) ? columns.pop : {} 724: return save_failure(:invalid) if opts[:validate] != false and !valid? 725: use_transaction = opts.include?(:transaction) ? opts[:transaction] : use_transactions 726: use_transaction ? db.transaction(opts){_save(columns, opts)} : _save(columns, opts) 727: 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 732 732: def save_changes(opts={}) 733: save(opts.merge(:changed=>true)) || false if modified? 734: 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 740 740: def set(hash) 741: set_restricted(hash, nil, nil) 742: end
Runs set with the passed hash and then runs save_changes.
# File lib/sequel/model/base.rb, line 768 768: def update(hash) 769: update_restricted(hash, nil, nil) 770: end
Validates the object and returns true if no errors are reported.
# File lib/sequel/model/base.rb, line 797 797: def valid? 798: errors.clear 799: if before_validation == false 800: save_failure(:validation) 801: return false 802: end 803: validate 804: after_validation 805: errors.empty? 806: end