Class: Sequel::Model::OrmAdapter
- Inherits:
-
OrmAdapter::Base
- Object
- OrmAdapter::Base
- Sequel::Model::OrmAdapter
- Defined in:
- lib/orm_adapter-sequel/sequel.rb
Class Method Summary collapse
-
.model_classes ⇒ Object
Gets a list of the available models for this adapter.
Instance Method Summary collapse
-
#column_names ⇒ Object
Get a list of column/property/field names.
-
#create!(attributes) ⇒ Object
Create a model using attributes.
-
#find_all(options) ⇒ Object
Find all models, optionally matching conditions, and specifying order Sequel: #.order doesn’t like an array hence the *.
-
#find_first(options) ⇒ Object
Find the first instance, optionally matching conditions, and specifying order.
-
#get(id) ⇒ Object
Get an instance by id of the model.
-
#get!(id) ⇒ Object
Get an instance by id of the model.
-
#initialize(klass) ⇒ OrmAdapter
constructor
A new instance of OrmAdapter.
Constructor Details
#initialize(klass) ⇒ OrmAdapter
Returns a new instance of OrmAdapter.
15 16 17 |
# File 'lib/orm_adapter-sequel/sequel.rb', line 15 def initialize(klass) @klass = klass end |
Class Method Details
.model_classes ⇒ Object
Gets a list of the available models for this adapter
11 12 13 |
# File 'lib/orm_adapter-sequel/sequel.rb', line 11 def self.model_classes Sequel::Model.descendents end |
Instance Method Details
#column_names ⇒ Object
Get a list of column/property/field names
20 21 22 |
# File 'lib/orm_adapter-sequel/sequel.rb', line 20 def column_names klass.columns end |
#create!(attributes) ⇒ Object
Create a model using attributes
Sequel: no support for mass creation of associated objects so we fake it.
* use a Sequel transaction
* passed in key (col) names are checked against association names
* an array of associated objects is created with the assoc method
* the main object is created followed by all associated records
* save is called to throw an error if found
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/orm_adapter-sequel/sequel.rb', line 84 def create!(attributes) associated_objects = [] attrs = {} klass.db.transaction do attributes.each do |col, value| if klass.associations.include?(col) Array(value).each{|v| associated_objects << [association_method(col), v]} else attrs.merge!(col => value) # pass it on end end obj = klass.create(attrs) # create main obj associated_objects.each do |m,o| obj.send(m, o) end obj.save end # transaction end |
#find_all(options) ⇒ Object
Find all models, optionally matching conditions, and specifying order Sequel: #.order doesn’t like an array hence the *
70 71 72 73 |
# File 'lib/orm_adapter-sequel/sequel.rb', line 70 def find_all() conditions, order = extract_conditions!() klass.filter(conditions_to_hash(conditions)).order(*order_clause(order)).all end |
#find_first(options) ⇒ Object
Find the first instance, optionally matching conditions, and specifying order
You can call with just conditions, providing a hash
User.to_adapter.find_first :name => "Fred", :age => 23
Or you can specify :order, and :conditions as keys
User.to_adapter.find_first :conditions => {:name => "Fred", :age => 23}
User.to_adapter.find_first :order => [:age, :desc]
User.to_adapter.find_first :order => :name, :conditions => {:age => 18}
When specifying :order, it may be
-
a single arg e.g.
:order => :name
-
a single pair with :asc, or :desc as last, e.g.
:order => [:name, :desc]
-
an array of single args or pairs (with :asc or :desc as last), e.g.
:order => [[:name, :asc], [:age, :desc]]
Sequel: #.order doesn’t like an array hence the *
61 62 63 64 |
# File 'lib/orm_adapter-sequel/sequel.rb', line 61 def find_first() conditions, order = extract_conditions!() klass.filter(conditions_to_hash(conditions)).order(*order_clause(order)).first end |
#get(id) ⇒ Object
Get an instance by id of the model. Returns nil if a model is not found. This should comply with ActiveModel#to_key API, i.e.:
User.to_adapter.get(@user.to_key) == @user
39 40 41 |
# File 'lib/orm_adapter-sequel/sequel.rb', line 39 def get(id) klass.find(wrap_key(klass.primary_key => id)) end |
#get!(id) ⇒ Object
Get an instance by id of the model. Raises an error if a model is not found. This should comply with ActiveModel#to_key API, i.e.:
User.to_adapter.get!(@user.to_key) == @user
Sequel: no built in finder/filter that raises an error so one is added here
30 31 32 |
# File 'lib/orm_adapter-sequel/sequel.rb', line 30 def get!(id) klass[wrap_key(id)] || raise(Error, "#{klass.name} not found with #{klass.primary_key} of #{wrap_key(id)}") end |