Class: Findable::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Associations, Inspection, Schema
Defined in:
lib/findable/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inspection

#inspect

Methods included from Schema

#attribute=, #attribute?

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

.allObject



26
27
28
# File 'lib/findable/base.rb', line 26

def all
  collection!(query.all)
end

.arel_tableObject

Raises:



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

Returns:

  • (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_idsObject

Query APIs



107
# File 'lib/findable/base.rb', line 107

delegate :find_by_ids, :find_by_index, :insert, to: :query

.firstObject 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_keyObject



22
23
24
# File 'lib/findable/base.rb', line 22

def primary_key
  "id"
end

.queryObject



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

#attributesObject



173
174
175
# File 'lib/findable/base.rb', line 173

def attributes
  @_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
end

#deleteObject Also known as: destroy



168
169
170
# File 'lib/findable/base.rb', line 168

def delete
  self.class.delete(self)
end

#hashObject



151
152
153
# File 'lib/findable/base.rb', line 151

def hash
  id.hash
end

#idObject



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

Returns:

  • (Boolean)


155
156
157
# File 'lib/findable/base.rb', line 155

def new_record?
  id ? !self.class.exists?(self) : true
end

#persisted?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/findable/base.rb', line 159

def persisted?
  !new_record?
end

#saveObject Also known as: save!



163
164
165
# File 'lib/findable/base.rb', line 163

def save
  self.class.insert(self)
end