Class: Findable::Base
- Inherits:
-
Object
- Object
- Findable::Base
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Model, Associations
- Defined in:
- lib/findable/base.rb
Class Method Summary collapse
- .all ⇒ Object
- .column_names ⇒ Object
- .create(attrs = {}) ⇒ Object (also: create!)
-
.define_field(attr, options = {}) ⇒ 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.
- .insert(obj) ⇒ Object
-
.primary_key ⇒ Object
ActiveRecord like APIs.
- .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!)
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
143 144 145 146 147 148 |
# File 'lib/findable/base.rb', line 143 def initialize(params = {}) params = Oj.load(params) if params.is_a?(String) params.symbolize_keys! params.keys.each {|attr| self.class.define_field(attr) } @_attributes = params end |
Class Method Details
.all ⇒ Object
35 36 37 |
# File 'lib/findable/base.rb', line 35 def all data.map {|val| new(val) } end |
.column_names ⇒ Object
31 32 33 |
# File 'lib/findable/base.rb', line 31 def column_names @_column_names ||= [:id] end |
.create(attrs = {}) ⇒ Object Also known as: create!
95 96 97 98 99 |
# File 'lib/findable/base.rb', line 95 def create(attrs = {}) record = new(attrs) record.save record end |
.define_field(attr, options = {}) ⇒ Object
field definitions
16 17 18 19 20 21 22 23 |
# File 'lib/findable/base.rb', line 16 def define_field(attr, = {}) unless public_method_defined?(attr) .symbolize_keys! define_attribute_methods attr define_method(attr) { attributes[attr.to_sym] } column_names << attr.to_sym end end |
.delete(obj) ⇒ Object
127 128 129 130 131 |
# File 'lib/findable/base.rb', line 127 def delete(obj) if _id = id_from(obj) query.delete(_id) end end |
.exists?(obj) ⇒ Boolean
115 116 117 118 119 120 121 |
# File 'lib/findable/base.rb', line 115 def exists?(obj) if _id = id_from(obj) query.exists?(_id) else false end end |
.find(ids) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/findable/base.rb', line 39 def find(ids) if values = find_by_ids(ids).compact.presence ids.is_a?(Array) ? values.map {|val| new(val) } : new(values.first) else raise RecordNotFound.new(self, id: ids) end end |
.find_by(conditions) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/findable/base.rb', line 47 def find_by(conditions) if conditions.is_a?(Hash) conditions.symbolize_keys! if id = conditions.delete(:id) values = find_by_ids(id).compact case when values.empty? then nil when conditions.empty? then new(values.first) else value = values.detect {|val| record = new(val) conditions.all? {|k, v| record.public_send(k) == v } } value ? new(value) : nil end else all.detect {|r| conditions.all? {|k, v| r.public_send(k) == v } } end else values = find_by_ids(conditions).compact values.empty? ? nil : new(values.first) end end |
.find_by!(conditions) ⇒ Object
73 74 75 |
# File 'lib/findable/base.rb', line 73 def find_by!(conditions) find_by(conditions.dup) || (raise RecordNotFound.new(self, conditions)) end |
.find_by_ids ⇒ Object
Query APIs
111 |
# File 'lib/findable/base.rb', line 111 delegate :find_by_ids, :data, to: :query |
.insert(obj) ⇒ Object
123 124 125 |
# File 'lib/findable/base.rb', line 123 def insert(obj) query.insert(obj.attributes) end |
.primary_key ⇒ Object
ActiveRecord like APIs
27 28 29 |
# File 'lib/findable/base.rb', line 27 def primary_key "id" end |
.query ⇒ Object
133 134 135 |
# File 'lib/findable/base.rb', line 133 def query @_query ||= Query.new(self) end |
.where(conditions) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/findable/base.rb', line 77 def where(conditions) if id = conditions.delete(:id) values = find_by_ids(id).compact if conditions.empty? values.map {|val| new(val) } else values.map {|val| record = new(val) conditions.all? {|k, v| record.public_send(k) == v } ? record : nil }.compact end else all.select {|r| conditions.all? {|k, v| r.public_send(k) == v } } end end |
Instance Method Details
#attributes ⇒ Object
181 182 183 |
# File 'lib/findable/base.rb', line 181 def attributes @_attributes ||= {} end |
#delete ⇒ Object Also known as: destroy
176 177 178 |
# File 'lib/findable/base.rb', line 176 def delete self.class.delete(self) end |
#hash ⇒ Object
158 159 160 |
# File 'lib/findable/base.rb', line 158 def hash id.hash end |
#id ⇒ Object
150 151 152 |
# File 'lib/findable/base.rb', line 150 def id attributes[:id].presence end |
#id=(value) ⇒ Object
154 155 156 |
# File 'lib/findable/base.rb', line 154 def id=(value) attributes[:id] = value end |
#new_record? ⇒ Boolean
162 163 164 |
# File 'lib/findable/base.rb', line 162 def new_record? id ? !self.class.exists?(self) : true end |
#persisted? ⇒ Boolean
166 167 168 |
# File 'lib/findable/base.rb', line 166 def persisted? !new_record? end |
#save ⇒ Object Also known as: save!
170 171 172 173 |
# File 'lib/findable/base.rb', line 170 def save @_attributes = self.class.insert(self) self end |