Class: Findable::Base
- Inherits:
-
Object
- Object
- Findable::Base
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Model, Associations, Inspection
- Defined in:
- lib/findable/base.rb
Class Method Summary collapse
-
.all ⇒ Object
ActiveRecord like APIs.
- .column_names ⇒ Object
- .create(attrs = {}) ⇒ Object (also: create!)
-
.define_field(attr) ⇒ Object
field definitions.
- .delete(obj) ⇒ Object
- .exists?(obj) ⇒ Boolean
- .find(ids) ⇒ Object
- .find_by(conditions) ⇒ Object
- .find_by!(conditions) ⇒ Object
-
.find_by_ids ⇒ Object
Query APIs.
- .primary_key ⇒ Object
- .query ⇒ Object
- .where(conditions) ⇒ Object
Instance Method Summary collapse
- #attribute=(attr, value) ⇒ Object
- #attribute?(attr) ⇒ Boolean
- #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
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
133 134 135 136 137 |
# File 'lib/findable/base.rb', line 133 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
ActiveRecord like APIs
27 |
# File 'lib/findable/base.rb', line 27 delegate :all, to: :query |
.column_names ⇒ Object
33 34 35 |
# File 'lib/findable/base.rb', line 33 def column_names @_column_names ||= [:id] end |
.create(attrs = {}) ⇒ Object Also known as: create!
90 91 92 93 94 |
# File 'lib/findable/base.rb', line 90 def create(attrs = {}) record = new(attrs) record.save record end |
.define_field(attr) ⇒ Object
field definitions
17 18 19 20 21 22 23 |
# File 'lib/findable/base.rb', line 17 def define_field(attr) unless public_method_defined?(attr) define_attribute_methods attr define_method(attr) { attributes[attr] } column_names << attr.to_sym end end |
.delete(obj) ⇒ Object
117 118 119 120 121 |
# File 'lib/findable/base.rb', line 117 def delete(obj) if _id = id_from(obj) query.delete(_id) end end |
.exists?(obj) ⇒ Boolean
109 110 111 112 113 114 115 |
# File 'lib/findable/base.rb', line 109 def exists?(obj) if _id = id_from(obj) query.exists?(_id) else false end end |
.find(ids) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/findable/base.rb', line 37 def find(ids) if records = find_by_ids(ids).presence ids.is_a?(Array) ? records : records.first else raise RecordNotFound.new(self, id: ids) end end |
.find_by(conditions) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/findable/base.rb', line 45 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.detect {|record| conditions.all? {|k, v| record.public_send(k) == v } } end else find_by_ids(conditions).first end end |
.find_by!(conditions) ⇒ Object
68 69 70 |
# File 'lib/findable/base.rb', line 68 def find_by!(conditions) find_by(conditions.dup) || (raise RecordNotFound.new(self, conditions)) end |
.find_by_ids ⇒ Object
Query APIs
105 |
# File 'lib/findable/base.rb', line 105 delegate :find_by_ids, :insert, to: :query |
.primary_key ⇒ Object
29 30 31 |
# File 'lib/findable/base.rb', line 29 def primary_key "id" end |
.query ⇒ Object
123 124 125 |
# File 'lib/findable/base.rb', line 123 def query @_query ||= Query.new(self) end |
.where(conditions) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/findable/base.rb', line 72 def where(conditions) conditions.symbolize_keys! if id = conditions.delete(:id) records = find_by_ids(id) if conditions.empty? records else records.select {|record| conditions.all? {|k, v| record.public_send(k) == v } } end else all.select {|record| conditions.all? {|k, v| record.public_send(k) == v } } end end |
Instance Method Details
#attribute=(attr, value) ⇒ Object
173 174 175 |
# File 'lib/findable/base.rb', line 173 def attribute=(attr, value) attributes[attr.to_sym] = value end |
#attribute?(attr) ⇒ Boolean
177 178 179 |
# File 'lib/findable/base.rb', line 177 def attribute?(attr) attributes[attr.to_sym].present? end |
#attributes ⇒ Object
169 170 171 |
# File 'lib/findable/base.rb', line 169 def attributes @_attributes ||= ActiveSupport::HashWithIndifferentAccess.new end |
#delete ⇒ Object Also known as: destroy
164 165 166 |
# File 'lib/findable/base.rb', line 164 def delete self.class.delete(self) end |
#hash ⇒ Object
147 148 149 |
# File 'lib/findable/base.rb', line 147 def hash id.hash end |
#id ⇒ Object
139 140 141 |
# File 'lib/findable/base.rb', line 139 def id attributes[:id].presence end |
#id=(value) ⇒ Object
143 144 145 |
# File 'lib/findable/base.rb', line 143 def id=(value) attributes[:id] = value end |
#new_record? ⇒ Boolean
151 152 153 |
# File 'lib/findable/base.rb', line 151 def new_record? id ? !self.class.exists?(self) : true end |
#persisted? ⇒ Boolean
155 156 157 |
# File 'lib/findable/base.rb', line 155 def persisted? !new_record? end |
#save ⇒ Object Also known as: save!
159 160 161 |
# File 'lib/findable/base.rb', line 159 def save self.class.insert(self) end |