| 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 500
500: def initialize(values = {}, from_db = false)
501: if from_db
502: @new = false
503: @values = values
504: else
505: @values = {}
506: @new = true
507: set(values)
508: changed_columns.clear
509: yield self if block_given?
510: end
511: after_initialize
512: end
Compares model instances by values.
# File lib/sequel/model/base.rb, line 538
538: def ==(obj)
539: (obj.class == model) && (obj.values == @values)
540: end
Returns value of the column‘s attribute.
# File lib/sequel/model/base.rb, line 515
515: def [](column)
516: @values[column]
517: 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 526
526: def []=(column, value)
527: # If it is new, it doesn't have a value yet, so we should
528: # definitely set the new value.
529: # If the column isn't in @values, we can't assume it is
530: # NULL in the database, so assume it has changed.
531: if new? || !@values.include?(column) || value != @values[column]
532: changed_columns << column unless changed_columns.include?(column)
533: @values[column] = typecast_value(column, value)
534: end
535: 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 558
558: def associations
559: @associations ||= {}
560: 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 580
580: def destroy
581: use_transactions ? db.transaction{_destroy} : _destroy
582: end
Returns a string representation of the model instance including the class name and values.
# File lib/sequel/model/base.rb, line 618
618: def inspect
619: "#<#{model.name} @values=#{inspect_values}>"
620: 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 635
635: def pk
636: raise(Error, "No primary key is associated with this model") unless key = primary_key
637: case key
638: when Array
639: key.collect{|k| @values[k]}
640: else
641: @values[key]
642: end
643: 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 655
655: def refresh
656: _refresh(this)
657: 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 677
677: def save(*columns)
678: opts = columns.last.is_a?(Hash) ? columns.pop : {}
679: return save_failure(:invalid) if opts[:validate] != false and !valid?
680: use_transaction = opts.include?(:transaction) ? opts[:transaction] : use_transactions
681: use_transaction ? db.transaction(opts){_save(columns, opts)} : _save(columns, opts)
682: end
Saves only changed columns or does nothing if no columns are marked as chanaged. If no columns have been changed, returns nil. If unable to save, returns false unless raise_on_save_failure is true.
# File lib/sequel/model/base.rb, line 687
687: def save_changes
688: save(:changed=>true) || false unless changed_columns.empty?
689: 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 695
695: def set(hash)
696: set_restricted(hash, nil, nil)
697: end
Runs set with the passed hash and runs save_changes (which runs any callback methods).
# File lib/sequel/model/base.rb, line 723
723: def update(hash)
724: update_restricted(hash, nil, nil)
725: end
Validates the object and returns true if no errors are reported.
# File lib/sequel/model/base.rb, line 752
752: def valid?
753: errors.clear
754: if before_validation == false
755: save_failure(:validation)
756: return false
757: end
758: validate
759: after_validation
760: errors.empty?
761: end