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 555 555: def initialize(values = {}, from_db = false) 556: if from_db 557: @new = false 558: set_values(values) 559: else 560: @values = {} 561: @new = true 562: @modified = true 563: set(values) 564: changed_columns.clear 565: yield self if block_given? 566: end 567: after_initialize 568: end
Returns value of the column‘s attribute.
# File lib/sequel/model/base.rb, line 571 571: def [](column) 572: @values[column] 573: 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 579 579: def []=(column, value) 580: # If it is new, it doesn't have a value yet, so we should 581: # definitely set the new value. 582: # If the column isn't in @values, we can't assume it is 583: # NULL in the database, so assume it has changed. 584: v = typecast_value(column, value) 585: if new? || !@values.include?(column) || v != @values[column] 586: changed_columns << column unless changed_columns.include?(column) 587: @values[column] = v 588: end 589: 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 or if the :transaction option is given and true.
# File lib/sequel/model/base.rb, line 634 634: def destroy(opts = {}) 635: checked_save_failure{checked_transaction(opts){_destroy(opts)}} 636: end
Compares model instances by values.
# File lib/sequel/model/base.rb, line 647 647: def eql?(obj) 648: (obj.class == model) && (obj.values == @values) 649: end
Returns a string representation of the model instance including the class name and values.
# File lib/sequel/model/base.rb, line 677 677: def inspect 678: "#<#{model.name} @values=#{inspect_values}>" 679: end
Remove elements of the model object that make marshalling fail. Returns self.
# File lib/sequel/model/base.rb, line 692 692: def marshallable! 693: @this = nil 694: self 695: 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 699 699: def modified! 700: @modified = true 701: 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 706 706: def modified? 707: @modified || !changed_columns.empty? 708: 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 718 718: def pk 719: raise(Error, "No primary key is associated with this model") unless key = primary_key 720: key.is_a?(Array) ? key.map{|k| @values[k]} : @values[key] 721: 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 733 733: def refresh 734: _refresh(this) 735: 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 755 755: def save(*columns) 756: opts = columns.last.is_a?(Hash) ? columns.pop : {} 757: if opts[:validate] != false and !valid? 758: raise(ValidationFailed.new(errors)) if raise_on_save_failure 759: return 760: end 761: checked_save_failure{checked_transaction(opts){_save(columns, opts)}} 762: 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 767 767: def save_changes(opts={}) 768: save(opts.merge(:changed=>true)) || false if modified? 769: 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 775 775: def set(hash) 776: set_restricted(hash, nil, nil) 777: end
Runs set with the passed hash and then runs save_changes.
# File lib/sequel/model/base.rb, line 810 810: def update(hash) 811: update_restricted(hash, nil, nil) 812: end
Update the instances values by calling set_fields with the hash and fields, then save any changes to the record. Returns self.
# File lib/sequel/model/base.rb, line 828 828: def update_fields(hash, fields) 829: set_fields(hash, fields) 830: save_changes 831: end
Validates the object and returns true if no errors are reported.
# File lib/sequel/model/base.rb, line 847 847: def valid? 848: errors.clear 849: if before_validation == false 850: save_failure(:validation) if raise_on_save_failure 851: return false 852: end 853: validate 854: after_validation 855: errors.empty? 856: end
Validates the object. If the object is invalid, errors should be added to the errors attribute. By default, does nothing, as all models are valid by default. See the "Model Validations" guide. for details about validation.
# File lib/sequel/model/base.rb, line 843 843: def validate 844: end