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_destroy, #after_destroy_hooks, #after_update, #after_update_hooks, #before_create, #before_create_hooks, #before_destroy, #before_destroy_hooks, #before_update, #before_update_hooks, #find_hooks_of_type, #inject_hook
#associate!, #associations_id_syms, #belongs_to, #has_and_belongs_to_many, #has_many, #has_one
Instance Method Details
#all ⇒ Object
15
16
17
|
# File 'lib/passive_record/class_methods.rb', line 15
def all
instances_by_id.values
end
|
#create(attrs = {}) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/passive_record/class_methods.rb', line 44
def create(attrs={})
instance = new
instance.singleton_class.class_eval { attr_accessor :id }
instance_id = attrs.delete(:id) { SecureRandom.uuid }
instance.send(:id=, instance_id)
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
|
#descendants ⇒ Object
11
12
13
|
# File 'lib/passive_record/class_methods.rb', line 11
def descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
|
#destroy(id) ⇒ Object
69
70
71
|
# File 'lib/passive_record/class_methods.rb', line 69
def destroy(id)
@instances.reject! {|k,_| id == k }
end
|
#destroy_all ⇒ Object
73
74
75
|
# File 'lib/passive_record/class_methods.rb', line 73
def destroy_all
@instances = {}
end
|
#find(id_or_ids) ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/passive_record/class_methods.rb', line 20
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
36
37
38
|
# File 'lib/passive_record/class_methods.rb', line 36
def find_all_by(conditions)
where(conditions).all
end
|
#find_by(conditions) ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/passive_record/class_methods.rb', line 28
def find_by(conditions)
if conditions.is_a?(Hash)
where(conditions).first
else find(conditions)
end
end
|
#find_by_id(id_to_find) ⇒ Object
78
79
80
|
# File 'lib/passive_record/class_methods.rb', line 78
def find_by_id(id_to_find)
instances_by_id[id_to_find]
end
|
#find_by_ids(ids) ⇒ Object
82
83
84
|
# File 'lib/passive_record/class_methods.rb', line 82
def find_by_ids(ids)
instances_by_id.values_at(*ids)
end
|
#where(conditions = {}) ⇒ Object
40
41
42
|
# File 'lib/passive_record/class_methods.rb', line 40
def where(conditions={})
Query.new(self, conditions)
end
|