Class: Veneer::Base::ClassWrapper
- Inherits:
- BasicObject
- Defined in:
- lib/veneer/base/class_wrapper.rb
Overview
Required methods new find_many destroy_all model_classes
Optional Methods find_first create! create count sum max min
Direct Known Subclasses
ActiveRecord::Base::VeneerInterface::ClassWrapper, DataMapper::Resource::VeneerInterface::ClassWrapper, MongoMapper::Document::VeneerInterface::ClassWrapper
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Class Method Summary collapse
Instance Method Summary collapse
-
#all(opts = {}) ⇒ Object
Provides an interface to query the objects.
-
#collection_associations ⇒ Object
Provides an array of associations of the format [ { :name => :association_name, :class_name => ‘TheClass’ } ].
-
#count(opts = {}) ⇒ Object
Obtains a count of all matching records Takes the same options as all.
-
#create(opts = {}) ⇒ Object
Create an instance of the object.
-
#create!(opts = {}) ⇒ Object
Create an instance of the object.
- #find_first(opts) ⇒ Object
-
#find_many(opts) ⇒ Object
Should return an array or array like structure with elements matching the provided conditions hash.
-
#first(opts = {}) ⇒ Object
Provides query access to the first item who meets the conditions in the passed in options hash.
-
#initialize(klass, opts) ⇒ ClassWrapper
constructor
A new instance of ClassWrapper.
-
#max(field, opts = {}) ⇒ Object
Obtains the maximum value of the given field of all matching records Takes the same options as all.
-
#member_associations ⇒ Object
Provides an array of association for belongs_to type associaions of the format:.
-
#min(field, opts = {}) ⇒ Object
Obtains the minimum value of the given field of all matching records Takes the same options as all.
-
#new(opts = {}) ⇒ Object
Instantiate an item The interface required is that a hash of attributes is passed The object should take each key, and set the value provided.
-
#primary_keys ⇒ Object
Should return an array of primary keys.
-
#sum(field, opts = {}) ⇒ Object
Obtains a sum of all matching records for the given field Takes the same options as all.
-
#validators_on(name) ⇒ Object
Should return an array of ActiveModel::Validator class instances for given property name.
Constructor Details
#initialize(klass, opts) ⇒ ClassWrapper
Returns a new instance of ClassWrapper.
21 22 23 |
# File 'lib/veneer/base/class_wrapper.rb', line 21 def initialize(klass, opts) @klass, @opts = klass, opts end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
20 21 22 |
# File 'lib/veneer/base/class_wrapper.rb', line 20 def klass @klass end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
20 21 22 |
# File 'lib/veneer/base/class_wrapper.rb', line 20 def opts @opts end |
Class Method Details
.inherited(klass) ⇒ Object
25 26 27 |
# File 'lib/veneer/base/class_wrapper.rb', line 25 def self.inherited(klass) subclasses << klass end |
.model_classes ⇒ Object
33 34 35 |
# File 'lib/veneer/base/class_wrapper.rb', line 33 def self.model_classes raise ::Veneer::Errors::NotImplemented end |
.subclasses ⇒ Object
29 30 31 |
# File 'lib/veneer/base/class_wrapper.rb', line 29 def self.subclasses @subclasses ||= [] end |
Instance Method Details
#all(opts = {}) ⇒ Object
Provides an interface to query the objects
Options that must be supported are
* :limit
* :offset
* :conditions
* :order
111 112 113 |
# File 'lib/veneer/base/class_wrapper.rb', line 111 def all(opts={}) find_many(::Hashie::Mash.new(opts)) end |
#collection_associations ⇒ Object
Provides an array of associations of the format [
{
:name => :association_name,
:class_name => 'TheClass'
}
]
The collection associations maps has_many, has n, embeds_many and the like.
47 48 49 |
# File 'lib/veneer/base/class_wrapper.rb', line 47 def collection_associations [] end |
#count(opts = {}) ⇒ Object
Obtains a count of all matching records Takes the same options as all
Adapter implementers should overwrite with a more efficient implementation
121 122 123 |
# File 'lib/veneer/base/class_wrapper.rb', line 121 def count(opts={}) all(opts).size end |
#create(opts = {}) ⇒ Object
Create an instance of the object. That is, instantiate and persist it in one step.
79 80 81 82 83 |
# File 'lib/veneer/base/class_wrapper.rb', line 79 def create(opts = {}) instance = new(opts) instance.save instance end |
#create!(opts = {}) ⇒ Object
Create an instance of the object. That is, instantiate and persist it in one step. Raise an error if the object is not persisted
69 70 71 72 73 |
# File 'lib/veneer/base/class_wrapper.rb', line 69 def create!(opts = {}) instance = new(opts) instance.save! instance end |
#find_first(opts) ⇒ Object
185 186 187 188 |
# File 'lib/veneer/base/class_wrapper.rb', line 185 def find_first(opts) opts[:limit] = 1 find_many(opts).first end |
#find_many(opts) ⇒ Object
Should return an array or array like structure with elements matching the provided conditions hash
169 170 171 |
# File 'lib/veneer/base/class_wrapper.rb', line 169 def find_many(opts) ::Kernel.raise Errors::NotImplemented end |
#first(opts = {}) ⇒ Object
Provides query access to the first item who meets the conditions in the passed in options hash
97 98 99 |
# File 'lib/veneer/base/class_wrapper.rb', line 97 def first(opts={}) ::Kernel.Veneer(find_first(::Hashie::Mash.new(opts))) end |
#max(field, opts = {}) ⇒ Object
Obtains the maximum value of the given field of all matching records Takes the same options as all
Adapter implementers should overwrite with a more efficient implementation
157 158 159 160 161 162 163 164 |
# File 'lib/veneer/base/class_wrapper.rb', line 157 def max(field, opts={}) opts = ::Hashie::Mash.new(opts) all(opts).inject(nil) do |max, item| val = item.send(field) max = val if !val.nil? && (max.nil? || val > max) max end end |
#member_associations ⇒ Object
Provides an array of association for belongs_to type associaions of the format:
[
{
:name => :assocaition_name,
:class_name => 'TheClass'
}
]
60 61 62 |
# File 'lib/veneer/base/class_wrapper.rb', line 60 def member_associations [] end |
#min(field, opts = {}) ⇒ Object
Obtains the minimum value of the given field of all matching records Takes the same options as all
Adapter implementers should overwrite with a more efficient implementation
142 143 144 145 146 147 148 149 |
# File 'lib/veneer/base/class_wrapper.rb', line 142 def min(field, opts={}) opts = ::Hashie::Mash.new(opts) all(opts).inject(nil) do |min, item| val = item.send(field) min = val if !val.nil? && (min.nil? || val < min) min end end |
#new(opts = {}) ⇒ Object
Instantiate an item The interface required is that a hash of attributes is passed The object should take each key, and set the value provided
89 90 91 |
# File 'lib/veneer/base/class_wrapper.rb', line 89 def new(opts = {}) klass.new(opts) end |
#primary_keys ⇒ Object
Should return an array of primary keys
175 176 177 |
# File 'lib/veneer/base/class_wrapper.rb', line 175 def primary_keys ::Kernel.raise Errors::NotImplemented end |
#sum(field, opts = {}) ⇒ Object
Obtains a sum of all matching records for the given field Takes the same options as all
Adapter implementers should overwrite with a more efficient implementation
131 132 133 134 |
# File 'lib/veneer/base/class_wrapper.rb', line 131 def sum(field, opts={}) opts = ::Hashie::Mash.new(opts) all(opts).inject(0){|sum, item| (item.send(field) || 0) + sum } end |
#validators_on(name) ⇒ Object
Should return an array of ActiveModel::Validator class instances for given property name
181 182 183 |
# File 'lib/veneer/base/class_wrapper.rb', line 181 def validators_on(name) ::Kernel.raise Errors::NotImplemented end |