Module: PassiveRecord::ClassMethods

Extended by:
Forwardable
Includes:
Enumerable, Associations, Core, Hooks
Defined in:
lib/passive_record/class_methods.rb

Instance Method Summary collapse

Methods included from Hooks

#after_create, #after_create_hooks, #after_update, #after_update_hooks, #before_create, #before_create_hooks

Methods included from Associations

#associate!, #associations_id_syms, #belongs_to, #has_and_belongs_to_many, #has_many, #has_one

Instance Method Details

#allObject



15
16
17
# File 'lib/passive_record/class_methods.rb', line 15

def all
  instances_by_id.values
end

#create(attrs = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/passive_record/class_methods.rb', line 52

def create(attrs={})
  instance = new

  instance.singleton_class.class_eval { attr_accessor :id }
  instance.send(:"id=", id_factory.generate(self))

  register(instance)

  before_create_hooks.each do |hook|
    hook.run(instance)
  end

  attrs.each do |(k,v)|
    instance.send("#{k}=", v)
  end

  after_create_hooks.each do |hook|
    hook.run(instance)
  end

  instance
end

#descendantsObject



11
12
13
# File 'lib/passive_record/class_methods.rb', line 11

def descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

#destroy_allObject



75
76
77
# File 'lib/passive_record/class_methods.rb', line 75

def destroy_all
  @instances = {}
end

#find(id_or_ids) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/passive_record/class_methods.rb', line 24

def find(id_or_ids)
  if id_or_ids.is_a?(Array)
    find_by_ids(id_or_ids)
  else
    find_by_id(id_or_ids)
  end
end

#find_all_by(conditions) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/passive_record/class_methods.rb', line 40

def find_all_by(conditions)
  if conditions.is_a?(Array) && conditions.all? { |c| c.is_a?(Identifier) }
    find_by_ids(conditions)
  else
    where(conditions).all
  end
end

#find_by(conditions) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/passive_record/class_methods.rb', line 32

def find_by(conditions)
  if conditions.is_a?(Hash)
    where(conditions).first
  else # assume we have an identifier/identifiers
    find(conditions)
  end
end

#find_by_id(id_to_find) ⇒ Object (protected)



80
81
82
# File 'lib/passive_record/class_methods.rb', line 80

def find_by_id(id_to_find)
  find_by(id: id_to_find)
end

#find_by_ids(ids) ⇒ Object (protected)



84
85
86
# File 'lib/passive_record/class_methods.rb', line 84

def find_by_ids(ids)
  instances_by_id.select { |id,_| ids.include?(id) }.values
end

#lastObject



20
21
22
# File 'lib/passive_record/class_methods.rb', line 20

def last
  all.last
end

#where(conditions) ⇒ Object



48
49
50
# File 'lib/passive_record/class_methods.rb', line 48

def where(conditions)
  Query.new(self, conditions)
end