Class: Findable::Base
- Inherits:
-
Object
- Object
- Findable::Base
- Includes:
- ActiveModel::Model, Associations, Inspection, Schema
- Defined in:
- lib/findable/base.rb
Class Method Summary collapse
- .all ⇒ Object
- .arel_table ⇒ Object
- .create(attrs = {}) ⇒ Object (also: create!)
- .exists?(obj) ⇒ Boolean
- .find(ids) ⇒ Object
- .find_by(conditions) ⇒ Object
- .find_by!(conditions) ⇒ Object
-
.find_by_ids ⇒ Object
Query APIs.
-
.first ⇒ Object
(also: take)
ActiveRecord like APIs.
-
.ordered_find(*_ids) ⇒ Object
Extension.
- .primary_key ⇒ Object
- .query ⇒ Object
- .where(conditions) ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
- #delete ⇒ Object (also: #destroy)
- #hash ⇒ Object
- #id ⇒ Object
- #id=(value) ⇒ Object
-
#initialize(params = {}) ⇒ Base
constructor
A new instance of Base.
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #save ⇒ Object (also: #save!)
Methods included from Inspection
Methods included from Schema
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
137 138 139 140 141 |
# File 'lib/findable/base.rb', line 137 def initialize(params = {}) params = params.with_indifferent_access params.keys.each {|attr| self.class.define_field(attr) } @_attributes = params end |
Class Method Details
.all ⇒ Object
26 27 28 |
# File 'lib/findable/base.rb', line 26 def all collection!(query.all) end |
.arel_table ⇒ Object
13 14 15 |
# File 'lib/findable/base.rb', line 13 def arel_table raise NotActiveRecord.new(self) end |
.create(attrs = {}) ⇒ Object Also known as: create!
91 92 93 94 95 |
# File 'lib/findable/base.rb', line 91 def create(attrs = {}) record = new(attrs) record.save record end |
.exists?(obj) ⇒ Boolean
111 112 113 114 115 116 117 |
# File 'lib/findable/base.rb', line 111 def exists?(obj) if _id = id_from(obj) query.exists?(_id) else false end end |
.find(ids) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/findable/base.rb', line 30 def find(ids) if records = find_by_ids(ids).presence ids.is_a?(Array) ? collection!(records) : records.first else raise not_found(id: ids) end end |
.find_by(conditions) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/findable/base.rb', line 38 def find_by(conditions) if conditions.is_a?(Hash) conditions.symbolize_keys! if index = conditions.keys.detect {|key| key.in?(indexes) } value = conditions.delete(index) if index == :id records = find_by_ids(value) else records = find_by_index(index, value) end case when records.empty? then nil when conditions.empty? then records.first else records.detect {|record| conditions.all? {|k, v| record.public_send(k) == v } } end else all.find_by(conditions.dup) end else find_by_ids(conditions).first end end |
.find_by!(conditions) ⇒ Object
65 66 67 |
# File 'lib/findable/base.rb', line 65 def find_by!(conditions) find_by(conditions.dup) || (raise not_found(conditions)) end |
.find_by_ids ⇒ Object
Query APIs
107 |
# File 'lib/findable/base.rb', line 107 delegate :find_by_ids, :find_by_index, :insert, to: :query |
.first ⇒ Object Also known as: take
ActiveRecord like APIs
19 |
# File 'lib/findable/base.rb', line 19 delegate :first, :last, :order, :pluck, to: :all |
.ordered_find(*_ids) ⇒ Object
Extension
100 101 102 103 |
# File 'lib/findable/base.rb', line 100 def ordered_find(*_ids) _ids.flatten! find(_ids).ordered_find(_ids) end |
.primary_key ⇒ Object
22 23 24 |
# File 'lib/findable/base.rb', line 22 def primary_key "id" end |
.query ⇒ Object
119 120 121 |
# File 'lib/findable/base.rb', line 119 def query @_query ||= Query.new(self) end |
.where(conditions) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/findable/base.rb', line 69 def where(conditions) conditions.symbolize_keys! if index = conditions.keys.detect {|key| key.in?(indexes) } value = conditions.delete(index) if index == :id records = find_by_ids(value) else records = find_by_index(index, value) end if conditions.empty? collection!(records) else collection!(records.select {|record| conditions.all? {|k, v| record.public_send(k) == v } }) end else all.where(conditions.dup) end end |
Instance Method Details
#attributes ⇒ Object
173 174 175 |
# File 'lib/findable/base.rb', line 173 def attributes @_attributes ||= ActiveSupport::HashWithIndifferentAccess.new end |
#delete ⇒ Object Also known as: destroy
168 169 170 |
# File 'lib/findable/base.rb', line 168 def delete self.class.delete(self) end |
#hash ⇒ Object
151 152 153 |
# File 'lib/findable/base.rb', line 151 def hash id.hash end |
#id ⇒ Object
143 144 145 |
# File 'lib/findable/base.rb', line 143 def id attributes[:id].presence end |
#id=(value) ⇒ Object
147 148 149 |
# File 'lib/findable/base.rb', line 147 def id=(value) attributes[:id] = value end |
#new_record? ⇒ Boolean
155 156 157 |
# File 'lib/findable/base.rb', line 155 def new_record? id ? !self.class.exists?(self) : true end |
#persisted? ⇒ Boolean
159 160 161 |
# File 'lib/findable/base.rb', line 159 def persisted? !new_record? end |
#save ⇒ Object Also known as: save!
163 164 165 |
# File 'lib/findable/base.rb', line 163 def save self.class.insert(self) end |