Module: Sequel::Model::DatasetMethods
- Defined in:
- lib/sequel/model/base.rb
Overview
Dataset methods are methods that the model class extends its dataset with in the call to set_dataset.
Instance Attribute Summary collapse
-
#model ⇒ Object
The model class associated with this dataset.
Instance Method Summary collapse
-
#[](*args) ⇒ Object
Assume if a single integer is given that it is a lookup by primary key, and call with_pk with the argument.
-
#destroy ⇒ Object
Destroy each row in the dataset by instantiating it and then calling destroy on the resulting model object.
-
#to_hash(key_column = nil, value_column = nil) ⇒ Object
This allows you to call
to_hashwithout any arguments, which will result in a hash with the primary key value being the key and the model object being the value. -
#with_pk(pk) ⇒ Object
Given a primary key value, return the first record in the dataset with that primary key value.
Instance Attribute Details
#model ⇒ Object
The model class associated with this dataset
Artist.dataset.model # => Artist
1632 1633 1634 |
# File 'lib/sequel/model/base.rb', line 1632 def model @model end |
Instance Method Details
#[](*args) ⇒ Object
Assume if a single integer is given that it is a lookup by primary key, and call with_pk with the argument.
Artist.dataset[1] # SELECT * FROM artists WHERE (id = 1) LIMIT 1
1638 1639 1640 1641 1642 1643 1644 |
# File 'lib/sequel/model/base.rb', line 1638 def [](*args) if args.length == 1 && (i = args.at(0)) && i.is_a?(Integer) with_pk(i) else super end end |
#destroy ⇒ Object
Destroy each row in the dataset by instantiating it and then calling destroy on the resulting model object. This isn’t as fast as deleting the dataset, which does a single SQL call, but this runs any destroy hooks on each object in the dataset.
Artist.dataset.destroy
# DELETE FROM artists WHERE (id = 1)
# DELETE FROM artists WHERE (id = 2)
# ...
1655 1656 1657 1658 |
# File 'lib/sequel/model/base.rb', line 1655 def destroy pr = proc{all{|r| r.destroy}.length} model.use_transactions ? @db.transaction(&pr) : pr.call end |
#to_hash(key_column = nil, value_column = nil) ⇒ Object
This allows you to call to_hash without any arguments, which will result in a hash with the primary key value being the key and the model object being the value.
Artist.dataset.to_hash # SELECT * FROM artists
# => {1=>#<Artist {:id=>1, ...}>,
# 2=>#<Artist {:id=>2, ...}>,
# ...}
1668 1669 1670 1671 1672 1673 1674 1675 |
# File 'lib/sequel/model/base.rb', line 1668 def to_hash(key_column=nil, value_column=nil) if key_column super else raise(Sequel::Error, "No primary key for model") unless model and pk = model.primary_key super(pk, value_column) end end |
#with_pk(pk) ⇒ Object
Given a primary key value, return the first record in the dataset with that primary key value.
# Single primary key
Artist.dataset.with_pk(1) # SELECT * FROM artists WHERE (id = 1) LIMIT 1
# Composite primary key
Artist.dataset.with_pk([1, 2]) # SELECT * FROM artists
# WHERE ((id1 = 1) AND (id2 = 2)) LIMIT 1
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 |
# File 'lib/sequel/model/base.rb', line 1686 def with_pk(pk) case primary_key = model.primary_key when Array raise(Error, "single primary key given (#{pk.inspect}) when a composite primary key is expected (#{primary_key.inspect})") unless pk.is_a?(Array) raise(Error, "composite primary key given (#{pk.inspect}) does not match composite primary key length (#{primary_key.inspect})") if pk.length != primary_key.length first(primary_key.zip(pk)) else raise(Error, "composite primary key given (#{pk.inspect}) when a single primary key is expected (#{primary_key.inspect})") if pk.is_a?(Array) first(primary_key=>pk) end end |