Class: Findable::Base
- Inherits:
-
Object
- Object
- Findable::Base
- Includes:
- ActiveModel::AttributeMethods, 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!)
- .delete(obj) ⇒ Object
- .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.
- .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.
125 126 127 128 129 |
# File 'lib/findable/base.rb', line 125 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
27 28 29 |
# File 'lib/findable/base.rb', line 27 def all collection!(query.all) end |
.arel_table ⇒ Object
14 15 16 |
# File 'lib/findable/base.rb', line 14 def arel_table raise NotActiveRecord.new(self) end |
.create(attrs = {}) ⇒ Object Also known as: create!
80 81 82 83 84 |
# File 'lib/findable/base.rb', line 80 def create(attrs = {}) record = new(attrs) record.save record end |
.delete(obj) ⇒ Object
101 102 103 104 105 |
# File 'lib/findable/base.rb', line 101 def delete(obj) if _id = id_from(obj) query.delete(_id) end end |
.exists?(obj) ⇒ Boolean
93 94 95 96 97 98 99 |
# File 'lib/findable/base.rb', line 93 def exists?(obj) if _id = id_from(obj) query.exists?(_id) else false end end |
.find(ids) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/findable/base.rb', line 31 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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/findable/base.rb', line 39 def find_by(conditions) if conditions.is_a?(Hash) conditions.symbolize_keys! if id = conditions.delete(:id) records = find_by_ids(id) 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
60 61 62 |
# File 'lib/findable/base.rb', line 60 def find_by!(conditions) find_by(conditions.dup) || (raise not_found(conditions)) end |
.find_by_ids ⇒ Object
Query APIs
89 |
# File 'lib/findable/base.rb', line 89 delegate :find_by_ids, :insert, to: :query |
.first ⇒ Object Also known as: take
ActiveRecord like APIs
20 |
# File 'lib/findable/base.rb', line 20 delegate :first, :last, :order, :pluck, to: :all |
.primary_key ⇒ Object
23 24 25 |
# File 'lib/findable/base.rb', line 23 def primary_key "id" end |
.query ⇒ Object
107 108 109 |
# File 'lib/findable/base.rb', line 107 def query @_query ||= Query.new(self) end |
.where(conditions) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/findable/base.rb', line 64 def where(conditions) conditions.symbolize_keys! if id = conditions.delete(:id) records = find_by_ids(id) 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
161 162 163 |
# File 'lib/findable/base.rb', line 161 def attributes @_attributes ||= ActiveSupport::HashWithIndifferentAccess.new end |
#delete ⇒ Object Also known as: destroy
156 157 158 |
# File 'lib/findable/base.rb', line 156 def delete self.class.delete(self) end |
#hash ⇒ Object
139 140 141 |
# File 'lib/findable/base.rb', line 139 def hash id.hash end |
#id ⇒ Object
131 132 133 |
# File 'lib/findable/base.rb', line 131 def id attributes[:id].presence end |
#id=(value) ⇒ Object
135 136 137 |
# File 'lib/findable/base.rb', line 135 def id=(value) attributes[:id] = value end |
#new_record? ⇒ Boolean
143 144 145 |
# File 'lib/findable/base.rb', line 143 def new_record? id ? !self.class.exists?(self) : true end |
#persisted? ⇒ Boolean
147 148 149 |
# File 'lib/findable/base.rb', line 147 def persisted? !new_record? end |
#save ⇒ Object Also known as: save!
151 152 153 |
# File 'lib/findable/base.rb', line 151 def save self.class.insert(self) end |