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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#klassObject (readonly)

Returns the value of attribute klass.



20
21
22
# File 'lib/veneer/base/class_wrapper.rb', line 20

def klass
  @klass
end

#optsObject (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_classesObject



33
34
35
# File 'lib/veneer/base/class_wrapper.rb', line 33

def self.model_classes
  raise ::Veneer::Errors::NotImplemented
end

.subclassesObject



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_associationsObject

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

See Also:



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.

See Also:



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

See Also:



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

See Also:

  • all


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

See Also:

  • all


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

See Also:



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_associationsObject

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

See Also:



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_keysObject

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

See Also:



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